Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Predict and explain first...
// =============> write your prediction here
// The error in the code occurs because the variable 'str' is being declared twice within the same scope.
// The first declaration is in the function parameter, and the second declaration is inside the function body.
// This causes a syntax error because JavaScript does not allow redeclaration of variables in the same scope using 'Let'.
// To fix this error, we can simply remove the 'Let' keyword from the second declaration of 'str' inside the function body.
// The correction would be to assign the new variable without redeclaration, like this: 'str = `${str[0].toUpperCase()}${str.slice(1)}`;'.
// This way, we are reassigning the value of 'str' without trying to declare it again.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -9,5 +14,12 @@ function capitalise(str) {
return str;
}

// =============> write your explanation here
// =============> write your new code here
// The error in the code occurs because the variable 'str' is being declared twice within the same scope.
// The first declaration is in the function parameter, and the second declaration is inside the function body.
// This causes a syntax error because JavaScript does not allow redeclaration of variables in the same scope using 'Let'.
// To fix this error, I can rename the variable inside the function body to something else, like 'caps', to avoid redeclaration.

function capitalise(str) {
let caps = `${str[0].toUpperCase()}${str.slice(1)}`;
return caps;
}
12 changes: 9 additions & 3 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// Declaring a variable with the same name as the function parameter in the function body causes a SyntaxError.

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

Expand All @@ -14,7 +14,13 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

// =============> write your explanation here
// After running the code, the following error message was displayed:
// Uncaught SyntaxError: Identifier 'decimalNumber' has already been declared

// 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: 7 additions & 6 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

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

// =============> write your prediction of the error here
// The error occurs because 3 is used as a function parameter.
// Parameters must be variable names, whereas values are passed as arguments when the function is called.

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

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

// =============> explain this error message here
// The parser evaluates the code as a SyntaxError before execution because a value cannot be used as a parameter name in JavaScript.

// Finally, correct the code to fix the problem

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


// function square(num) {
// return num * num;
// }
13 changes: 10 additions & 3 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
// The result of multiplying 10 and 32 is expected to be undefined because the multiply() function does not return the result.
// It only prints it using console.log().


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
// After running the code, the result of multiplying 10 and 32 is undefined indeed.
// The problem occurs because the function has no return statement to send a value back to where the function was called.

// 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)}`);
12 changes: 9 additions & 3 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// The issue is that return is on one line and a + b is on the next.
// Therefore, a + b is never returned.

function sum(a, b) {
return;
Expand All @@ -8,6 +9,11 @@ function sum(a, b) {

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

// =============> write your explanation here
// After running the code, the sum of 10 and 32 is undefined.
// JavaScript inserts a semicolon after return when a + b is placed on the next line.
// This causes the function to return before calculating the sum.

// Finally, correct the code to fix the problem
// =============> write your new code here
// function sum(a, b) {
// return a + b;
// }
23 changes: 18 additions & 5 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// I believe that in each case, the output will be the last digit of 103, which is 3.

const num = 103;

Expand All @@ -14,11 +14,24 @@ 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() does not accept a parameter.
// Consequently, it always uses the global variable num, whose value is 103.

// Finally, correct the code to fix the problem
// =============> write your new code here
// const num = 103;
// 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
// getLastDigit() is not working properly because it always uses the global num variable (103).
// I added num as a parameter so that the function can accept the value passed as an argument.
7 changes: 5 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,8 @@
// 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
}
let bmi = weight / (height * height);
return bmi.toFixed(1);
}

console.log(calculateBMI(70, 1.73));
7 changes: 7 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,10 @@
// 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.toUpperCase().replaceAll(" ", "_");
}

console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));
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");

return `£${pounds}.${pence}`;
}

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

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// pad() is called 3 times because it is used once for hours, once for minutes, and once for seconds.

// 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
// Because the first call to pad() is pad(totalHours), and formatTimeDisplay(61) calculates totalHours as 0, the value passed to pad() as num is 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 called for the first time?
// When pad() is called for the first time, num is 0 because totalHours is 0.
// The function converts it to a string and adds a leading zero, so the return value is "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
// Because the last call to pad() is pad(remainingSeconds) and formatTimeDisplay(61) calculates remainingSeconds as 1 (61 % 60 = 1), the value passed into pad() as num is 1.

// 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
// The return value of pad() when it is called for the last time is "01" because num is 1, and the function pads numbers by adding a leading zero.
Loading