Skip to content
Open
15 changes: 11 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
// 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;
// }

// =============> SyntaxError: Identifier 'str' has already been declared,
// So In JavaScript, we cannot declare a let variable with the same name as an existing parameter within the same scope. This causes a SyntaxError

// =============> write your new code here
function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

// =============> write your explanation here
// =============> write your new code here
console.log(capitalise("hello"));
25 changes: 18 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,27 @@

// 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);
// =============> JavaScript does not allow a parameter and a const variable inside the same scope to have the same name, so it gives an error.
//console.log(decimalNumber);

// =============> write your explanation here
//decimalNumber only exists inside the function. It is a local variable, so it cannot be accessed outside the function. This causes a ReferenceError.

// 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));
14 changes: 10 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@

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

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

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

//SyntaxError: Unexpected number
// =============> explain this error message here
//A SyntaxError will occur because `3` cannot be used as a function parameter name.
//Function parameters must be valid variable names, such as `num`.

// Finally, correct the code to fix the problem

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

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

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

// =============> write your prediction here
/*
The function should return the multiplication result so that
the returned value can be used inside the template literal.
*/
// function multiply(a, b) {
// return a * b;
// }

function multiply(a, b) {
console.log(a * b);
}

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

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

/*
The original function used console.log() instead of return.
console.log() displays 320 but does not give the value back to
the calling code. Therefore multiply(10, 32) returned undefined.
Using return allows the value 320 to be inserted into the string.
*/
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> 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)}`);
25 changes: 16 additions & 9 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here

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

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
/*The program will output:
"The sum of 10 and 32 is undefined"
because the function returns before performing the addition.*/

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

/*The `return` statement immediately stops the function.
Because it has no value after it, the function returns `undefined`.
The `a + b` line is never executed.*/

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> write your new code here

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

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

// Predict the output of the following code:
// Predict the output of the following code: The function will return "3" every time.
// =============> Write your prediction here

const num = 103;
/*Even though 42, 105, and 806 are passed to getLastDigit(),
the function does not have a parameter to receive those values.
Instead, it always uses the global variable num, whose value is 103.
Therefore, all three lines will output 3.*/

function getLastDigit() {
return num.toString().slice(-1);
}
// const num = 103;

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)}`);
// function getLastDigit() {
// 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)}`);

// 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
// 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
6 changes: 4 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,7 @@
// 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
}
const bmi = weight / (height * height);
return Math.round(bmi * 10) / 10;
}
console.log(calculateBMI(70, 1.73));
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(str) {
return str.toUpperCase().replaceAll(" ", "_");
}

console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));
28 changes: 28 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,31 @@
// 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
// This program takes a string representing a price in pence.
// The program then builds up a string representing the price in pounds.

// 1. Initialise a string variable with the value "399p"
const penceString = "399p";

// 2. Remove the trailing "p" from the string
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

// 3. Make sure the pence value has at least 3 digits
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

// 4. Get the pounds part by removing the last two digits
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

// 5. Get the pence part (the last two digits)
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

// 6. Display the final price
console.log(`£${pounds}.${pence}`);
20 changes: 10 additions & 10 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ function formatTimeDisplay(seconds) {
// Questions

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

// Call formatTimeDisplay with an input of 61, now answer the following:
// =============> 3 times

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

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// c) What is the return value of pad when it is called for the first time?
// =============> "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
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> 1, because formatTimeDisplay(61) has 1 remaining second,
// and the last call to pad is pad(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
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
// =============> "01", because pad adds a zero to the beginning of "1"
// to make it two characters long.
Loading