Skip to content
16 changes: 15 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
// Predict and explain first...
// =============> write your prediction here
// I don't think this will work because str is already an argument in the capitalise function
// so declaring it again is incorrect. Another thing is the variable str is being declared but the value assigned to
// it basically uses the str variable to interpolate the string

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring



function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

// =============> write your explanation here
// =============> write your explanation here//
// SyntaxError: Identifier 'str' has already been declared
// this is happening because the str variable already exist as an argument in this function
// =============> write your new code here

function capitalize(str){
let car = `${str[0].toUpperCase()}${str.slice(1)}`;
return car;
}
capitalize("hello")
console.log(capitalize("hello"))
21 changes: 14 additions & 7 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@

// Try playing computer with the example to work out what is going on

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
// function convertToPercentage(decimalNumber) {
// const decimalNumber = 0.5;
// const percentage = `${decimalNumber * 100}%`;

return percentage;
}
// return percentage;
// }

console.log(decimalNumber);
// console.log(decimalNumber);

// =============> write your explanation here

// Prediction decimalNumber is already used as an argument so the variable name needs to be different
// Finally, correct the code to fix the problem
// =============> write your new code here
function ConvertToPercentage(decimalNumber){
// const decimalNum = 0.5;
const percentage = `${decimalNumber* 100}%`;
return percentage;
}

console.log(ConvertToPercentage(0.5))
12 changes: 8 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

// Predict and explain first BEFORE you run any code...

// It will probably not run because the value 2 is a fixed integer, your parameters / arguments get values
// assigned to them so you can't really reassign 2.
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
Expand All @@ -10,11 +11,14 @@ function square(3) {
}

// =============> write the error message here

// SyntaxError: Unexpected number
// =============> explain this error message here

// This is a syntax error the number 2 cannot be used as a parameter since it cannot be reassigned.
// Finally, correct the code to fix the problem

// =============> write your new code here

function square(num){
return num*num;
}
// console.log(square(3))

11 changes: 9 additions & 2 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// Predict and explain first...

// =============> write your prediction here

// This function is not returning anything
function multiply(a, b) {
console.log(a * b);
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here

// This function is not returning anything. It does multiply the assigned values of a,b but it does not return the String with the
// as required on line 9
// Finally, correct the code to fix the problem
// =============> write your new code here

function multiply(a,b){
return(a*b);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return a * b; would be enough.

The general syntax of the return statement is:

  return expression;

And the value of expression is returned.

}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
8 changes: 7 additions & 1 deletion Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Predict and explain first...
// =============> write your prediction here

// this function does not return anything since the return statements has a semicolon before the command of what
// it has to actually return
function sum(a, b) {
return;
a + b;
Expand All @@ -11,3 +12,8 @@ console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a,b){
return (a+b);

}
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
13 changes: 11 additions & 2 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...

// the variable num is declared outside of the function so it's a global variable
// and it is constant. It should be a parameter or an argument of the getLastDigit function
// Predict the output of the following code:
// =============> Write your prediction here

Expand All @@ -15,10 +16,18 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);

// Now run the code and compare the output to your prediction
// =============> write the output here

// the output is 3 for everything
// Explain why the output is the way it is
// firstly because num is constant and it is a global variable outside the function. The last digit on 103 remains 3. It does not change
// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here

function getLastDigit(num){
return num.toString().slice(-1)
}
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
5 changes: 4 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
let bmi = weight/(height *height)
return `${bmi.toFixed(1)}`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What type of value do you expect your function to return? A number or a string?
Does your function return the type of value you expect?

Different types of values may appear identical in the console output, but they are represented and treated differently in the program. For example,

  console.log(123);              // Output 123
  console.log("123");            // Output 123
  
  // Treated differently in the program
  let sum1 = 123 + 100;         // Evaluate to 223 -- a number
  let sum 2 = "123" + 100;      // Evaluate to "123100" -- a string.

Note:

return `${bmi.toFixed(1)}`;

is the same as

return bmi.toFixed(1);

// return the BMI of someone based off their weight and height
}
}
console.log(calculateBMI(80 , 1.73))
5 changes: 5 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@
// You will need to come up with an appropriate name for the function
// Use the MDN string documentation to help you find a solution
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase

function UpperSnakeCase(str) {
Comment on lines +17 to +18

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you look up the naming conventions in JavaScript? In particulars,

  • Variable and function names
  • Class and Types names
  • Named constants

Then, update the function name according to those conventions.

return `${str.toUpperCase().replaceAll(" ", "_")}`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of string template is no needed.

}
console.log(UpperSnakeCase("hello world in javascript"));
24 changes: 24 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,27 @@
// You will need to declare a function called toPounds with an appropriately named parameter.

// You should call this function a number of times to check it works for different inputs
function toPounds(penceString){

const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(
3,
"0"
);
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

const result = `£${pounds}.${pence}`;
return result
Comment on lines +27 to +28

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is not properly formatted.

Have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode,
as recommended in
https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/blob/main/readme.md
?

}
console.log(toPounds("399p"))
20 changes: 15 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,34 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

console.log(formatTimeDisplay(61))

// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
// to help you answer these questions

// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// pad will be called 3 times

// Call formatTimeDisplay with an input of 61, now answer the following:

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// The value assigned to num when pad is called for the first
// time is 0 (the value of totalHours).

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value of pad when it is called for the first time is "00"
// because 0 is padded with a leading zero.

// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
//The value assigned to num when pad is called for the
// last time is 1 (the value of remainingSeconds).
// This is because the return statement is executed from left to right, so the order of pad calls is: totalHours, remainingMinutes, and finally remainingSeconds.



// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
// =============> write your answer here
//The return value of pad when it is called for the last time is "01" because 1 is padded with a leading zero.

// The final output of formatTimeDisplay(61) will be "00:01:01".
20 changes: 20 additions & 0 deletions Sprint-2/5-stretch-extend/format-time.js

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not complete the implementation of the formatAs12HourClock() function? It's a good programming exercise.

Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,23 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
const currentOutput3 = formatAs12HourClock("14:00");
const targetOutput = "02:00 pm";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

const currentOutput4 = formatAsHourClock("00:00");
const targetOutput4 = "12:00 am";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);

const currentOutput5 = formatAsHourClock("17:00");
const targetOutput5 = "05:00 pm";
console.assert(
currentOutput5 === targetOutput5,
`currentOutput: ${currentOutput5}, target output: ${targetOutput5}`
);
Loading