diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..093aee121 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,6 +1,6 @@ // Predict and explain first... -// =============> write your prediction here - +// =============> +I predicte that the error will be that the variable str is already declared in the function. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -9,5 +9,12 @@ function capitalise(str) { return str; } -// =============> write your explanation here + +// =============> write your explanation here +when I run the program it give an error message that the identifier str has already been declared. this is because the variable str is declared twice in the function capitalise and in the variable let str. +to fix this we can change the variable name of the let str to newStr and this will fix the issue. // =============> write your new code here +function capitalise(str) { + let newStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return newStr; +} \ 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 f2d56151f..7bbda9ad1 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,7 +1,8 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +=============> write your prediction here: the error will occur because the variable decimslNumber gevine a value of 0.5 + with a const declaration and this will make an error if the brograme given a diffrent value for the decimalNumber. // Try playing computer with the example to work out what is going on @@ -14,7 +15,18 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +// =============> write your explanation here +when the program run it give a SyntaxError: Identifier 'decimalNumber' has already been declared. This is because the variable decimalNumber is declared twice in the function +convertToPercentage, and inside the function with the constant varible it redeclared again also using the function name decimalNumber with the console.log function will give an error because the function name should be +to fix this error we can remove the const variable declaration of decimalNumber from the function and use the function name + 'ConvertToPercentage' to recall the function and pass the value of the decimalNumber.CSSStyleDeclaration // 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..cbbc6ee34 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,32 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here + the error will occur because the paramater of the function is not a valid variable name as it is a number. +also the variable num is not declared in the function and this will give an error when the program run because the function will not know what the value of the num is. function square(3) { return num * num; } -// =============> write the error message here +// =============> write the error message here +function square(3) { + ^ -// =============> explain this error message here +SyntaxError: Unexpected number -// Finally, correct the code to fix the problem +// =============> explain this error message here it means that the paramater of the function is an unexpected number (3) and this is not a valid variable number. +I deleted the paramater of the function and run the programe and I get this error: +/box/index.js:2 + return num * num; + ^ -// =============> write your new code here +ReferenceError: num is not defined +// Finally, correct the code to fix the problem +change the function to this: function square(num) +// =============> 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..b5f3f9235 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,8 @@ // Predict and explain first... // =============> write your prediction here +I prediact that the error will occur because the function multiply does not return any value and this will make the console.log function to print undefined instead. + function multiply(a, b) { console.log(a * b); @@ -9,6 +11,21 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +when the program run it give this output: + +320 +The result of multiplying 10 and 32 is undefined + +320: the out put for the console.log inside the function + +while the ouout the out put for the console.log function out of the function returned the string provided in the function and an undefiened because the function multibly doesn't return any value. +to fix this error we need to change the console.log function inside the multiply function to return. + // 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..a3bf324e7 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 +I predicted that the function will not return the correct result because the return statment is not correctly declared. function sum(a, b) { return; @@ -9,5 +10,15 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +when I run the program it give this output: +The sum of 10 and 32 is undefined + +the output is undefiened because the function sum does not return any value. +to fix this error we need to change the return statement to return a + b instead of just return. + // 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-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..83f5d6292 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 because the num variable has been given a constant value, the program will give undefiened when the getLastDigt function called after. + const num = 103; @@ -14,11 +16,24 @@ 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 output give 3 which is the last digit for the constant value declared to the num +because the function getLastDigit doesn't have the num as a paramater to accept it when we call it with the console.log function. // Finally, correct the code to fix the problem -// =============> write your new code here + +const num = 103; + +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 it hasn't been gevine a paramater when it declared but when the num added as a paramater the program give the xpeacted outputs. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..1ad7dd193 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,10 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + let BMI = weight / (height ** 2); + return BMI.toFixed(1); +} + +console.log(calculateBMI(80, 1.7)); + // return the BMI of someone based off their weight and height -} \ 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..80e6216e8 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/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(str) { + return str.toUpperCase().replace(/ /g, '_') +} + +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..73187627b 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(`£${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..5a96ceed3 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 +Three times, once for the 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 assighned to num when the pad function called for the first time is 0, because the total hours is 0. // c) What is the return value of pad is called for the first time? -// =============> write your answer here + the return value of pad when it is called for the first time is '00' because the pad function add a '0' to the left of the num to make it 2 characters long. AS the num is 0 the return value is also ' 0'. // 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 assighned to num when the pad function is called for the last time is 1 , because the remaining seconds is 1, as 61 seconds os equal to 1 minute and 1 second. // 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 of pad when it is called for the last time is '01' because the pad function adds a '0' to the left of the num to make it 2 characters long. As the num is 1, the return value is '01'. \ No newline at end of file