diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..c6c7dffed 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -10,4 +10,17 @@ function capitalise(str) { } // =============> write your explanation here +//The error occurs because the variable "str" is being +//declared twice in the same scope. The parameter "str" +//is already defined as a function argument, and then it +//is being redeclared with "let str =". This causes +//a syntax error because we cannot declare a variable +//with the same name in the same scope. To fix this, we +//can either rename the parameter or the variable inside +//the function. + // =============> write your new code here +function capitalise(str) { + let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedStr; +} diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..a14d08fde 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -2,7 +2,13 @@ // Why will an error occur when this program runs? // =============> write your prediction here - + //An error will occur because the variable "decimalNumber" + //is being declared twice in the same scope. + //The parameter "decimalNumber" is already defined as a + //function argument, and then it is being redeclared + //with "const decimalNumber = 0.5;". This causes a + //syntax error because we cannot declare a variable + //with the same name in the same scope. // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -16,5 +22,20 @@ console.log(decimalNumber); // =============> write your explanation here +//The error occurs because the variable "decimalNumber" is being +//declared twice in the same scope. The parameter "decimalNumber" +//is already defined as a function argument, and then it is being +//redeclared with "const decimalNumber = 0.5;". This causes a +//syntax error because we cannot declare a variable with the +//same name in the same scope. To fix this, we can either rename +//the parameter or the variable inside the function. + // 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-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..d815abfff 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -4,17 +4,24 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// I predict that there will be a syntax error because the parameter name "3" is not a valid +// identifier. function square(3) { return num * num; } // =============> write the error message here +// SyntaxError: Function argument name must be a valid identifier // =============> explain this error message here +// The error occurs because "3" is not a valid identifier for a function parameter. +// In JavaScript, parameter names must follow the rules for identifiers, which means they +// cannot be numbers or reserved keywords. // Finally, correct the code to fix the problem // =============> write your new code here - - +function square(num) { + return num * num; +} \ No newline at end of file diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..661d6e2ed 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,6 +1,9 @@ // Predict and explain first... // =============> write your prediction here +// I predict that the output of the console.log statement will be "The result of multiplying +// 10 and 32 is undefined" because the multiply function does not return a value, it only +// logs the result to the console. function multiply(a, b) { console.log(a * b); @@ -9,6 +12,15 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// The multiply function takes two parameters, a and b, and logs the product of a and b to +// the console. However, it does not return any value. When the function is called inside +// the template literal, it returns undefined, which is why the output of the console.log +// statement includes "undefined". // 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-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..69247bf99 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,5 +1,8 @@ // Predict and explain first... // =============> write your prediction here +// I predict that the output of the console.log statement will be "The sum of 10 and 32 +// is undefined" because the sum function does not return a value, it only has a return +// statement without any value. function sum(a, b) { return; @@ -9,5 +12,16 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here + +// The sum function takes two parameters, a and b, and has a return statement without +// any value. This means that when the function is called, it will return undefined. +// Therefore, when the function is called inside the template literal, it returns +// undefined, which is why the output of the console.log statement includes "undefined". + // 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-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..04974992d 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -3,6 +3,10 @@ // Predict the output of the following code: // =============> Write your prediction here +// I predict that the output of the console.log statements will be "The last digit of 42 is 3", +// "The last digit of 105 is 3", and "The last digit of 806 is 3" because the getLastDigit +// function always returns the last digit of the number 103, which is 3. + const num = 103; function getLastDigit() { @@ -15,10 +19,25 @@ 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 getLastDigit function is not working properly because it always returns the last +// digit of the number 103, regardless of the input. This is because the function does not +// take any parameters and uses a global variable. + // Finally, correct the code to fix the problem // =============> write your new code here +function getLastDigit(num) { + 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 +// The getLastDigit function is not working properly because it does not take any parameters +// and uses a global variable. To fix the problem, we need to add a parameter to the function +// so that it can take in a number and return the last digit of that number. \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..3258039dc 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -16,4 +16,8 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height + const bmi = weight / (height * height); + return bmi.toFixed(1); } + +console.log(calculateBMI(70, 1.73)); // Output: "23.4" diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..39f88b1b2 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,17 @@ // 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(inputString) { + // Split the input string into words using space as the delimiter + const words = inputString.split(' '); + + // Join the words with underscores and convert to uppercase + const upperSnakeCaseString = words.join('_').toUpperCase(); + + return upperSnakeCaseString; +} + + +console.log(toUpperSnakeCase("hello there")); // Output: "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // Output: "LORD_OF_THE_RINGS" diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..ef221f713 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -1,6 +1,53 @@ // In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js // You will need to take this code and turn it into a reusable block of code. + +const penceString = "399p"; + +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"); + +console.log(`£${pounds}.${pence}`); + + // You will need to declare a function called toPounds with an appropriately named parameter. +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}`; +} + // You should call this function a number of times to check it works for different inputs + +console.log(toPounds("399p")); // Output: £3.99 +console.log(toPounds("5p")); // Output: £0.05 +console.log(toPounds("1234p")); // Output: £12.34 +console.log(toPounds("0p")); // Output: £0.00 + + diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..e41e7bbb8 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -23,16 +23,31 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +//formatTimeDisplay will call pad three times, once for totalHours, once for +// remainingMinutes, and once for remainingSeconds. + // 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 when pad is called for the first time is 0, which is the value +// of totalHours when the input is 61 seconds. // c) What is the return value of pad when it 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", which is the string +// representation of totalHours padded with a leading zero. + // 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 when pad is called for the last time in this program is 1, +// which is the value of remainingSeconds when the input is 61 seconds. This is because 61 +// seconds has 1 second remaining after calculating the total minutes and hours. // e) What is the return value of pad when it 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 in this program is "01", +// which is the string representation of remainingSeconds padded with a leading zero. +// This is because the value of remainingSeconds is 1, and the pad function adds a leading +// zero to make it a two-digit string.