diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..2dc3fb7ff 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,23 @@ // Predict and explain first... // =============> write your prediction here - +//The variable str has been declared twice the first one as the function +// and the second time inside the function => let str = `${str[0].toUpperCase()}${str.slice(1)}`; // 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; -} +} */ // =============> write your explanation here +// An error occured => SyntaxError: Identifier 'str' has already been declared +//because in javascript we cant declare a variable more than once within scope . + // =============> write your new code here +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; + } + + console.log(capitalise("cool")) \ 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..1fdd4293b 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,9 +2,10 @@ // Why will an error occur when this program runs? // =============> write your prediction here - +//// The variable decimalNmber has been declared twice once as a parameter for convertToPercentage +// function and the second time inside the function // 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 +13,22 @@ function convertToPercentage(decimalNumber) { return percentage; } -console.log(decimalNumber); +console.log(decimalNumber); */ // =============> write your explanation here +//The function convertToPercentage(decimalNumber) accepts a parameter decimalNumber +//Inside the function, ywe have decimalNumber redeclared using const decimalNumber = 0.5;, which creates a conflict +// Finally, correct the code to fix the problem +//we should remove the redeclaration of decimalNumber inside the function. Instead, use the parameter directly.and then +//pass a decimal number as input => 0.5 in the function call // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} + +const result = convertToPercentage(0.5); +console.log(result); // Output => "50%" \ 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..fb220e8d2 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,25 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// The number three was passed straight to the function witch is why it's throwing an error -function square(3) { +/* function square(3) { return num * num; } - + */ // =============> write the error message here +// Error message => SyntaxError: Unexpected number // =============> explain this error message here +//The function was expecting a parameter and got a number instead and a number +// can not be a valid parameter name +//We pass 3 as an argument when calling square(3), and the function returns 9 (since 3 squared is 9). // 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..8b6369549 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,23 @@ // Predict and explain first... // =============> write your prediction here +//In this function console.log() is used instead of return -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 - +//By using console.log() this multiply function dose not return anything and it just printing the output +//of => a * b to the console + undefined => since console.log does not return // Finally, correct the code to fix the problem +// 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..4dae5316b 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 +//Here we have return; and => a + b ; in seprate lines closed with semicolon. -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 +//AS we know that return end and stops the execution of the function witch mean that => a + b +//will not be executed and line 8 is useless since the function stops at line 7. + // 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..50cdb500b 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,9 +1,12 @@ // Predict and explain first... +// The constant variable num is set to = 103 ,and num should be passed as a parameter to getLastDigit function // Predict the output of the following code: // =============> Write your prediction here +/* The last digit will be the same for all values that will be passed to it +since it coming from the constant.*/ -const num = 103; +/* const num = 103; function getLastDigit() { return num.toString().slice(-1); @@ -12,13 +15,33 @@ 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)}`); - + */ // 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 is the same because it coming from the execution of => num.toString().slice(-1); +//=> 3 over and over again + // 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 +// To correct the code we needed to get rid of => const num = 103 +//and pass num as a parameter to the function and now it giving the proper output +/* The last digit of 42 is 2 +The last digit of 105 is 5 +The last digit of 806 is 6 */ \ 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..dc6bd3636 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,11 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + let squared = height * height; + let bmi = weight/squared ; + let bmiRounded = Math.round(bmi * 10) / 10; + return bmiRounded ; // return the BMI of someone based off their weight and height -} \ No newline at end of file +} + +console.log(calculateBMI(70,1.73)) \ 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..922b1af73 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,10 @@ // 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.replace(/\s+/g, '_').toUpperCase(); + } + + 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..8ea7409ec 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,25 @@ // 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(toPounds("399p")) \ 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..42a572c00 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -10,6 +10,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 @@ -18,17 +19,26 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +/* The pad function is called three times inside the formatTimeDisplay function: +Once for totalHours => to pad the hour value. +Once for remainingMinutes => to pad the minutes value. +Once for remainingSeconds =>to pad the seconds value. */ // 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 +//The last time pad is called is for remainingSeconds, which is calculated as remainingSeconds = 61 % 60 = 1 when formatTimeDisplay(61) is called. // 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 last call to pad is for remainingSeconds, which is 1. The padStart(2, "0") will pad this number to make it two digits. +//And return value =>"01" \ No newline at end of file