diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..2bd6280251 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,36 @@ // Predict and explain first... + // =============> write your prediction here +// str is already a declared variable fed into the capitalise function, the function attempts +// to declare the variable again, this may throw an error. + // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring + +// Error: + +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// SyntaxError: Identifier 'str' has already been declared + + +/* Original Code: + function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; -} +} + +*/ // =============> write your explanation here +// As predicted, an already declared variable cannot be declared again + // =============> write your new code here + +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} + diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..4f58dbf3fb 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,10 +1,14 @@ // Predict and explain first... - // Why will an error occur when this program runs? + // =============> write your prediction here +// Assuming we define decimalNumber for the function (otherwise that will throw an error) +// then we run into the same problem as the last exercise, const decimalNumber = 0.5; +// is trying to define a variable that has already been determined // Try playing computer with the example to work out what is going on +/* Original code: function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -13,8 +17,20 @@ function convertToPercentage(decimalNumber) { } console.log(decimalNumber); +*/ // =============> write your explanation here +// Uncaught SyntaxError: Identifier 'decimalNumber' has already been declared +// As predicted the function is trying to declare a variable that has already been determined // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + decimalNumber = 0.5; + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +console.log(decimalNumber); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..c57119a27b 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,27 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// num hasn't been defined, that will throw an error +/* function square(3) { - return num * num; + return num3 * num3; } +*/ // =============> write the error message here +// Uncaught SyntaxError: Unexpected number // =============> explain this error message here +// the error appears in the line before, a function has to take defined parameters, these +// parameters cannot be represented by numbers (or at least begin with numbers) // 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..f8110b413d 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,27 @@ // Predict and explain first... // =============> write your prediction here +// The multiple function doesn't return anything +// the console log that calls the multiple function will not receive anything back, this +// may cause an error +/* Original code: 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 +// code works, however the log gave 'undefined' instead of the multiplication output +// this is because the multiply function doesn't return any 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 diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..8c2ca8bcaa 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,24 @@ // Predict and explain first... // =============> write your prediction here +// the sum function won't return anything to the console.log +// because the semicolon ends that line of logic +/* Original code: function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +*/ // =============> write your explanation here +// As before, the function returns "undefined" as nothing is returned + // 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..c6e6456097 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,7 +2,10 @@ // Predict the output of the following code: // =============> Write your prediction here +// the function does not pass through a parameter, therefore it will always +// call the defined variable num as 103, giving 3 as an output each time +/* Original code: const num = 103; function getLastDigit() { @@ -12,13 +15,32 @@ function getLastDigit() { 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 +// there is no parameter for the function + // Finally, correct the code to fix the problem // =============> write your new code here +const num = 103; + +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 17b1cbde1b..e7bc3c8f2d 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,5 @@ // 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 + return (weight / (height^2)).toFixed(1) } \ 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..22226a68ed 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,17 @@ // 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 upperSnakeCase(str) { + const upperCaseStr = str.toUpperCase(); + return upperCaseStr.replaceAll(" ","_"); +} + +//tests +console.log(upperSnakeCase("hello world")); +console.log(upperSnakeCase("hello world")); +console.log(upperSnakeCase("h e l l o w o r l d ")); +console.log(upperSnakeCase(" hello world ")); +console.log(upperSnakeCase("hello 5555 world 6666 ")); +console.log(upperSnakeCase("hello world the quick brown fox jumps over the lazy dog")); + diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..34667bf70d 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,23 @@ // 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 + +const penceString = "399p"; + +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}` +} + +//Tests +console.log(toPounds("399p")) +console.log(toPounds("3599p")) +console.log(toPounds("390p")) +console.log(toPounds("9p")) +console.log(toPounds("666399p")) +console.log(toPounds("30000001p")) +console.log(toPounds("99p")) +console.log(toPounds("300p")) \ 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..7157dfadec 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -22,17 +22,26 @@ function formatTimeDisplay(seconds) { // 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 +// When pad is called for the last time it is here pad(remainingSeconds) +// remaining seconds = seconds % 60 = 61 % 60 = 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 +// when pad is called it takes the num, in this case 1, and pads it out to two digits by adding zeros at the front +// so 1 becomes 01