diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..ee5c6d5c5f 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 +// =============> the function is not going to work because a variable is declared twice (str) // 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 new code here +// =============> the error message 'Identifier 'str' has already been declared', diplays because the toUpperCase function does not change the value of the varible declared +// but it creates a new one. +// =============> my new code: +function capitalise(str) { + let result = `${str[0].toUpperCase()}${str.slice(1)}`; + return resault; +} diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..d842a46ef3 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 +// =============> because first: the variable decimalNumber is declared twice +// second: the parameter decimalNumber is assigned to a value inside the function // Try playing computer with the example to work out what is going on @@ -14,7 +15,13 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +// =============> Identifier 'decimalNumber' has already been declared is the error msg thrown by the code // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> +function convertToPercentage(decimalNumber) { + // const decimalNumber = 0.5; this line must be deleted + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..ebf9ca54c2 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,20 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> identifier not declared (unknown) function square(3) { return num * num; } -// =============> write the error message here +// =============> Unexpected number -// =============> explain this error message here +// =============> the parameter shouldn't be given an argument in the function definition // Finally, correct the code to fix the problem -// =============> write your new code here - - +// =============> my new code +function square(num) { + return num * num; +} +console.log(square(3)); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..44f3ab69d0 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 +// =============> the function when called will throw an error function multiply(a, b) { console.log(a * b); @@ -8,7 +8,12 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here - +// =============> the function multiply has no return statement +// when we call the function multiply(10, 32) it will throw this message: The result of multiplying 10 and 32 is undefined // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> +function multiply(a, b) { + return a * b; // we change the console.log statement with the return statement +} + +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 37cedfbcfd..90c8f9c1e5 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// =============> The sum of 10 and 32 is undefined function sum(a, b) { return; @@ -8,6 +8,12 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> the function sum returns Nul because it's straight followed by semicolon thate ends the statement +// also the statement a + b is in new line // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> my new code +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 57d3f5dc35..9c483c7d54 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,10 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> +// The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 3 const num = 103; @@ -14,11 +17,21 @@ 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 variable num is assigned to the value 103 in the global scope and not assigned to any value in the function scope // Finally, correct the code to fix the problem // =============> write your new code here +function getLastDigit(num) { + // we add the identifier num as parameter to the function getLastDigit + 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 diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..60b08f5c2c 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -3,7 +3,7 @@ // The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared. // For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by: - +// // squaring your height: 1.73 x 1.73 = 2.99 // dividing 70 by 2.99 = 23.41 // Your result will be displayed to 1 decimal place, for example 23.4. @@ -15,5 +15,6 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file + return Number((weight / (height * height)).toFixed(1)); // return the BMI of someone based off their weight and height +} +console.log(calculateBMI(90, 1.77)); // when calling this function with given someone's weight and height diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..570e7bdf71 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 upperSnakeCase(wordsSet) { + let wsSnakeCase = wordsSet.replaceAll(" ", "_"); + let wsupperSnakeCase = wsSnakeCase.toUpperCase(); + return wsupperSnakeCase; +} +upperSnakeCase("i do not know"); // evaluates to: "I_DO_NOT_KNOW" diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..a17621653d 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 console.log(`£${pounds}.${pence}`); +} +toPounds("399p"); // £3.99 +toPounds("1095p"); // £10.95 diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8c..bf8c94ba29 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,27 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// =============> pad will be called 3 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 +// =============> the value assigned to num 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 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 +// =============> the value assigned to num is 1 +// because the last time pad is called for the remainingSeconds holding the value 1 +// return pad(remainingSeconds) +// function pad(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 +// =============> the return value of pad is called for the last time is "01" +// because the argument passed to the pad parameter is the value of the remainingSeconds wich is 1 +// function pad(1){ +// return 1.toString().padStart(2, "0"); +// return "1".pad(2,"0"); +// return "01" +//}