diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..76b8a3d9a 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,29 @@ // Predict and explain first... // =============> write your prediction here +// the code will throw an error because the variable str is being declared twice within the same scope // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +/* function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } -// =============> write your explanation here -// =============> write your new code here +*/ + +// =============> write your explanation +/* the code throws a SyntaxError because the variable 'str' is declared twice in the same scope using 'let'. In JavaScript, + you cannot declare a variable with the same name more than once in the same scope. The first declaration of 'str' is as a + function parameter, and the second declaration is within the function body. To fix this, you can either rename the second + 'str' variable or remove the 'let' keyword from the second declaration to reassign the value to the existing parameter variable.*/ + + // =============> write your new code here + +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} + +console.log(capitalise("hello")); // Output: "Hello" diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..f89eb25e3 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,9 +2,12 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// the code will throw an error because the variable str is being declared twice within the same scope // Try playing computer with the example to work out what is going on +/* + function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -14,7 +17,23 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); +*/ + // =============> write your explanation here +/* The error occurs because the variable 'decimalNumber' is declared twice within the same function scope using 'const'. +This is not allowed in JavaScript, as each variable must have a unique name within its scope. When the function is called, +the JavaScript engine encounters the second declaration of 'decimalNumber' and throws a SyntaxError indicating that the +identifier has already been declared.*/ // Finally, correct the code to fix the problem // =============> write your new code here + +let decimalNumber = 0.6; + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +console.log(convertToPercentage(decimalNumber)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..0cc11d7b0 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,37 @@ // 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 the code will throw a SyntaxError because the parameter '3' is not a valid identifier. +Function parameters must be valid variable names.*/ + +/* function square(3) { return num * num; } +*/ + // =============> write the error message here +/* Process exited with code 1 +Uncaught SyntaxError /home/ads/Desktop/CYF-SEP/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/2.js:10 +function square(3) { + ^ + +SyntaxError: Unexpected number */ // =============> explain this error message here +/* The error message indicates a SyntaxError due to an "Unexpected number". This occurs because the function +parameter '3' is not a valid identifier. In JavaScript, function parameters must be valid variable names, and +using a number as a parameter name is not allowed. The JavaScript engine expects a valid identifier after the +opening parenthesis of the function declaration, but it encounters a number instead, leading to the syntax error.*/ // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} +console.log(square(4)); // Output: 16 diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..05b512db9 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,26 @@ // Predict and explain first... // =============> write your prediction here +/* The function multiply does not return any value, it only logs the product of a and b to the console. +Therefore, when we try to log the result of multiply(10, 32), it will log 'undefined' because the +function does not return anything.*/ +/* function multiply(a, b) { console.log(a * b); } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +*/ // =============> write your explanation here +// The function multiply does not have a return statement, so it returns undefined by default. // 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..d8a25d274 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,27 @@ // Predict and explain first... // =============> write your prediction here +// An error will occur because the function 'sum' has a return statement that is not returning any value. +/* function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +*/ // =============> write your explanation here +/* The error occurs because the 'sum' function has a return statement that does not return any value. In JavaScript, +when a function reaches a return statement without a value, it returns 'undefined' by default. Therefore, when we +call 'sum(10, 32)', it returns 'undefined', and the output will be "The sum of 10 and 32 is undefined". To fix this, +we need to ensure that the function returns the result of 'a + b'.*/ + // 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..ab75db690 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,7 +2,10 @@ // Predict the output of the following code: // =============> Write your prediction here +/* The output 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 function getLastDigit always returns the last digit of the constant number 'num', which is set to 103.*/ +/* const num = 103; function getLastDigit() { @@ -12,13 +15,34 @@ 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 -// Finally, correct the code to fix the problem -// =============> write your new code here +/* The output is the way it is because the function getLastDigit does not use its parameter. Instead, it always +returns the last digit of the constant variable 'num', which is set to 103. Therefore, regardless of the input +value provided when calling getLastDigit, it will always return '3', the last digit of 103. To fix this, we need +to modify the function to accept a parameter and return the last digit of that parameter.*/ // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem +// the return function is not using the parameter passed to it and is instead using a constant value + + +// Finally, correct the code to fix the problem +// =============> write your new code here + +function getLastDigit(number) { + return number.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)}`); + diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..1323b92d7 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -14,6 +14,13 @@ // Then when we call this function with the weight and height // It should return their Body Mass Index to 1 decimal place +let weight = 80; // in kg +let height = 1.73; // in metres + function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file + let bmi = weight / (height * height); + return bmi.toFixed(1); +} + +console.log(calculateBMI(weight, height)); // should return 26.7 \ 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..dd0d8c8d5 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,14 @@ // 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 + +const input1 = "hello there"; +const input2 = "lord of the rings"; + +function toUpperSnakeCase(str) { + // return the string in UPPER_SNAKE_CASE + return str.replace(/ /g, "_").toUpperCase(); // Replace spaces with underscores and convert to uppercase +} + +console.log(toUpperSnakeCase(input1)); // should return "HELLO_THERE" +console.log(toUpperSnakeCase(input2)); // should return "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..6dab5bdc3 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,28 @@ // 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 // to remove the trailing 'p' +); + +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 // all but the last two characters +); + +const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) // the last two characters + .padEnd(2, "0"); // in case there is only one character of pence + + return `£${pounds}.${pence}`; +} + +console.log(toPounds("399p")); // should log "£3.99" +console.log(toPounds("5p")); // should log "£0.05" +console.log(toPounds("89p")); // should log "£0.89" +console.log(toPounds("123456p")); // should log "£1234.56" \ 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..8017e1c60 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,24 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// pad is called 3 times because there are 3 values to be padded - hours, minutes and seconds // Call formatTimeDisplay with an input of 61, now answer the following: +console.log(formatTimeDisplay(61)); // Should return "00:01:01" // b) What is the value assigned to num when pad is called for the first time? // =============> write your answer here +// num is assigned the value of totalHours which is 0 because 61 seconds is less than a minute // c) What is the return value of pad is called for the first time? // =============> write your answer here +// The return value is "00" because the padStart method adds a "0" to the start of the string "0" to make it 2 characters long // 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 +// num is assigned the value of remainingSeconds which is 1 because 61 seconds is 1 second more than a full minute (60 seconds) // 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 is "01" because the padStart method adds a "0" to the start of the string "1" to make it 2 characters long +