Skip to content
Open
12 changes: 10 additions & 2 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Predict and explain first...
// =============> write your prediction here
// The code will throw a SyntaxError

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

function capitalise(str) {
// Original code with error:
/* function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
} */

// =============> write your explanation here
// The error occurs because because the variable 'str' is being redeclared inside the function.
// The parameter 'str' is already defined, and trying to declare it again with 'let' causes a conflict.

// =============> write your new code here
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
14 changes: 13 additions & 1 deletion Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// The error will occur because the variable 'decimalNumber' is being redeclared.

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

/*
function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
Expand All @@ -13,8 +14,19 @@ function convertToPercentage(decimalNumber) {
}

console.log(decimalNumber);
*/

// =============> write your explanation here
// The parameter decimalNumber is already declared as part of the function.
// When we try to declare it again inside the function, it causes a syntax error because
// we cannot redeclare a variable that is already declared in the same scope.

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(convertToPercentage(0.5));
13 changes: 9 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@

// Predict and explain first BEFORE you run any code...
// It will throw a syntax error because 3 is not a valid.

// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here

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

*/
// =============> write the error message here
// SyntaxError: Unexpected number

// =============> explain this error message here
// The error occurs because function parameters must be valid identifiers (i.e., variable names), not literal numbers.

// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {
return num * num;
}


console.log(square(3));
12 changes: 10 additions & 2 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
// Predict and explain first...

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

// I think it will return undefined.
/*
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
// The function multiply does not have a return statement, so it returns undefined by default.
// The string, it will show "The result of multiplying 10 and 32 is undefined".

// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
return a * b;
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
11 changes: 11 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
// Predict and explain first...
// =============> write your prediction here
// It will return undefined.

/*
function sum(a, b) {
return;
a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
*/

// =============> write your explanation here
// The function sum returns nothing because the return statement is followed by a semicolon
// and the expression `a + b` is not returned.

// 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)}`);
20 changes: 20 additions & 0 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

// Predict the output of the following code:
// =============> Write your prediction here
// The output will be 3 in each expression.

/*
const num = 103;

function getLastDigit() {
Expand All @@ -12,13 +14,31 @@ function getLastDigit() {
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)}`);
*/

// Now run the code and compare the output to your prediction
// =============> write the output here
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3

// Explain why the output is the way it is
// =============> write your explanation here
// The function getLastDigit always returns the last digit of the variable `num`, which is 103.

// 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

// The function getLastDigit was not working properly because it was using a variable `num`
// instead of the parameter passed to the function. By changing the function to accept a parameter `num`,
// it now correctly returns the last digit of the number passed to it.
5 changes: 3 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
// return the BMI of someone based off their weight and height
return Number((weight / (height * height)).toFixed(1));
}
6 changes: 6 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,9 @@
// 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 toUpperSnakeCase(input) {
return input.trim().toUpperCase().replace(/\s+/g, "_");
}

console.log(toUpperSnakeCase("manchester united")); // "MANCHESTER_UNITED"
9 changes: 9 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,12 @@
// 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(penceAmount) {
const padded = penceAmount.padStart(3, "0");
const pounds = padded.substring(0, padded.length - 2);
const pence = padded.substring(padded.length - 2);
return `£${pounds}.${pence}p`;
}

console.log(toPounds("399"));
18 changes: 18 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,35 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// Answer: 3 times
// pad(totalHours)
// pad(remainingMinutes)
// pad(remainingSeconds)

// 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
// pad(num) = pad(0) = 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// pad(num) = pad(0) = "00"

// 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
// Answer: pad(num) = pad(1) = 1
// Explanation below:
// Last call: pad(remainingSeconds)
// remainingSeconds = 1
// pad(1) → num = 1

// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// Answer: pad(num) = pad(1) = "01"
// Explanation below:
// Last call: pad(remainingSeconds)
// remainingSeconds = 1
// toString() → "1"
// "1".padStart(2, "0") → "01"
// pad(1) → "01"