From 4d85df1823df871b1a336590b72052e7dbe4c4b9 Mon Sep 17 00:00:00 2001 From: John Robinson Date: Sat, 11 Jul 2026 14:07:26 +0100 Subject: [PATCH] submission Sprint-2 work --- Sprint-2/1-key-errors/0.js | 10 +++--- Sprint-2/1-key-errors/1.js | 15 ++++++--- Sprint-2/1-key-errors/2.js | 12 +++---- Sprint-2/2-mandatory-debug/0.js | 10 ++++-- Sprint-2/2-mandatory-debug/1.js | 13 +++++--- Sprint-2/2-mandatory-debug/2.js | 18 ++++++++--- Sprint-2/3-mandatory-implement/1-bmi.js | 9 ++++-- Sprint-2/3-mandatory-implement/2-cases.js | 8 +++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 24 ++++++++++++++ Sprint-2/4-mandatory-interpret/time-format.js | 10 +++--- Sprint-2/5-stretch-extend/format-time.js | 31 +++++++++++++++++-- 11 files changed, 124 insertions(+), 36 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..9d6fe600fb 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,13 @@ // Predict and explain first... -// =============> write your prediction here +// A) Error message - str has already been declared // 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; + let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedStr; } -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("hello there")); + diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..19d5def6f9 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,25 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> Decimal number is declared twice in the same scope // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { +/*function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); +console.log(decimalNumber);*/ -// =============> write your explanation here +// =============>Decimal number is declared twice & also set as a parameter which causes the syntaxError. // 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)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..7a12310afc 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,18 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// The input to the function is a number, but the function is trying to use that number as a variable name. function square(3) { return num * num; } -// =============> write the error message here +// =============> /SyntaxError: Unexpected number -// =============> explain this error message here +// =============> // Function parameter '3' is a literal value, not a parameter. Javascript expects a text name for the parameter. // Finally, correct the code to fix the problem -// =============> write your new code here - - + function square(num) { + return num * num; +} diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..59ebe7684b 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 +// =============> Error because console.log is within the multiply function does not return a value function multiply(a, b) { console.log(a * b); @@ -8,7 +8,13 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> The console.log inside the function multiply is read first & prints the result of multiplying 10 and 32 - 320, to the console. +// But the aim of the function which was tryin to return it in the template literals is left as undefined because the multiply function does not return a value +// // Finally, correct the code to fix the problem // =============> write your new code here + +function multiply(a, b) { + return a * b; +} \ 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 37cedfbcfd..bfb422fb1e 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,6 @@ // Predict and explain first... -// =============> write your prediction here - +// =============> The function won't return the sum of a and b because the return is between the +// return statement and the expression a + b. function sum(a, b) { return; a + b; @@ -8,6 +8,11 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> returns undefined because the empty return tells the function to return nothing, so the function returns 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)}`); \ 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 57d3f5dc35..1381dfce56 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 +// in the const num = 103; and not the parameter passed to the function getLastDigit. const num = 103; @@ -14,11 +14,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 +// =============> as explained above, the const num = 103; is being used in the function getLastDigit instead of the parameter passed to the function. + // Finally, correct the code to fix the problem // =============> write your new code here // This program should tell the user the last digit of each number. -// Explain why getLastDigit is not working properly - correct the problem +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)}`); diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..1fd01faef7 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -14,6 +14,11 @@ // Then when we call this function with the weight and height // 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 heightSquared = height * height; + const bmi = weight / heightSquared; + return 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 5b0ef77ad9..c88b31e329 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,11 @@ // 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) { + const upperCaseString = inputString.toUpperCase(); + const upperSnakeCaseString = upperCaseString.replace(/ /g, "_"); + return upperSnakeCaseString; +} + +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 6265a1a703..83948023a0 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(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("1p")); +console.log(toPounds("90p")); +console.log(toPounds("303p")); +console.log(toPounds("23456p")); \ 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 17127bc01e..7547270e28 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -21,18 +21,18 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> 3 // 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 +// =============> 1, the modulo % operator means that 61 gives a remainder of 1 // 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 +// =============> 01, this is so the 1 is padded with a leading 0 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 32a32e66b8..4cefa2ae59 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,12 +2,24 @@ // Make sure to do the prep before you do the coursework // 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. +const pad = (num) => String(num).padStart(2, "0"); + function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + const minutes = time.slice(3, 5); + + if (hours === 0) { + return `12:${minutes} am`; + } + else if (hours > 12) { + return `${pad(hours - 12)}:${minutes} pm`; + } + else if (hours === 12) { + return `${hours}:${minutes} pm`; + } + else { + return `${pad(hours)}:${minutes} am`; } - return `${time} am`; } const currentOutput = formatAs12HourClock("08:00"); @@ -23,3 +35,16 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); +const currentOutput3 = formatAs12HourClock("13:15"); +const targetOutput3 = "01:15 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("12:00"); +const targetOutput4 = "12:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); \ No newline at end of file