diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..2c0ec877e0 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// =============> Prediction: The function capitalise is possibly meant to capitalise the first letter in a string. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -9,5 +9,10 @@ function capitalise(str) { return str; } -// =============> write your explanation here +// =============> =============> write your explanation here +// The 'str' variable on the left hand side is the same used in the function name implementation which should not be. // =============> write your new code here +function capitalise(str) { + let capFunction = `${str[0].toUpperCase()}${str.slice(1)}`; + return capFunction; +} \ 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 f2d56151f4..fef6a3cadc 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,7 +1,7 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> I suspect than an error will occur // Try playing computer with the example to work out what is going on @@ -14,7 +14,10 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here - +// =============> write your explanation here. The functions takes a decimal number as input and converts it to percentage by multiplying by 100 // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} \ 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 aad57f7cfe..f5021c8a5e 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,22 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> write your prediction of the error here. The error is likely to be caused by square(3), '3' is supposed to be replaced by a string representing the number. function square(3) { return num * num; } // =============> write the error message here - +// function square(3) { +// ^ +// SyntaxError: Unexpected number // =============> explain this error message here - +// The muber '3' should be used when calling the function and it should not be used as a parameter. // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..451ba98c57 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here. It may likely have issues as console.log() is within function implementation, instead of using'return' function multiply(a, b) { console.log(a * b); @@ -8,7 +8,10 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here. Instead of having console.log() is within function implementation, it should be replaced with 'return' // Finally, correct the code to fix the problem // =============> write your new code here +function multiply(a, b) { + return (a * b); +} \ 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 37cedfbcfd..9c8926d7ad 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here. An error will result becasue of the ';' between return and a + b + function sum(a, b) { return; @@ -8,6 +9,9 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here. The ideal statement should be 'return a + b; ' but this statement is separated by ';' // Finally, correct the code to fix the problem // =============> write your new code here +function sum(a, b) { + return a + b; +} \ 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 57d3f5dc35..47aba7d876 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,8 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> Write your prediction here. It may run but give incorrect results because of const num & no parameter specified + const num = 103; @@ -15,10 +16,19 @@ 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 +// =============> write your explanation here. num is a constant variable, it's value cannot change. // Finally, correct the code to fix the problem // =============> write your new code here - +function getLastDigit() { + return num.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 +// getLastDigit is not working properly because the value for num is fixed at '103' +function getLastDigit(num) { + return num.toString().slice(-1); +} diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..a0c825b4c6 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,5 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height + return Math.round((weight) / (height ** 2)).toFixed(1) } \ 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 5b0ef77ad9..bdf32e61af 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,7 @@ // 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 upCase(strCar) { + let result = strCar.toUpperCase().replaceAll(" ", "_"); + return result; +} diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..bb2ad27393 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,23 @@ // 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(money) { + const moneyWithoutTrailingP = money.substring( + 0, + money.length - 1 + ); + const paddedPenceNumberString = moneyWithoutTrailingP.padStart(3, "0"); + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + result = pounds + "." + pence; + res = `£${pounds}.${pence}`; + return res; +} +console.log(toPounds("399p")) +console.log(toPounds("1099p")) \ 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 7c98eb0e8c..ebaf68ef12 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,18 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> write your answer here.pad will be called three times. // 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 +// =============> write your answer here. the value assigned to num when pad is called for the first time is 00 // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> write your answer here. the return value of pad is called for the first time 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 +// =============> write your answer here. the value assigned to num when pad is called for the last time is 01 // 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 +// =============> write your answer here. the return value assigned to num when pad is called for the last time in this program is '01'