diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..6200eeb23 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,15 @@ // Predict and explain first... -// =============> write your prediction here - +// =============> write your prediction here==> SyntaxError because str is declared twice (parameter + let) // 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==> (let str) will make a syntaxerror because str is already declared as parameter in the function: [function capitalise(str) ] and we can not declare an identifier in the same scope +// =============> write your new code here: function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; } - -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("hello world")); diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..36bef60fd 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,20 +1,17 @@ // Predict and explain first... - // Why will an error occur when this program runs? -// =============> write your prediction here +// Prediction: SyntaxError — decimalNumber is a parameter and is redeclared with const inside the function. +// Also, console.log(decimalNumber) outside the function would cause ReferenceError (not in that scope). -// Try playing computer with the example to work out what is going on +// Explanation: +// 1) You cannot declare const decimalNumber when decimalNumber is already a parameter in the same scope. +// 2) decimalNumber only exists inside the function, so logging it outside fails. +// Fix: use the parameter (do not reassign it) and call the function in console.log. function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; - return percentage; } -console.log(decimalNumber); - -// =============> write your explanation here +console.log(convertToPercentage(0.5)); -// Finally, correct the code to fix the problem -// =============> write your new code here diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..8aa65d6ef 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,29 @@ - // 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: SyntaxError — you cannot use a number literal (3) as a parameter name. +// Parameter names must be valid identifiers (e.g. num), not values. +// The value 3 should be passed when calling the function, not in the definition. -function square(3) { - return num * num; -} +//function square(3) { +//return num * num; +//} // =============> write the error message here +// SyntaxError: Unexpected number +// (exact wording can vary slightly by Node version, e.g. "Missing formal parameter") // =============> explain this error message here +// In a function definition, the parentheses list parameter names (placeholders). +// 3 is a value (argument), not a name, so JavaScript rejects it while parsing the file. +// Even if that were fixed, num is not defined in the original code — the parameter +// and the name used inside the function must match. // Finally, correct the code to fix the problem - // =============> write your new code here +function square(num) { + return num * num; +} +console.log(square(3)); diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..f5cbba421 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,13 @@ // Predict and explain first... - -// =============> write your prediction here +// Prediction: multiply logs 320 but returns undefined, so the template +// string prints: "The result of multiplying 10 and 32 is undefined" function multiply(a, b) { - console.log(a * b); + return a * b; } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here +// Explanation: console.log inside a function only prints; it does not return +// a value. Without return, the function result is undefined. Using return +// gives 320 so the template string shows the correct message. diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..442f01cb2 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,24 @@ // Predict and explain first... // =============> write your prediction here +// Prediction: return; exits immediately with undefined, so a + b never runs +// and the message shows "undefined". -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)}`); // =============> write your explanation here +// Explanation: A bare return; returns undefined. The expression must be on +// the same return statement: return a + b; + // 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-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..fe87377d3 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,24 +1,43 @@ // Predict and explain first... - // Predict the output of the following code: // =============> Write your prediction here +// Prediction: All three lines print "3" because getLastDigit has no parameter +// and always uses the outer const num = 103 (last digit 3). The arguments +// 42, 105, and 806 are ignored. -const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +//const num = 103; + +//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 // =============> 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 +// getLastDigit() does not declare a parameter, so the values passed in +// (42, 105, 806) are not available inside the function. It reads the +// global num (103) every time, so slice(-1) always returns "3". + // 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-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..3c3d31645 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,7 @@ // It should return a string of their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height + return (weight/(height*height)).toFixed(1); } + +console.log(calculateBMI(70, 1.73)); diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..89e4166de 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/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(string) { + return string.toUpperCase().replaceAll(" ", "_"); +} + +console.log(toUpperSnakeCase("hello there")); +console.log(toUpperSnakeCase("lord of the rings")); +console.log(toUpperSnakeCase("upper snake case")); diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..572177862 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,28 @@ // 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("399p")); +console.log(toPounds("50p")); +console.log(toPounds("1729p")); +console.log(toPounds("5p")); diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..dddc5a631 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -14,7 +14,7 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } - +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 @@ -22,17 +22,22 @@ function formatTimeDisplay(seconds) { // 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 +//Ther value is 0. // c) What is the return value of pad when it is called for the first time? // =============> write your answer here +// The 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" — last call is pad(remainingSeconds), and for 61 seconds that is 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 +//The value is ""01" — pad turns 1 into a two-digit string.