diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..b46655823 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 - +//Should return error as str was already declared? // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +10,11 @@ function capitalise(str) { } // =============> write your explanation here +//The parameter name str is already declared as a variable // =============> write your new code here +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} + +console.log(capitalise("string")); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..fcf59105a 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,7 +2,7 @@ // Why will an error occur when this program runs? // =============> write your prediction here - +//2 errors: 1. The parameter decimalNumber is already declared 2. it should show error or undefined as console.log is calling variable not the function // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -15,6 +15,13 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here - +// Identifier 'decimalNumber' has already been declared - // 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..65cd53912 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,20 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here - -function square(3) { - return num * num; -} +//we can't declare number as a variable +// function square(3) { +// return num * num; +// } // =============> write the error message here - +//Uncaught SyntaxError SyntaxError: Unexpected number // =============> explain this error message here - +//we can't declare number as a variable ? // Finally, correct the code to fix the problem - +function square(num) { + return num * num; +} +console.log(square(2)) // =============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..92041895b 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 +// =============> write your prediction here - It will print 320 inside the function, but the template string will show "undefined" because multiply does not return a value. function multiply(a, b) { console.log(a * b); @@ -8,7 +8,11 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here - multiply() uses console.log to display the result, but it doesn't return anything. In JavaScript, a function with no return statement returns undefined, so ${multiply(10, 32)} becomes undefined even though 320 was logged earlier. // Finally, correct the code to fix the problem // =============> write your new code here +function multiplyFixed(a, b) { + return a * b; +} +console.log(`The result of multiplying 10 and 32 is ${multiplyFixed(10, 32)}`); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..30b260162 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,5 @@ // Predict and explain first... -// =============> write your prediction here - +// =============> write your prediction here - should show undefined as there are ";" after return inside the function function sum(a, b) { return; a + b; @@ -10,4 +9,9 @@ console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> write your new code here - you can't divide the return parameters with ";" +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..3084ea0ac 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,7 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> Write your prediction here - it should print last digit of 103 const num = 103; @@ -14,11 +14,22 @@ 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 +// =============> write the output here - we have set num as constant value 103, inside the function we use it as it is constant, ignoring other inputs +// 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 +// =============> write your explanation here - // 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 +// It wasn't working because it used the outer variable num instead of the input value diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..1aa6b12c2 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,8 @@ // 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 + const bmi = weight / (height * height); + return Number(bmi.toFixed(1)); +} + +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..9c8008ce5 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 upperSnake(string) +{ + return string.trim().split(" ").join("_").toUpperCase() +} +console.log(upperSnake("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..4ddab3e33 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,27 @@ // 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(str) { + // 1. const penceString = "399p": initialises a string variable with the value "399p" + const penceStringWithoutTrailingP = str.substring(0, str.length - 1); + + //2. removes "P" from the string + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + //3. Ensures the string is at least 3 characters long by adding 0 to the start + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); + + //4. Extracts everything except the last 2 digits of paddedPenceNumberString + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + return pence; +} +//5. takes the last 2 digits as the pence from paddedPenceNumberStringCollapse comment +console.log(toPounds("399p")); +console.log(toPounds("400p")); +console.log(toPounds("301p")); +console.log(toPounds("302p")); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..271635131 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -16,19 +16,19 @@ function formatTimeDisplay(seconds) { // Questions -// a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// a) When formatTimeDisplay is called how many times will pad be called? +// =============> write your answer here - 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 +// b) What is the value assigned to num when pad is called for the first time? - +// =============> write your answer here - First call is pad(totalHours) // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> write your answer here - it returs 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 +// =============> write your answer here - Last call is pad(remainingSeconds) For 61, remainingSeconds = 61 % 60 = 1, so num is 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 +// =============> write your answer here - With num = 1, pad(1) returns "01". "1".padStart(2, "0") adds zero to make it 2 characters long. diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..a21018373 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -3,23 +3,30 @@ // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + let ampm = "am"; + let newHours = hours; + + if (hours === 0) { + newHours = 12; // midnight + } else if (hours === 12) { + ampm = "pm"; // noon + } else if (hours > 12) { + newHours = hours - 12; + ampm = "pm"; } - return `${time} am`; + + const showHours = newHours.toString().padStart(2, "0"); + return `${showHours}:${minutes} ${ampm}`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); +// Now let's test different times -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; -console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` -); +console.log(formatAs12HourClock("00:00")); +console.log(formatAs12HourClock("00:01")); +console.log(formatAs12HourClock("08:00")); +console.log(formatAs12HourClock("11:59")); +console.log(formatAs12HourClock("12:00")); +console.log(formatAs12HourClock("13:45")); +console.log(formatAs12HourClock("23:14"));