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
17 changes: 13 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Predict and explain first...
// =============> write your prediction here
// The code will throw an error because the variable 'str' is being redeclared with 'let' inside the function.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
// the error shows that 'str' is already declared in the outer scope, and we cannot redeclare it with 'let' in the inner scope.

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

// =============> write your explanation here
// The code will throw a SyntaxError because the variable 'str' is being redeclared with 'let' inside the function, which is not allowed in JavaScript.
// The function capitalise is trying to declare 'str' again with 'let', which is already declared in the outer scope. This leads to a SyntaxError because 'let' does not allow redeclaration of the same variable in the same scope.
// =============> write your new code here
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
console.log(capitalise("hello"));
22 changes: 16 additions & 6 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// The code will shows an error because the variable 'decimalNumber' is being redeclared with 'const' inside the function, which is not allowed in JavaScript.

// 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
// there is a SyntaxError because the variable 'decimalNumber' is being redeclared with 'const' inside the function.
// The function convertToPercentage is trying to declare 'decimalNumber' again with 'const', which is already declared in the outer scope. This leads to a SyntaxError because 'const' does not allow redeclaration of the same variable in the same scope.


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

return percentage;
}
console.log(convertToPercentage(0.7));
16 changes: 12 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// the code will show an errror because no varible has been declared

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

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

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

// =============> explain this error message here
// The error occurs because the function parameter is incorrectly defined as a number (3) instead of a variable name. In JavaScript, function parameters must be variable names, not literal values. This leads to a SyntaxError because the JavaScript engine expects a valid identifier for the parameter.

// Finally, correct the code to fix the problem

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

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

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

// =============> write your prediction here
// The code will show an error because the function does not have a return value.

function multiply(a, b) {
console.log(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 code will throw an error because the function `multiply` does not return a value, but the console.log statement expects a value to be printed.
// The function `multiply` is defined to log the result of multiplying two numbers, but it does not return anything, leading to an undefined value being logged.
// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
return a * b; // Change console.log to return the result
}
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
17 changes: 12 additions & 5 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
// the code will throw an error because the operation a + b is outside the return syntax.

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

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

// =============> write your explanation here
// The code will throw an error because the return statement is not followed by any value. The function `sum` is expected to return the result of adding `a` and `b`, but since the return statement is immediately followed by a semicolon, it effectively returns `undefined`. This leads to an incorrect output when trying to log the result of the sum operation.

// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a, b) {
return a + b; // Corrected to return the sum of a and b
}
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
26 changes: 19 additions & 7 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,35 @@

// Predict the output of the following code:
// =============> Write your prediction here
// the code will show an error because we didn't set an an identifier in the function's parameter.

const num = 103;
// const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
}
// 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)}`);
// 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 output is the same for all three calls because the function `getLastDigit` uses a hardcoded variable `num` instead of the parameter passed to it. Therefore, it always returns the last digit of `103`, which is `3`, regardless of the input provided in the function call.
// 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 using the parameter `num` correctly.
8 changes: 6 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,9 @@
// 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);// Calculate BMI by dividing weight by height squared

// Return the BMI rounded to 1 decimal place
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(input) // Function to convert a string to UPPER_SNAKE_CASE
{
return input.toUpperCase().replace(/\s+/g, "_");// Convert the string to uppercase and replace spaces with underscores
}
console.log(toUpperSnakeCase("hello there")); // Outputs: "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // Outputs: "LORD_OF_THE_RINGS"
21 changes: 21 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,24 @@
// 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("100p"));
console.log(toPounds("430p"));
6 changes: 6 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,23 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// 3 times, once for each of hours, minutes, and seconds - pad(totalHours), pad(remainingMinutes), and 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
// The value assigned to num when pad is called for the first time is 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value of pad when called for the first time is "00", because it converts the number 0 to a string and pads it with leading zeros to ensure it has a length of at least 2 characters.

// 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 in this program is 1. This is because the last call to pad is for remainingSeconds, which is calculated as 61 % 60, resulting in 1. The function then pads this value to ensure it has a length of at least 2 characters, resulting in "01".

// 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
// The return value assigned to num when pad is called for the last time in this program is "01". This is because the function converts the number 1 to a string and pads it with leading zeros to ensure it has a length of at least 2 characters, resulting in "01".
// This is the final output for the seconds part of the time format, which is displayed as part of the formatted time string.