diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..75b639a71 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,7 +1,11 @@ // Predict and explain first... -// =============> write your prediction here +// I thought it was going to work, however my prediction was wrong. =============> write your prediction here // call the function capitalise with a string input + +//SyntaxError: Identifier 'str' has already been declared + + // interpret the error message and figure out why an error is occurring function capitalise(str) { @@ -9,5 +13,13 @@ function capitalise(str) { return str; } -// =============> write your explanation here -// =============> write your new code here + + + +// The identifier "str" has already been declared as a parameter in the function and cannot be redeclared withing a block scope.// =============> write your explanation here + + + function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; + } + // =============> write your new code here diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..72066c28f 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -5,16 +5,24 @@ // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; -} -console.log(decimalNumber); -// =============> write your explanation here +// we already have a parameter "decimalNumber" which has been declared in the function. declaring it again with constant is going to give syntax error. +// Also the console.log is trying to print "decimalNumber" which only exist in a function that is outside its scope.=============> write your explanation here // 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.5));// =============> write your new code here diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..78c2b8373 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,21 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// A number cannot be used as a parameter.=============> write your prediction of the error here -function square(3) { - return num * num; -} -// =============> write the error message here +// SyntaxError: Unexpected number =============> write the error message here + -// =============> explain this error message here +// The parameter is not meant to be a number. Also we are trying to return num that was not never created. =============> explain this error message here // Finally, correct the code to fix the problem + +function square (num) { + return num * num; + +} // =============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..731b1044a 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,19 @@ // Predict and explain first... -// =============> write your prediction here +// I am predicting there will be an error. =============> write your prediction here + + -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 +// Although it is going to print the value to be 320, it won't return back the value as 320.=============> write your explanation here // Finally, correct the code to fix the problem -// =============> write your new code here + + +function multiply(a, b) { + return (a * b); +} + +console.log(`${multiply(10, 32)}`) +//=============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..b1d672aaf 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,16 @@ // Predict and explain first... -// =============> write your prediction here +// There will be an error because there is going to be nothing to define after return. =============> write your prediction here -function sum(a, b) { - return; - a + b; -} -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here -// Finally, correct the code to fix the problem -// =============> write your new code here + + +//Finally, correct the code to fix the problem + function sum(a, b) { + return a+b; + +} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +//=============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..b487bf80b 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,23 +1,23 @@ // Predict and explain first... -// Predict the output of the following code: -// =============> Write your prediction here +// IPredict the output of the following code: +// I am predicting an error because the variable const num = 103 is outside the function. =============> Write your prediction here +// There is no parameters in the function condition -const num = 103; -function getLastDigit() { + +// Now run the code and compare the output to your prediction +// when i ran the output i got 3,3,3.=============> write the output here +// Explain why the output is the way it is +// the argument for the console.log is ignored because "getlastDigit" does not have a parameter. =============> write your explanation here +// Finally, correct the code to fix 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)}`); - -// 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 -// 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. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..0115d9529 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,11 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + const bmi = weight/(height * height); + return bmi.toFixed(1); // return the BMI of someone based off their weight and height -} \ No newline at end of file +} + +console.log (calculateBMI(90,1.92)); + +// After running node console.;og printed (24.4). \ 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 5b0ef77ad..4b8f42e7a 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) { + return str.toUpperCase().replaceAll(" ", "_"); +} +console.log (toUpperSnakeCase("Code Your Future")); + +// After i ran node it printed (CODE_YOUR_FUTURE). \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..4808093b3 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,32 @@ // 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")); //£3.99 +console.log (toPounds("156p")); //£1.56 +console.log (toPounds("10p")); //£0.10 +console.log (toPounds("56p")); //£0.56 +console.log (toPounds("25p")); // £0.25 + +// After running node it printed the above respectively. diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..53bb971c7 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,18 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// 3 times. =============> write your answer here // 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 +// Answer num is 0. =============> write your answer here // c) What is the return value of pad is called for the first time? -// =============> write your answer here +num.toString().padStart(2, "0"); // answer will be "00"=============> write your answer here // 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 +// num will be 1. The last pad is for the seconds value. =============> write your answer here // 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 +// Ans is "01" because padStart will enable two digits thereby adding 0 in front of it. =============> write your answer here diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..352ba235f 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,14 +2,21 @@ // 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`; + let [hours, minutes] = time.split(":").map(Number); + let period = hours >= 12 ? "pm" : "am"; + + if (hours === 0) { + hours = 12; + } else if (hours > 12) { + hours -= 12; } - return `${time} am`; -} + const hoursString = hours < 10 ? `0${hours}` : `${hours}`; + const minutesString = minutes < 10 ? `0${minutes}` : `${minutes}`; + + return `${hoursString}:${minutesString} ${period}`; const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; console.assert( @@ -23,3 +30,17 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("21:00"); +const targetOutput3 = "09:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); +} + + + +console.log(formatAs12HourClock("08:00")); // 08:00 am +console.log(formatAs12HourClock("23:00")); // 11:00 pm +console.log(formatAs12HourClock("21:00")); // 09:00 pm \ No newline at end of file