diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..cb668b157 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,22 @@ // 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 +// function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// 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. +// This causes a "Identifier 'str' has already been declared" error according to mdn. + +// =============> write your new code here function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + return `${str[0].toUpperCase()}${str.slice(1)}`; } -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("bethan")); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..c82903787 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% diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..7769ada67 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,25 @@ - // Predict and explain first BEFORE you run any code... // 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)); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..ce0d5ac60 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,23 @@ -// 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)}`); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..b548d8914 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)}`); diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..4c6769b1e 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,23 +2,36 @@ // 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; +// const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +// 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)}`); +// 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 diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..35eccb31c 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)); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..5ff76d072 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(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..227facbec 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,21 @@ // 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 diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..80f30b1e4 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,22 @@ 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". diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..591f757d7 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,24 +2,71 @@ // 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"; +// Test cases for different groups of input data and edge cases console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` + formatAs12HourClock("00:00") === "12:00 am", + "Test midnight failed" ); - -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; +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( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` + formatAs12HourClock("23:59") === "11:59 pm", + "Test 11:59pm failed" ); diff --git a/prep/example.js b/prep/example.js new file mode 100644 index 000000000..5fb4293a2 --- /dev/null +++ b/prep/example.js @@ -0,0 +1,15 @@ +// function decimalTopercent(num){ +// let percent = num * 100; +// return percent +// } +// console.log(decimalTopercent(0.5) + "%"); + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} + +const result = convertToPercentage(0.5); +const result1 = convertToPercentage(0.231); +console.log(result); +console.log(result1);