diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..577238d93 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,6 +1,11 @@ // Predict and explain first... // =============> write your prediction here +// Prediction: +// The code will throw an error because `str` is declared twice: +// once as the function parameter and again with `let str` inside the function. +// JavaScript does not allow redeclaring the same identifier in the same scope. + // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +15,20 @@ function capitalise(str) { } // =============> write your explanation here + +// Explanation: +// When the file runs, JavaScript gives a SyntaxError like: +// "Identifier 'str' has already been declared". +// This happens because `str` already exists as a parameter, so `let str` tries +// to redeclare it in the same scope. To fix it, we should not redeclare `str`. +// Instead, we can create a new variable or just return the new string. + // =============> write your new code here + +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} + +console.log(capitalise("hello")); // Hello + diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..ea10badec 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -3,6 +3,14 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// Prediction: +// An error will occur because `decimalNumber` is declared twice. +// It is first declared as a function parameter and then declared again +// inside the function using `const decimalNumber = 0.5;`. +// JavaScript does not allow redeclaring the same variable in the same scope. +// Also, `console.log(decimalNumber);` is outside the function, +// so `decimalNumber` is not defined in the global scope. + // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -16,5 +24,21 @@ console.log(decimalNumber); // =============> write your explanation here +// Explanation: +// The parameter `decimalNumber` is already defined in the function. +// Declaring `const decimalNumber = 0.5;` again causes a SyntaxError: +// "Identifier 'decimalNumber' has already been declared". +// Additionally, `console.log(decimalNumber);` causes a ReferenceError +// because `decimalNumber` only exists inside the function and +// cannot be accessed outside of it. + // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} + +const result = convertToPercentage(0.5); +console.log(result); // 50% diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..f99e66ed7 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,46 @@ - // 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 +// Prediction: +// An error will occur because `3` is not a valid parameter name. +// Function parameters must be variable names, not numbers. +// JavaScript will throw a SyntaxError before running the program. + function square(3) { return num * num; } // =============> write the error message here +// Error message: +// SyntaxError: Unexpected number + // =============> explain this error message here +// Explanation: +// The error occurs because `3` is used as the function parameter. +// In JavaScript, function parameters must be identifiers (variable names). +// A number cannot be used as a parameter name. +// Because of this invalid syntax, JavaScript throws a SyntaxError +// before the program can run. +// Additionally, the variable `num` is used inside the function +// but is not defined, which would cause another error +// if the syntax error did not occur first. + // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} + +console.log(square(3)); // 9 + + + + diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..420a24d93 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -2,13 +2,41 @@ // =============> write your prediction here +// Prediction: +// The program will print 320, +// but it will also print "undefined" in the sentence. +// This happens because the function uses console.log() +// instead of returning a value. +// If a function does not return anything, +// JavaScript automatically returns undefined. + 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 +// Explanation: +// The function multiply(a, b) prints the result using console.log(), +// but it does not return the result. +// When multiply(10, 32) is used inside the template string, +// JavaScript expects a returned value. +// Because nothing is returned, the value becomes undefined. +// That is why the output shows: +// +// 320 +// The result of multiplying 10 and 32 is undefined + + // 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)}`);//320 + diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..2bfa60446 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,12 @@ // Predict and explain first... // =============> write your prediction here +// Prediction: +// The program will print "The sum of 10 and 32 is undefined". +// This happens because the return statement is written incorrectly. +// JavaScript stops executing a function immediately after "return". +// Since nothing is returned, the function returns undefined. + function sum(a, b) { return; a + b; @@ -8,6 +14,22 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); + // =============> write your explanation here + +// Explanation: +// In JavaScript, when "return" is written on its own line, +// the function stops immediately and returns undefined. +// The line "a + b;" is never executed. +// That is why sum(10, 32) becomes undefined. +// The issue is caused by the line break after return. + + // 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)}`);//32 diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..8a1838926 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -3,6 +3,12 @@ // Predict the output of the following code: // =============> Write your prediction here +// Prediction: +// The function will always return "3". +// Even though we pass 42, 105, and 806 into the function, +// it will ignore those values and use the global variable `num = 103`. +// Therefore, every line will print 3 as the last digit. + const num = 103; function getLastDigit() { @@ -13,12 +19,43 @@ 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 + +// Output: +// 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 + +// Explanation: +// The function getLastDigit does not accept a parameter. +// Because of that, it ignores the values passed inside the parentheses. +// Instead, it always uses the global constant `num`, which is 103. +// The function converts 103 to a string and takes the last character, +// which is "3". That is why every output is 3. + + // 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)}`);//2 +console.log(`The last digit of 105 is ${getLastDigit(105)}`);//5 +console.log(`The last digit of 806 is ${getLastDigit(806)}`);//6 + + // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem + +// The function was not working properly because it did not use a parameter. +// It was hard-coded to use the global variable `num` instead of the value +// passed into the function. By adding a parameter (number), +// the function now works correctly for any input. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..47130b119 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,7 @@ 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(88, 1.78));//27.8 + diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..373590feb 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) { + return str.replaceAll(" ", "_").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..b02faf97e 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -1,6 +1,31 @@ // In Sprint-1, there is a program written in interpret/to-pounds.js - // You will need to take this code and turn it into a reusable block of code. // 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}`; +} + +// Call the function a number of times to check it works for different inputs +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("5p")); // £0.05 +console.log(toPounds("45p")); // £0.45 +console.log(toPounds("120p")); // £1.20 +console.log(toPounds("0p")); // £0.00 diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..162639ac3 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,24 +11,29 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +// Calling the function with 61 as the argument +console.log(formatTimeDisplay(61)); + + + // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit // to help you answer these questions // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> pad will be called 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 +// =============> The value is 0. // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> The return value is "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 value is 1 because 61 % 60 leaves 1 second remaining. // 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 return num.toString().padStart(2, "0") formats 1 into "01". \ No newline at end of file diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..c5dd2901f 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,9 +4,24 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + + // If midnight (00), convert to 12 am + if (hours === 0) { + return `12:${minutes} am`; + } + + // If noon (12), it stays 12 pm + if (hours === 12) { + return `12:${minutes} pm`; + } + + // If greater than 12, subtract 12 and make it pm if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`; } + + // Otherwise it is morning (am) return `${time} am`; }