diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..f09d6b877 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,12 @@ // Predict and explain first... -// =============> write your prediction here +// =============> When we call capitalise("fay"), it will throw an error instead of returning "Fay" // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +// =============> "str" has already been declared, in JavaScript we can not redeclare a parameter using let inside the same scope. +// =============> write your new code here function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + return str = `${str[0].toUpperCase()}${str.slice(1)}`; } - -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("fay")); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..f73785833 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,23 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> 'decimalNumber' has already been declared. + // Try playing computer with the example to work out what is going on + +// =============>The variable in line 9 "decimalNumber" is declared as a parameter passed to the function +// but in line 10 the variable redeclared again. +// Finally, correct the code to fix the problem +// =============> write your new code here + +const decimalNumber = 0.5; function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; + const percentage = `${decimalNumber * 100}%`; return percentage; } console.log(decimalNumber); - -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..ef68ed00a 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,19 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> Function parameters must be variable names not values. -function square(3) { - return num * num; -} -// =============> write the error message here +// =============> SyntaxError: Unexpected number -// =============> explain this error message here +// =============> We can not put a number (3) 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(3)); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..86fda8144 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,13 @@ // Predict and explain first... -// =============> write your prediction here +// =============> The result of the multiplying is undefined -function multiply(a, b) { - console.log(a * b); -} - -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); - -// =============> write your explanation here +// =============> The function logs the result but does not return anything. // 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)}`); \ 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..ff863fd39 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,11 @@ // Predict and explain first... -// =============> write your prediction here +// =============> The sum of 10 and 32 is undefined +// =============> The line break and the semicolon after "return" stops the function and returns undefined. +// Finally, correct the code to fix the problem +// =============> write your new code here function sum(a, b) { - return; - a + b; + return a + b; } 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 diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..3219d8624 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,11 +1,18 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> The last digit will be 3 with all the function. +// Now run the code and compare the output to your prediction +// =============> write the output here +// Explain why the output is the way it is +// =============> A function "getLastDigit" must declare parameters to receive arguments. + //If it doesn’t, any arguments passed in are ignored. +// Finally, correct the code to fix the problem +// =============> write your new code here const num = 103; -function getLastDigit() { +function getLastDigit(num) { return num.toString().slice(-1); } @@ -13,12 +20,5 @@ 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 -// Explain why the output is the way it is -// =============> write your explanation here -// 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 diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..7bfbad379 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,7 @@ 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 parseFloat(bmi.toFixed(1)); +} +console.log(calculateBMI(60, 1.70)); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..2f5a23ffa 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 +function toUpperSnakeCase(str) { + return str.toUpperCase().replaceAll(" ", "_"); +} +console.log(toUpperSnakeCase("hello there")); +console.log(toUpperSnakeCase("lord of the rings")); \ 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..54064fbe2 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,22 @@ // 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}`; +} \ 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..790ecdcf2 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -15,20 +15,19 @@ function formatTimeDisplay(seconds) { // to help you answer these questions // Questions - -// a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here + // a) When formatTimeDisplay is called how many times will pad be called + // // =============> 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 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 +// =============> 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 +// =============> "01" diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..e14cabf86 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -10,16 +10,11 @@ function formatAs12HourClock(time) { return `${time} am`; } + const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; console.assert( currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` + `Expected ${targetOutput} but got ${currentOutput}` ); -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; -console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` -);