From 1eb4e07fe265f7ef02dd224503f81371dceebece Mon Sep 17 00:00:00 2001 From: Abayie Date: Sat, 21 Jun 2025 07:26:24 +0100 Subject: [PATCH 01/15] prep --- prep/example.js | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 prep/example.js diff --git a/prep/example.js b/prep/example.js new file mode 100644 index 000000000..f3bfd7449 --- /dev/null +++ b/prep/example.js @@ -0,0 +1,5 @@ +// function decimalTopercent(num){ +// let percent = num * 100; +// return percent +// } +// console.log(decimalTopercent(0.5) + "%"); From bc143cef416bae1b5539f4a7f7d31147aefb236f Mon Sep 17 00:00:00 2001 From: Abayie Date: Sat, 21 Jun 2025 07:41:16 +0100 Subject: [PATCH 02/15] prep-files --- prep/example.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/prep/example.js b/prep/example.js index f3bfd7449..eddee5ebe 100644 --- a/prep/example.js +++ b/prep/example.js @@ -3,3 +3,12 @@ // return percent // } // console.log(decimalTopercent(0.5) + "%"); + +const decimalNumber = 0.5; + +function convertToPercentage() { + const percentage = `${decimalNumber * 100}%`; +} + +convertToPercentage(0.5); +console.log(percentage); \ No newline at end of file From 5cb7a479613203db1bbf200eed04aed40653d8b4 Mon Sep 17 00:00:00 2001 From: Abayie Date: Sat, 21 Jun 2025 08:02:03 +0100 Subject: [PATCH 03/15] functions --- prep/example.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/prep/example.js b/prep/example.js index eddee5ebe..5fb4293a2 100644 --- a/prep/example.js +++ b/prep/example.js @@ -4,11 +4,12 @@ // } // console.log(decimalTopercent(0.5) + "%"); -const decimalNumber = 0.5; - -function convertToPercentage() { +function convertToPercentage(decimalNumber) { const percentage = `${decimalNumber * 100}%`; + return percentage; } -convertToPercentage(0.5); -console.log(percentage); \ No newline at end of file +const result = convertToPercentage(0.5); +const result1 = convertToPercentage(0.231); +console.log(result); +console.log(result1); From b383e53396f1dc9a1c4f3751555c72e2dfa42299 Mon Sep 17 00:00:00 2001 From: Abayie Date: Sun, 29 Jun 2025 23:31:25 +0100 Subject: [PATCH 04/15] 0.js --- Sprint-2/1-key-errors/0.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..b7104a962 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,8 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here => This code will throw a syntax error +// because the parameter str is redeclared as a let variable inside the function, which is not allowed. + + // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -9,5 +12,12 @@ function capitalise(str) { return str; } -// =============> write your explanation here +// =============> write your explanation here => The error occurs because you cannot declare +// a new variable with the same name as a function parameter (str) using let inside the function. +// This causes a "Identifier 'str' has already been declared" error. + + // =============> write your new code here +function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; +} \ No newline at end of file From ea7eebf164f4b0141781be0466aa6eda50698759 Mon Sep 17 00:00:00 2001 From: Abayie Date: Thu, 17 Jul 2025 00:24:43 +0100 Subject: [PATCH 05/15] checking --- Sprint-2/1-key-errors/0.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index b7104a962..94fecb35f 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -7,17 +7,19 @@ // 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; -} +// function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +// } -// =============> write your explanation here => The error occurs because you cannot declare +// =============> write your explanation here => The error occurs because we cannot declare // a new variable with the same name as a function parameter (str) using let inside the function. -// This causes a "Identifier 'str' has already been declared" error. +// This causes a "Identifier 'str' has already been declared" error according to mdn. // =============> write your new code here function capitalise(str) { return `${str[0].toUpperCase()}${str.slice(1)}`; -} \ No newline at end of file +} + +console.log(capitalise("bethan")); \ No newline at end of file From d8487ec2c95b52c863d82ceb14c828dbd5b4e30d Mon Sep 17 00:00:00 2001 From: Abayie Date: Thu, 17 Jul 2025 00:30:23 +0100 Subject: [PATCH 06/15] 1.js --- Sprint-2/1-key-errors/1.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..44b685134 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,19 +2,32 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// An error will occur because 'decimalNumber' is redeclared inside the function using 'const', +// which is not allowed. Also, 'decimalNumber' is not defined in the global scope, +// so console.log(decimalNumber) will throw a ReferenceError. // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; - return percentage; -} +// return percentage; +// } -console.log(decimalNumber); +// console.log(decimalNumber); // =============> write your explanation here +// The error occurs because we cannot redeclare the parameter 'decimalNumber' inside the function +// using 'const'. Also, 'decimalNumber' is not defined outside the function, so +// console.log(decimalNumber) will throw a ReferenceError. // 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.9)); // Output: 90% \ No newline at end of file From fcb215c4f86f0f38f81c5b4f3cc39558970c21ea Mon Sep 17 00:00:00 2001 From: Abayie Date: Thu, 17 Jul 2025 00:38:32 +0100 Subject: [PATCH 07/15] 2.js --- Sprint-2/1-key-errors/2.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..e16e72f0e 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/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 +// The code will throw a syntax error because you cannot use a number as a function parameter. + +// function square(3) { +// return num * num; +// } -function square(3) { - return num * num; -} // =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here +// Function parameters must be variable names, not values. Using a number as a parameter is invalid syntax in JavaScript -mdn // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} - +console.log(square(7)); From db6578c9c06aeb0bb33e8cc6bde7031d94f95db8 Mon Sep 17 00:00:00 2001 From: Abayie Date: Thu, 17 Jul 2025 00:46:36 +0100 Subject: [PATCH 08/15] 0.js --- Sprint-2/2-mandatory-debug/0.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..4ca323dd4 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,24 @@ -// Predict and explain first... - // =============> write your prediction here +// function multiply(a, b) { +// console.log(a * b); +// } -function multiply(a, b) { - console.log(a * b); -} +// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +// The output will be: +// 320 +// The result of multiplying 10 and 32 is undefined +// This is because multiply logs the result but does not return it, so the template literal gets undefined. // =============> write your explanation here +// The function multiply only prints the result to the console and does not return anything. +// When we use multiply(10, 32) inside the template literal, it evaluates to undefined because the function has no return value. + // 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)}`); \ No newline at end of file From 7229e509f60cc36d257828c14069b170443aaea6 Mon Sep 17 00:00:00 2001 From: Abayie Date: Thu, 17 Jul 2025 00:51:28 +0100 Subject: [PATCH 09/15] 1.js --- Sprint-2/2-mandatory-debug/1.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..08a9f484f 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,21 @@ // Predict and explain first... // =============> write your prediction here +// function sum(a, b) { +// return; +// a + b; +// } -function sum(a, b) { - return; - a + b; -} - -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +// The output will be: The sum of 10 and 32 is undefined +// This is because the function returns immediately with no value, so a + b is never executed. // =============> write your explanation here +// The return statement ends the function before a + b is calculated, 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 From ba0a3a81329a0fd4f36282918dfed28f7f517b37 Mon Sep 17 00:00:00 2001 From: Abayie Date: Thu, 17 Jul 2025 00:57:29 +0100 Subject: [PATCH 10/15] 2.js --- Sprint-2/2-mandatory-debug/2.js | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..dd665d537 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,23 +2,37 @@ // 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 +// The last digit of 806 is 3 +// Because getLastDigit always uses the global variable num (103) and ignores the argument. -const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +// const num = 103; -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)}`); +// 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)}`); -// Now run the code and compare the output to your prediction +// // Now run the code and compare the output to your prediction // =============> write the output here // Explain why the output is the way it is // =============> write your explanation here +// The function getLastDigit does not accept any parameters and always uses the global variable num (103). +// So, regardless of the argument passed, it always returns the last digit of 103, which is "3". + // 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 From fe53ddf996efbc750dcdcf1882c75817dd1cacd0 Mon Sep 17 00:00:00 2001 From: Abayie Date: Thu, 17 Jul 2025 01:14:03 +0100 Subject: [PATCH 11/15] 1-bmi.js --- Sprint-2/3-mandatory-implement/1-bmi.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..81f2b1c05 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,10 @@ // 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 + // Calculate BMI: weight divided by height squared + const bmi = weight / (Math.pow(height, 2)); + // Return BMI rounded to 1 decimal place + return +(bmi.toFixed(1)); +} + +console.log(calculateBMI(78, 1.72)); \ No newline at end of file From 09b1e52be84c28a5ef78cb2c0f20434885dc0b6d Mon Sep 17 00:00:00 2001 From: Abayie Date: Thu, 17 Jul 2025 01:24:11 +0100 Subject: [PATCH 12/15] 3-to-pounds.js --- Sprint-2/3-mandatory-implement/2-cases.js | 9 +++++++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..51b6cb3ba 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,12 @@ // 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(str) { + // Replace spaces with underscores and convert to uppercase + return str.replace(/ /g, "_").toUpperCase(); +} + + +console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS" diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..3a2a318c9 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,20 @@ // 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) { + // Remove trailing 'p' and pad with zeros to ensure at least 3 digits + const pence = penceString.substring(0, penceString.length - 1).padStart(3, "0"); + // Get pounds and pence parts + const pounds = pence.substring(0, pence.length - 2); + const pencePart = pence.substring(pence.length - 2).padEnd(2, "0"); + // Return formatted string + return `£${pounds}.${pencePart}`; +} + + +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("9p")); // £0.09 +console.log(toPounds("99p")); // £0.99 +console.log(toPounds("100p")); // £1.00 +console.log(toPounds("1234p")); // £12.34 From 75983a9a8141c6d9a82a3040d24656d2bb68a570 Mon Sep 17 00:00:00 2001 From: Abayie Date: Thu, 17 Jul 2025 01:28:05 +0100 Subject: [PATCH 13/15] time-format.js --- Sprint-2/4-mandatory-interpret/time-format.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..1350f7cf5 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 will be called 3 times (once for hours, once for minutes, once for seconds). // 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 first call is pad(totalHours). For input 61, totalHours is 0. + // c) What is the return value of pad is called for the first time? // =============> write your answer here +// pad(0) returns "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 last call is pad(remainingSeconds). For input 61, remainingSeconds 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 +// pad(1) returns "01". From 42f5fa5b69f8f755b55602a9f74eabcb995d529a Mon Sep 17 00:00:00 2001 From: Abayie Date: Thu, 17 Jul 2025 01:36:47 +0100 Subject: [PATCH 14/15] format-time.js --- Sprint-2/5-stretch-extend/format-time.js | 64 ++++++++++++++++++------ 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..296d77950 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,24 +2,58 @@ // 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. +// function formatAs12HourClock(time) { +// const hours = Number(time.slice(0, 2)); +// if (hours > 12) { +// return `${hours - 12}:00 pm`; +// } +// return `${time} am`; +// } + +// const currentOutput = formatAs12HourClock("08:00"); +// const targetOutput = "08:00 am"; +// console.assert( +// currentOutput === targetOutput, +// `current output: ${currentOutput}, target output: ${targetOutput}` +// ); + +// const currentOutput2 = formatAs12HourClock("23:00"); +// const targetOutput2 = "11:00 pm"; +// console.assert( +// currentOutput2 === targetOutput2, +// `current output: ${currentOutput2}, target output: ${targetOutput2}` +// ); + + + function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + + if (hours === 0) { + return `12:${minutes} am`; + } + if (hours === 12) { + return `12:${minutes} pm`; + } if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${pad(hours - 12)}:${minutes} pm`; } - return `${time} am`; + return `${pad(hours)}:${minutes} am`; +} + +function pad(num) { + return num.toString().padStart(2, "0"); } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); - -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; -console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` -); +// Test cases for different groups of input data and edge cases +console.assert(formatAs12HourClock("00:00") === "12:00 am", "Test midnight failed"); +console.assert(formatAs12HourClock("01:00") === "01:00 am", "Test 1am failed"); +console.assert(formatAs12HourClock("08:00") === "08:00 am", "Test 8am failed"); +console.assert(formatAs12HourClock("12:00") === "12:00 pm", "Test noon failed"); +console.assert(formatAs12HourClock("13:00") === "01:00 pm", "Test 1pm failed"); +console.assert(formatAs12HourClock("11:59") === "11:59 am", "Test 11:59am failed"); +console.assert(formatAs12HourClock("12:59") === "12:59 pm", "Test 12:59pm failed"); +console.assert(formatAs12HourClock("15:30") === "03:30 pm", "Test 3:30pm failed"); +console.assert(formatAs12HourClock("23:00") === "11:00 pm", "Test 11pm failed"); +console.assert(formatAs12HourClock("23:59") === "11:59 pm", "Test 11:59pm failed"); \ No newline at end of file From 4e090d9fa4a50a72eb25bbc8d19d9353f227bb01 Mon Sep 17 00:00:00 2001 From: Abayie Date: Thu, 17 Jul 2025 02:37:02 +0100 Subject: [PATCH 15/15] prettier --- Sprint-2/1-key-errors/0.js | 11 +++----- Sprint-2/1-key-errors/1.js | 10 +++---- Sprint-2/1-key-errors/2.js | 4 +-- Sprint-2/2-mandatory-debug/0.js | 3 +-- Sprint-2/2-mandatory-debug/1.js | 2 +- Sprint-2/2-mandatory-debug/2.js | 1 - Sprint-2/3-mandatory-implement/1-bmi.js | 10 +++---- Sprint-2/3-mandatory-implement/2-cases.js | 5 ++-- Sprint-2/3-mandatory-implement/3-to-pounds.js | 27 ++++++++++--------- Sprint-2/4-mandatory-interpret/time-format.js | 2 -- Sprint-2/5-stretch-extend/format-time.js | 27 ++++++++++++++----- 11 files changed, 53 insertions(+), 49 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 94fecb35f..cb668b157 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,9 +1,7 @@ // Predict and explain first... -// =============> write your prediction here => This code will throw a syntax error +// =============> write your prediction here => This code will throw a syntax error // because the parameter str is redeclared as a let variable inside the function, which is not allowed. - - // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -12,14 +10,13 @@ // return str; // } -// =============> write your explanation here => The error occurs because we cannot declare -// a new variable with the same name as a function parameter (str) using let inside the function. +// =============> write your explanation here => The error occurs because we cannot declare +// a new variable with the same name as a function parameter (str) using let inside the function. // This causes a "Identifier 'str' has already been declared" error according to mdn. - // =============> write your new code here function capitalise(str) { return `${str[0].toUpperCase()}${str.slice(1)}`; } -console.log(capitalise("bethan")); \ No newline at end of file +console.log(capitalise("bethan")); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index 44b685134..c82903787 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,8 +2,8 @@ // Why will an error occur when this program runs? // =============> write your prediction here -// An error will occur because 'decimalNumber' is redeclared inside the function using 'const', -// which is not allowed. Also, 'decimalNumber' is not defined in the global scope, +// An error will occur because 'decimalNumber' is redeclared inside the function using 'const', +// which is not allowed. Also, 'decimalNumber' is not defined in the global scope, // so console.log(decimalNumber) will throw a ReferenceError. // Try playing computer with the example to work out what is going on @@ -18,8 +18,8 @@ // console.log(decimalNumber); // =============> write your explanation here -// The error occurs because we cannot redeclare the parameter 'decimalNumber' inside the function -// using 'const'. Also, 'decimalNumber' is not defined outside the function, so +// The error occurs because we cannot redeclare the parameter 'decimalNumber' inside the function +// using 'const'. Also, 'decimalNumber' is not defined outside the function, so // console.log(decimalNumber) will throw a ReferenceError. // Finally, correct the code to fix the problem @@ -30,4 +30,4 @@ function convertToPercentage(decimalNumber) { return percentage; } -console.log(convertToPercentage(0.9)); // Output: 90% \ No newline at end of file +console.log(convertToPercentage(0.9)); // Output: 90% diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index e16e72f0e..7769ada67 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,4 +1,3 @@ - // Predict and explain first BEFORE you run any code... // this function should square any number but instead we're going to get an error @@ -10,7 +9,6 @@ // return num * num; // } - // =============> write the error message here // SyntaxError: Unexpected number @@ -21,7 +19,7 @@ // =============> write your new code here function square(num) { - return num * num; + return num * num; } console.log(square(7)); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index 4ca323dd4..ce0d5ac60 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -14,11 +14,10 @@ // The function multiply only prints the result to the console and does not return anything. // When we use multiply(10, 32) inside the template literal, it evaluates to undefined because the function has no return value. - // 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)}`); \ No newline at end of file +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 08a9f484f..b548d8914 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -18,4 +18,4 @@ 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 +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 dd665d537..4c6769b1e 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -8,7 +8,6 @@ // The last digit of 806 is 3 // Because getLastDigit always uses the global variable num (103) and ignores the argument. - // const num = 103; // function getLastDigit() { diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 81f2b1c05..35eccb31c 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,10 +15,10 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // Calculate BMI: weight divided by height squared - const bmi = weight / (Math.pow(height, 2)); - // Return BMI rounded to 1 decimal place - return +(bmi.toFixed(1)); + // Calculate BMI: weight divided by height squared + const bmi = weight / Math.pow(height, 2); + // Return BMI rounded to 1 decimal place + return +bmi.toFixed(1); } -console.log(calculateBMI(78, 1.72)); \ No newline at end of file +console.log(calculateBMI(78, 1.72)); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 51b6cb3ba..5ff76d072 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -16,10 +16,9 @@ // This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase function toUpperSnakeCase(str) { - // Replace spaces with underscores and convert to uppercase - return str.replace(/ /g, "_").toUpperCase(); + // Replace spaces with underscores and convert to uppercase + return str.replace(/ /g, "_").toUpperCase(); } - console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE" console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS" diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 3a2a318c9..227facbec 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -6,18 +6,19 @@ // You should call this function a number of times to check it works for different inputs function toPounds(penceString) { - // Remove trailing 'p' and pad with zeros to ensure at least 3 digits - const pence = penceString.substring(0, penceString.length - 1).padStart(3, "0"); - // Get pounds and pence parts - const pounds = pence.substring(0, pence.length - 2); - const pencePart = pence.substring(pence.length - 2).padEnd(2, "0"); - // Return formatted string - return `£${pounds}.${pencePart}`; + // Remove trailing 'p' and pad with zeros to ensure at least 3 digits + const pence = penceString + .substring(0, penceString.length - 1) + .padStart(3, "0"); + // Get pounds and pence parts + const pounds = pence.substring(0, pence.length - 2); + const pencePart = pence.substring(pence.length - 2).padEnd(2, "0"); + // Return formatted string + return `£${pounds}.${pencePart}`; } - -console.log(toPounds("399p")); // £3.99 -console.log(toPounds("9p")); // £0.09 -console.log(toPounds("99p")); // £0.99 -console.log(toPounds("100p")); // £1.00 -console.log(toPounds("1234p")); // £12.34 +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("9p")); // £0.09 +console.log(toPounds("99p")); // £0.99 +console.log(toPounds("100p")); // £1.00 +console.log(toPounds("1234p")); // £12.34 diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 1350f7cf5..80f30b1e4 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -26,7 +26,6 @@ function formatTimeDisplay(seconds) { // =============> write your answer here // The first call is pad(totalHours). For input 61, totalHours is 0. - // c) What is the return value of pad is called for the first time? // =============> write your answer here // pad(0) returns "00". @@ -35,7 +34,6 @@ function formatTimeDisplay(seconds) { // =============> write your answer here // The last call is pad(remainingSeconds). For input 61, remainingSeconds 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 // pad(1) returns "01". diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 296d77950..591f757d7 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -24,8 +24,6 @@ // `current output: ${currentOutput2}, target output: ${targetOutput2}` // ); - - function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); const minutes = time.slice(3, 5); @@ -47,13 +45,28 @@ function pad(num) { } // Test cases for different groups of input data and edge cases -console.assert(formatAs12HourClock("00:00") === "12:00 am", "Test midnight failed"); +console.assert( + formatAs12HourClock("00:00") === "12:00 am", + "Test midnight failed" +); console.assert(formatAs12HourClock("01:00") === "01:00 am", "Test 1am failed"); console.assert(formatAs12HourClock("08:00") === "08:00 am", "Test 8am failed"); console.assert(formatAs12HourClock("12:00") === "12:00 pm", "Test noon failed"); console.assert(formatAs12HourClock("13:00") === "01:00 pm", "Test 1pm failed"); -console.assert(formatAs12HourClock("11:59") === "11:59 am", "Test 11:59am failed"); -console.assert(formatAs12HourClock("12:59") === "12:59 pm", "Test 12:59pm failed"); -console.assert(formatAs12HourClock("15:30") === "03:30 pm", "Test 3:30pm failed"); +console.assert( + formatAs12HourClock("11:59") === "11:59 am", + "Test 11:59am failed" +); +console.assert( + formatAs12HourClock("12:59") === "12:59 pm", + "Test 12:59pm failed" +); +console.assert( + formatAs12HourClock("15:30") === "03:30 pm", + "Test 3:30pm failed" +); console.assert(formatAs12HourClock("23:00") === "11:00 pm", "Test 11pm failed"); -console.assert(formatAs12HourClock("23:59") === "11:59 pm", "Test 11:59pm failed"); \ No newline at end of file +console.assert( + formatAs12HourClock("23:59") === "11:59 pm", + "Test 11:59pm failed" +);