diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..227bd3158 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -4,10 +4,15 @@ // 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; -} +// function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +// } // =============> write your explanation here // =============> write your new code here +function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; +} + +console.log(capitalise("hello")); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..d699325d1 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -5,16 +5,22 @@ // 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 // 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)); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..f545fc449 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,29 @@ - // Predict and explain first BEFORE you run any code... // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// SyntaxError: Unexpected number -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 +// The function parameter is written as 3, which is a number literal. +// Function parameters must be variable names (identifiers). +// JavaScript does not allow numbers as parameter names, +// so it throws a SyntaxError before running the program. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} +console.log(square(3)); diff --git a/Sprint-2/1-key-errors/answers.md b/Sprint-2/1-key-errors/answers.md new file mode 100644 index 000000000..0cadbfafa --- /dev/null +++ b/Sprint-2/1-key-errors/answers.md @@ -0,0 +1,101 @@ +# Sprint 2 - 1 Key Errors (Answers) + +## 0.js +- Predicted error: +- Actual error: +- Why it happens: +- Minimal fix: +- Docs: + +## 1.js +- Predicted error: +- Actual error: +- Why it happens: +- Minimal fix: +- Docs: + +## 2.js +- Predicted error: +- Actual error: +- Why it happens: +- Minimal fix: + + + +## 0.js + +- Predicted error: SyntaxError (variable redeclaration) + +- Actual error: SyntaxError: Identifier 'str' has already been declared + +- Why it happens: + The function parameter `str` is already declared. Inside the function, `let str = ...` attempts to redeclare the same variable in the same scope. JavaScript does not allow redeclaration of variables using `let`. + +- Minimal fix: + Remove the `let` and return the new string directly: + + ```js + function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; + } + + - Docs: + https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let + + + + ## 1.js +- Predicted error: SyntaxError (variable redeclaration) + +- Actual error: SyntaxError: Identifier 'decimalNumber' has already been declared + +- Why it happens: + The function parameter `decimalNumber` is already declared. Inside the function, `const decimalNumber = 0.5;` attempts to redeclare the same variable in the same scope. JavaScript does not allow redeclaration using `const`. + + There is also a second issue: + `console.log(decimalNumber);` is outside the function, and `decimalNumber` is not defined in the global scope. This would cause a ReferenceError. + +- Concepts tested: + Variable scope, function parameters, redeclaration rules, global vs local variables. + + function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} + +console.log(convertToPercentage(0.5)); + + + + +## 2.js + +- Predicted error: SyntaxError (invalid function parameter) + +- Actual error: SyntaxError: Unexpected number + +- Why it happens: + The function is declared as `function square(3)`. + Function parameters must be variable names (identifiers), not literal values. + The number `3` is not a valid parameter name, so JavaScript throws a syntax error. + +- Concept tested: + Function declaration syntax and valid parameter identifiers. + +- Minimal fix: + +```js +function square(number) { + return number * number; +} + +console.log(square(3)); + + + + + + + + + diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..c6086cab8 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,29 @@ // Predict and explain first... // =============> write your prediction here +// The function will print 320 from inside multiply, +// but the final console.log will show "undefined" +// because multiply does not return a 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 function multiply only logs the result using console.log, +// but it does not return the value. +// Since it does not return anything, JavaScript returns undefined by default. +// Therefore, the template string inserts 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)}`); + + diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..b98071272 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,27 @@ // Predict and explain first... // =============> write your prediction here +// The function will return undefined because the return statement +// is followed by a line break. JavaScript inserts a semicolon automatically, +// so the function exits before a + b is executed. -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 return statement is on its own line. +// JavaScript automatically inserts a semicolon after return. +// This means the function returns undefined immediately, +// and the expression a + b is never executed. + // 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..f37ddbb8a 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,23 +2,38 @@ // Predict the output of the following code: // =============> Write your prediction here +// The function will always return 3 because it uses the global variable num (103). +// So all outputs will show 3 regardless of the input. -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 function does not take a parameter, so it ignores the values passed in. +// It always uses the global variable num (which is 103). +// Therefore, it always returns the last digit of 103, which is 3. + // Finally, correct the code to fix the problem // =============> write your new code here -// This program should tell the user the last digit of each number. -// Explain why getLastDigit is not working properly - correct the problem +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)}`); \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..944479920 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -14,6 +14,20 @@ // Then when we call this function with the weight and height // 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 +// } + + function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file + const bmi = weight / (height * height); + return bmi.toFixed(1); +} + +// Test cases +console.log(calculateBMI(70, 1.73)); // Output: 23.4 +console.log(calculateBMI(80, 1.8)); // Output: 24.7 + + + + diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..142ea3fcc 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,13 @@ // 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.replaceAll(" ", "_").toUpperCase(); +} + +// Test cases +console.log(toUpperSnakeCase("hello there")); // HELLO_THERE +console.log(toUpperSnakeCase("lord of the rings")); // LORD_OF_THE_RINGS + diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..ee531dd6e 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -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 + + + +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}`; +} + +// Test cases +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("5p")); // £0.05 +console.log(toPounds("1200p")); // £12.00 + diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..5511599fa 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,24 +11,21 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } -// 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 +// =============> 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 +// =============> 0 (the first call is pad(totalHours), and totalHours is 0 when seconds = 61) -// 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? +// =============> "00" (0 becomes "0" then padStart(2,"0") makes it "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 (the last call is pad(remainingSeconds), and 61 % 60 = 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 +// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer +// =============> "01" ("1" is padded to two digits using padStart) \ No newline at end of file diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..9cc463c99 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -3,11 +3,17 @@ // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; - } - return `${time} am`; + const hours24 = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + + const period = hours24 >= 12 ? "pm" : "am"; + + let hours12 = hours24 % 12; + if (hours12 === 0) hours12 = 12; + + const paddedHours12 = String(hours12).padStart(2, "0"); + + return `${paddedHours12}:${minutes} ${period}`; } const currentOutput = formatAs12HourClock("08:00"); @@ -23,3 +29,44 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + + + +// -------------------- +// Extra tests (edge cases + minutes) +// -------------------- + +// Midnight (00:00) should be 12:00 am +console.assert( + formatAs12HourClock("00:00") === "12:00 am", + `current output: ${formatAs12HourClock("00:00")}, target output: 12:00 am` +); + +// Noon (12:00) should be 12:00 pm +console.assert( + formatAs12HourClock("12:00") === "12:00 pm", + `current output: ${formatAs12HourClock("12:00")}, target output: 12:00 pm` +); + +// Minutes must be preserved +console.assert( + formatAs12HourClock("23:15") === "11:15 pm", + `current output: ${formatAs12HourClock("23:15")}, target output: 11:15 pm` +); + +console.assert( + formatAs12HourClock("13:05") === "01:05 pm", + `current output: ${formatAs12HourClock("13:05")}, target output: 01:05 pm` +); + +// Morning should stay am +console.assert( + formatAs12HourClock("01:09") === "01:09 am", + `current output: ${formatAs12HourClock("01:09")}, target output: 01:09 am` +); + +// Boundary just before noon +console.assert( + formatAs12HourClock("11:59") === "11:59 am", + `current output: ${formatAs12HourClock("11:59")}, target output: 11:59 am` +); \ No newline at end of file