diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..872ff72bc 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,7 @@ // Predict and explain first... // =============> write your prediction here +// I predict that the code will throw an error due to the fact that the function redeclares the +// the str using let str. This is not allowed as it is already defined as a parameter. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -9,5 +11,15 @@ function capitalise(str) { return str; } -// =============> write your explanation here +//console.log(capitalise("hello")); +// =============> write your explanation here +// The error occures due to the fact that str is already declared as a function parameter. +// Using let str inside the function tries to redeclare the same variable, which is causing a syntaxError for Javascript. + +// =============> write your new code 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..2cd06cad0 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,6 +2,9 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// My prediction is that this will solution will fail due to the fact that the decimalNumber is being declared twice. +// The second issue is the decimalNumber is being logged to the console, but this is parameter for convertToPercentage. +// So to call the function accurately we need to call convertToPercentage not decimalNumber. // Try playing computer with the example to work out what is going on @@ -15,6 +18,14 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +// The function convertToPercentage takes a decimal number as parameter. +// It multiplies the number by 100 and converts it into a string with a "%" symbol. +// The corrected version removes the duplicate declaration and calls the function properly. // 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..8e19b6620 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 +// This function will cause a syntax error due to the fact that the parameter should be variable name, not a literal value. +// The second issue is that "num" is used inside the function however it is not defined anywhere. function square(3) { return num * num; } // =============> write the error message here +// SyntaxError: Unexpected number +// at internalCompileFunction (node:internal/vm:76:18) +// at wrapSafe (node:internal/modules/cjs/loader:1283:20) +// at Module._compile (node:internal/modules/cjs/loader:1328:27) +// at Module._extensions..js (node:internal/modules/cjs/loader:1422:10) +// at Module.load (node:internal/modules/cjs/loader:1203:32) +// at Module._load (node:internal/modules/cjs/loader:1019:12) +// at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12) +// at node:internal/main/run_main_module:28:49 // =============> explain this error message here +// This error message means that JS expects a parameter name within the function. +// The second error message is "num" is not declared. // 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/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..2cd730fb7 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,22 @@ // Predict and explain first... // =============> write your prediction here +// The function multiply(a, b) logs the result within the function but does not return anything. 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 +// Console.log() will print something. +// Return gives a value back to the caller - the function currently will print the result but does not return it. // 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..723346c6b 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,24 @@ -// Predict and explain first... -// =============> write your prediction here + // Predict and explain first... + // =============> write your prediction here + // I predict that the function will not return undefined. + // This is due to the fact that the return statement is on its own line, therefore trying to do the addition will not work as the code is not read. -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 -// Finally, correct the code to fix the problem -// =============> write your new code here + // =============> write your explanation here + // The return statement is written on its own line, therefore JS automatically inserts a semicolon after the return. + // This means that the function exits immediately and returns undefined. + // The expression a + b; is not 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..2cd0c49bd 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 +// I predict that the three console.log messages will always output 3. +// This is due to the fact that the const num is defined as 103 and the program will always use that global value as the source of truth. const num = 103; @@ -9,16 +11,32 @@ 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 global value that is defined outside of the function is used as the source of truth. +// Therefore when the program executes the line return num.toString().slice(-1); it always considers the global value. +// The console.log ignores the values. + // 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 diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..9ecdd096b 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -14,6 +14,12 @@ // Then when we call this function with the weight and height // It should return their Body Mass Index to 1 decimal place +// solution 1 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 ** 2); + return Number(bmi.toFixed(1)); +} +console.log(`My BMI is: ${calculateBMI(55, 1.3)}`) + +// solution 2 +// To make this program fit for real world use, I would implement a conversion for various metrics used for height and weight. \ 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..64c3ba4f0 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,8 @@ // 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 +// Solution +function toUpperSnakeCase(str) { + return str.toUpperCase().replaceAll(" ","_"); +} +console.log(toUpperSnakeCase("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..614702682 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,17 @@ // 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 + +//Solution: +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("45p")); +console.log(toPounds("5p")); +console.log(toPounds("1200p")); \ 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..2cc6bcbd2 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,6 +11,8 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +console.log(formatTimeDisplay(61)); + // 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 @@ -18,17 +20,25 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// Pad will be called three times (for hours, minutes and 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 +// The value assigned to num for the first time that it is called is 0 (totalHours = 0) // c) What is the return value of pad is called for the first time? // =============> write your answer here +// The return value of pad for its first call 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 +// The value is 1, when formatTimeDisplay(61) runs: 61 seconds = 1 minute and 1 second +// remainingSeconds = 61 % 60 = 1 +// The last call to pad is pad(remainingSeconds), so num receives 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 +// The return value is "01" - pad converts the number to a string and ensures it has at least 2 digits using padStart(2, "0") +// Since "1" has only one digit, a leading zero is added so the value is then "01"