diff --git a/.gitignore b/.gitignore index bde36e530..925c16180 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ node_modules .DS_Store .vscode -**/.DS_Store \ No newline at end of file +**/.DS_Store +**/runme.md +.devcontainer/ \ No newline at end of file diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..559de913b 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,6 @@ // Predict and explain first... // =============> write your prediction here +// A syntax error should occur because the function parameter is named `str`, but it is also being redeclared inside the function. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +11,11 @@ function capitalise(str) { } // =============> write your explanation here +// The code outputs the error `SyntaxError: Identifier 'str' has already been declared`. +// JavaScript does not allow you to redeclare a variable or parameter with the same name in the same scope. +// In this case, the parameter `str` is being redeclared as a `let` variable inside the function, which is not allowed. // =============> write your new code here +function capitalise(str) { + const capitalisedString = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedString; +} diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..df4cc879d 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,6 +2,7 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// An error such as `decimalNumber is not defined` will occur because `decimalNumber` is not defined in the global scope. // Try playing computer with the example to work out what is going on @@ -15,6 +16,17 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +// The first error occurs when attempting to run `console.log(decimalNumber)`, resulting in a `SyntaxError: Identifier 'decimalNumber' has already been declared`. +// This happens because `decimalNumber` is declared twice within the same scope: once as a parameter of the function and again as a constant inside the function. +// The second error is that `decimalNumber` is not defined in the global scope. +// Therefore, trying to log it outside the function will result in a `ReferenceError: decimalNumber is not defined`. +// Instead, the function should use the parameter `decimalNumber` directly without redeclaring it. // 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)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..9963c6c33 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,26 @@ - // 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 +// A SyntaxError will occur because you cannot use a number such as `3` as a function parameter name. +// Names for parameters must be valid identifiers, for example, starting with a letter or underscore. function square(3) { return num * num; } // =============> write the error message here +// `SyntaxError: Unexpected number` // =============> explain this error message here +// Function parameters are placeholders for the values that will be passed into the function. +// These placeholders must follow the same naming rules as variables, and a variable name cannot be a number. +// In this instance, the code `function square(3)` is attempting to declare a parameter with the name `3`, which is not a valid identifier, hence the `SyntaxError`. // Finally, correct the code to fix the problem // =============> write your new code here - - +function squareFixed(num) { + return num * num; +} diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..9989d7db0 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,10 @@ // Predict and explain first... // =============> write your prediction here +// The console will output two lines. +// 1. `320` (from inside the multiply function) +// 2. `The result of multiplying 10 and 32 is undefined` +// This is because the `multiply` function logs the result but does not return it, as functions that do not explicitly return a value return `undefined` by default. function multiply(a, b) { console.log(a * b); @@ -9,6 +13,13 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// The bug occurs because the `multiply` function uses `console.log()` to display the result instead of using the `return` keyword. +// When the function call is placed inside the template literal, it resolves to its return value, which is `undefined`. // Finally, correct the code to fix the problem // =============> write your new code here +function multiplyFixed(a, b) { + return a * b; +} + +console.log(`The result of multiplying 10 and 32 is ${multiplyFixed(10, 32)}`); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..d9d2db4b3 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,7 @@ // Predict and explain first... // =============> write your prediction here +// The output should be `The sum of 10 and 32 is undefined` given that the `return;` statement is on a line by itself before the actual calculation. +// This should cause the function to exit and return `undefined`. function sum(a, b) { return; @@ -9,5 +11,14 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The `return` keyword immediately stops the execution of a function and returns a value. +// Here, `return;` is called without a value, so the function exits and returns `undefined`. +// As a result, the line `a + b;` is never executed. + // Finally, correct the code to fix the problem // =============> write your new code here +function sumFixed(a, b) { + return a + b; +} + +console.log(`The sum of 10 and 32 is ${sumFixed(10, 32)}`); diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..b87e47b48 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,6 +2,8 @@ // Predict the output of the following code: // =============> Write your prediction here +// The output will be the same, `3`, for all three calls. +// This is because the function ignores its input parameter and always operates on the global constant `num`. const num = 103; @@ -15,10 +17,22 @@ 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` is defined without any parameters, so it cannot use the numbers (42, 105, 806) being passed to it. +// Instead the function accesses the global constant `num` (which is 103) every time it is called. +// Therefore, it always resolves the last digit of 103, which is '3'. + // Finally, correct the code to fix the problem // =============> write your new code here +function getLastDigitFixed(inputNumber) { + return inputNumber.toString().slice(-1); +} -// This program should tell the user the last digit of each number. -// Explain why getLastDigit is not working properly - correct the problem +console.log(`The last digit of 42 is ${getLastDigitFixed(42)}`); +console.log(`The last digit of 105 is ${getLastDigitFixed(105)}`); +console.log(`The last digit of 806 is ${getLastDigitFixed(806)}`); diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..9754b97b4 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -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 -} \ No newline at end of file + const bmi = weight / (height * height); + return parseFloat(bmi.toFixed(1)); +} + +console.log(`BMI for 82.4kg, 1.82m: ${calculateBMI(82.4, 1.82)}`); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..c0f62a81c 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,11 @@ // 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(inputString) { + return inputString.toUpperCase().replaceAll(" ", "_"); +} + +console.log( + `"a wizard of earthsea" becomes: ${toUpperSnakeCase("a wizard of earthsea")}` +); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..648ad4c06 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -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(`"399p" converts to: ${toPounds("399p")}`); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..831e3e71c 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,25 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// The `pad` function will be called 3 times. This is because the return statement in `formatTimeDisplay` calls `pad` for `totalHours`, `remainingMinutes`, and `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` will be `0`. // c) What is the return value of pad is called for the first time? // =============> write your answer here +// The return value will be the string `"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 +// The value assigned to `num` will be `1`. +// With an input of 61, `remainingSeconds` is calculated as `61 % 60`, which is `1`. +// The last call to `pad` in the `return` statement uses `remainingSeconds` as its argument. // 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 will be the string `"01"`. +// The `pad` function takes the number `1`, converts it to the string `"1"`, and then uses `padStart(2, "0")` to add a leading zero, resulting in `"01"`.