diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..0c0a8242d 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,21 @@ // Predict and explain first... -// =============> write your prediction here +// I believe this will cause an error because str is declared twice // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring -function capitalise(str) { +/* function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } +*/ + +// The error happens because str is declared twice, once as a parameter & again with let +// If I give the new variable a different name, I hope it should work :) + +function capitalise(str) { + let capitalisedString = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedString; +} -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("frumentius")); diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..af93577c1 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,10 +1,12 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// Because decimalNumber is declared twice, once as a parameter and again with const // Try playing computer with the example to work out what is going on +/* + function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -12,9 +14,19 @@ function convertToPercentage(decimalNumber) { return percentage; } -console.log(decimalNumber); +console.log(convertToPercentage(decimalNumber)); */ -// =============> write your explanation here +// decimalNumber is already a parameter, so declaring it again with const causes an error +// decimalNumber has already been declared as a parameter in the same function scope +// console.log(decimalNumber) is wrong because decimalNumber is not defined outside the function, +// so we need to call the function with a value // 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.7)); diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..c0330bf46 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,30 @@ - // 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 +// I think using 3 will cause an error as parameter +// A parameter must be a valid variable name, not a 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 parser sees the number 3 where it expects a valid parameter name, +// so it cannot understand the structure of the function +// It means we cannot use a number as a parameter name // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} +console.log(square(7)); diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..e2679cf2e 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,24 @@ // Predict and explain first... // =============> write your prediction here +// I believe the function will calculate 10 x 32 -function multiply(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 +// After calling multiply(10, 32), the result is undefined because there is no return statement +// It can still print 320 because console.log() prints the result inside the function + // 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-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..45d4b156f 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,22 @@ // Predict and explain first... // =============> write your prediction here +// I guess the function will return error or undefined because the return; -function sum(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 problem is the semicolon immediately after return that prevent to reach out to 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)}`); diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..b10031f3a 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -2,8 +2,9 @@ // Predict the output of the following code: // =============> Write your prediction here +// The output will be the same because we don't give the function a parameter -const num = 103; +/*const num = 103; function getLastDigit() { return num.toString().slice(-1); @@ -11,14 +12,30 @@ 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)}`); +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 output is the same but the last digit is 3 + // Explain why the output is the way it is // =============> write your explanation here +// Because of the global variable and also because the function has no parameter + // 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 + +// Because the function was using the global variable, it ignored the arguments passed to it outside of the function box diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..3bde88c40 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,10 @@ // It should return a string of their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height + let BMI = weight / (height * height); + + return BMI.toFixed(1); } +console.log(calculateBMI(62, 1.67)); + +// return the BMI of someone based off their weight and height diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..605ce8295 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -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) { + return input.replaceAll(" ", "_").toUpperCase(); +} + +console.log(toUpperSnakeCase("alpha and omega")); diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..ee41cc557 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,21 @@ // 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(amount) { + const amountWithoutTrailingP = amount.substring(0, amount.length - 1); + + const paddedPenceNumberString = amountWithoutTrailingP.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("1250p")); diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..a99388fa0 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -14,6 +14,7 @@ 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 @@ -21,18 +22,18 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// 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 +// 0 // c) What is the return value of pad when it is called for the first time? -// =============> write your answer here +// "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 +// 1 // e) What is the return value of pad when it is called for the last time in this program? Explain your answer -// =============> write your answer here +// "01" diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..f86d01511 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -14,12 +14,12 @@ const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; console.assert( currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` + `current output: ${currentOutput}, target output: ${targetOutput}`, ); const currentOutput2 = formatAs12HourClock("23:00"); const targetOutput2 = "11:00 pm"; console.assert( currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` + `current output: ${currentOutput2}, target output: ${targetOutput2}`, );