diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..f550b4aef 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,29 @@ // Predict and explain first... // =============> write your prediction here +// Prediction: The code will throw a SyntaxError because the variable 'str' has already been declared as a parameter of the function. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +// Original code with error: + +/* function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } + */ // =============> write your explanation here +// Explanation: The error occurs because the variable 'str' is being redeclared within the function using 'let', +// which is not allowed since 'str' is already declared as a parameter of the function. +// To fix this, we can either rename the inner variable or simply assign the new value to the existing parameter without redeclaring it. + // =============> write your new code here +// Fixed code: +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} + +console.log(capitalise("hello")); // Output: "Hello" \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..4fe864aab 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,9 +2,14 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// Prediction: The error will occur because the variable 'decimalNumber' is being redeclared +// inside the function using 'const', which is an illegal operation. +// Variables declared with 'const' cannot be redeclared in the same scope. // Try playing computer with the example to work out what is going on +/* +Original code: function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -13,8 +18,18 @@ function convertToPercentage(decimalNumber) { } console.log(decimalNumber); +*/ // =============> write your explanation here +// Explanation: The error occurs because 'decimalNumber' is declared as a parameter of the function. +// When we try to declare it again inside the function with 'const', it causes a syntax error. +// To fix this, we should either use the parameter directly or rename the inner variable. // 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)); // Output: 50% \ 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..07c76552c 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,34 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// Prediction: It will throw a syntax error because 3 is not a valid parameter name. +/* Original Code: function square(3) { return num * num; } +*/ // =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here +// MDN Reference: https://developer.mozilla.org/en-US/docs/Glossary/Identifier + +// JavaScript does not allow numbers to be used as variable names or parameter names. +// Function parameters must follow the rules for valid identifiers. +// According to MDN, an identifier may not start with a digit and must follow JavaScript's naming rules. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} + +console.log(square(3)); // 9 +// Test cases: +// console.log(square(10)); // 100 +// console.log(square(5)); // 25 \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..aec2df87f 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,35 @@ // Predict and explain first... // =============> write your prediction here +// Prediction: The function will not multiply the numbers. +// Instead, it will print the sum of a and b (42) and the final console.log will ouput: +// The result of multiplying 10 and 32 is undefined +/* Original code function multiply(a, b) { console.log(a * b); } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +*/ + +// =============> Error message +// 320 +// The result of multiplying 10 and 32 is undefined // =============> write your explanation here +// MDN Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return + +// The function multiply() uses console.log() to print the result but it does not return anything. +// In JavaScript, if a function does not explicitly return a value, it returns undefined. +// When we try to use multiplay(10, 32) inside a console.log, it returns undefined. +// To fix this, we must use the return statement so the function gives back a value that can be used. // Finally, correct the code to fix the problem // =============> write your new code here +function multiply(a, b) { + return a * b; +} + +// The result of multiplying 10 and 32 is 320 +console.log(`The result of multiplying 10 and 32 is ` + multiply(10, 32)); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..99651445c 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,32 @@ // Predict and explain first... // =============> write your prediction here +// Prediction: The code should output - "The sum of 10 and 32 is 42" +// But the code will return undefined instead of 42. +/* Original code function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +*/ + +// =============> Error message +// The sum of 10 and 32 is undefined // =============> write your explanation here +// MDN Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return + +// The function uses `return;` without an expression. +// According to MDN, if a return statement has no value, the function returns undefined. +// So when `sum(10, 32)` is called, it returns undefined. +// To fix this, the return statement must include the expression `a + b`. + // 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..e18ffa597 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,7 +2,9 @@ // Predict the output of the following code: // =============> Write your prediction here +// Prediction: The function will always return "3" because it uses the hardcoded value 103. +/* Original code: const num = 103; function getLastDigit() { @@ -12,13 +14,35 @@ 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 +// MDN Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions#function_parameters + +// The function does not use the input number passed in. +// It always returns the last digit of the variable `num`, which is 103. +// To fix this, the function should accept a parameter and use that instead of the hardcoded value. + // 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 original function was not working as it wasn't taking any parameters and always used the variable `num = 103`. +// So no matter what number you passed in, the function would ignore it and always returned the hardcoded value, "103" +// instead, which was "3", in this case. \ 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..0608c2c57 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,8 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file + return (weight / (height * height)).toFixed(1); // toFixed(1) rounds to 1 decimal place. +} + +// Test case +console.log(calculateBMI(70, 1.73)) // Output: 23.4 \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..4ce0e1ba1 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,22 @@ // 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 + +// MDN References I used: +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim + +function toUpperSnakeCase(input) { + return input + // Removes extra spaces + .trim() + // Converts everything to caps + .toUpperCase() + // Replaces all spaces with underscores + // g means global, so it applies to the whole string + // / / is a regular expression matching a space + .replace(/ /g, "_"); +} + +console.log(toUpperSnakeCase("hello there")); // HELLO_THERE \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..c2cb60b8b 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,26 @@ // 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 + +// Converts a string representing pence (e.g. "399") +// into pounds.pence format (e.g. "3.99") +// MDN References used: +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring + +// Converts a pence string (e.g. "399") +function toPounds(penceString) { + // Ensure the string is atleast 3 characters long by padding with leading zeros if needed + // e.g. "5" → "005", "99" → "099", "399" stays "399" + const padded = penceString.padStart(3, "0"); + // Extract everything except the last two digits → this becomes the pounds + // e.g. "399" → "3" + const pounds = padded.substring(0, padded.length - 2); + // Extract the last two digits → this becomes the pence + // e.g. "399" → "99" + const pence = padded.substring(padded.length - 2); + // Combines pounds and pence into the final money format + return `${pounds}.${pence}`; +} + +console.log(toPounds("399")); // 3.99 \ No newline at end of file diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..6e2f1ab04 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,42 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// pad(totalHours) +// pad(remainingMinutes) +// pad(remainingSeconds) +// Answer: 3 // 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 +// formatTimeDisplay: +// seconds = 61 +// remainingSeconds = 61 % 60 = 1 +// totalMinutes = (61 - 1) / 60 = 1 +// remainingMinutes = 1 % 60 = 1 +// totalHours = (1 - 1) / 60 = 0 +// Answer: pad(num) = pad(0) = 0 // c) What is the return value of pad is called for the first time? // =============> write your answer here +// First call: pad(totalHours) +// totalHours = 0 +// pad(0) → "00" +// Answer: "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 +// Last call: pad(remainingSeconds) +// remainingSeconds = 1 +// pad(1) → num = 1 +// Answer: 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 +// Last call: pad(remainingSeconds) +// remainingSeconds = 1 +// toString() → "1" +// "1".padStart(2, "0") → "01" +// pad(1) → "01" +// Answer = "01" \ No newline at end of file