diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..2d7341451 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,20 @@ // Predict and explain first... // =============> write your prediction here - +// it will take a string and turn the first letter into a capital. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring - +// SyntaxError: Identifier 'str' has already been declared - str cannot be used twice as a variable name. function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } // =============> write your explanation here +// i've changed the variable name from str to result. This corrected the error and return result. // =============> write your new code here +function capitalise(str) { + let result = `${str[0].toUpperCase()}${str.slice(1)}`; + console.log("result",result); + return result; +} +capitalise("pear") \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..dc2a60c24 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,7 +2,7 @@ // Why will an error occur when this program runs? // =============> write your prediction here - +// They have used the same variable name twice. This will cause an error. Also line 9 is not needed because the function takes a decimal as an argument. // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -15,6 +15,14 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +// SyntaxError: Identifier 'decimalNumber' has already been declared. +// ReferenceError: decimalNumber is not defined // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + console.log("percentage",percentage) + return percentage; +} +convertToPercentage(0.8) \ 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 aad57f7cf..1d5ad5d72 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,20 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here - +// You can't put a number value into an argument. function square(3) { return num * num; } // =============> write the error message here - +// SyntaxError: Unexpected number // =============> explain this error message here - +// It is not expecting a number to be in the function argument. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} - +console.log("square",square(5)) \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..2f284c7cb 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,7 +1,7 @@ // Predict and explain first... // =============> write your prediction here - +// the function will not return anything. function multiply(a, b) { console.log(a * b); } @@ -10,5 +10,13 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// declared a variable called result and stored a*b. + // Finally, correct the code to fix the problem // =============> write your new code here +function multiply(a, b) { + console.log(a * b); + const result = a*b; + return result; +} +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..8813ca71a 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,20 @@ // Predict and explain first... // =============> write your prediction here - +// This appears correct. The function has been defined and called sum, a is 10 and b is 32 so we should get 42. function sum(a, b) { return; - a + b; +a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The sum of 10 and 32 is undefined. On line five the function has been exited because of the ; this because the function doesn't return anything. // 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 57d3f5dc3..e1472065d 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -3,6 +3,7 @@ // Predict the output of the following code: // =============> Write your prediction here +// It will return three as a string. const num = 103; function getLastDigit() { @@ -15,10 +16,22 @@ 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 +// "3" + // Explain why the output is the way it is // =============> write your explanation here +// This is because they've defined num as 103, and they've used num inside the function, last digit of 103 is 3. // Finally, correct the code to fix the problem // =============> write your new code here +function getLastDigit(aNumber) { + return aNumber.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 +// This is because they've defined num as 103 so the result will always be 3. The arguments should be used instead (42, 105, and 806) and num should not be used. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..c9c3985fa 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,13 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file + console.log("weight", weight); + console.log("height",height); + + const heightSquared = (height * height).toFixed(2); + console.log("heightSquared",heightSquared); + const BMI = (weight / heightSquared).toFixed(2); + console.log("BMI",BMI) + return BMI; +} +console.log("calculateBMI",calculateBMI(70, 1.73)) \ 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..16e1aa914 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,10 @@ // 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 makeUpperCaseSnakeCase(str){ +const result = str.split(" ").join("_").toUpperCase(); //take str, and turn it into an array and then turn it back into a string with underscores then upper case it. +console.log("result",result); +} + +console.log(makeUpperCaseSnakeCase("hello there")); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..d6b91763c 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,18 @@ // 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 + +let count = 0; + +count = count + 1; + +function toPounds(pennies) { + let pounds = pennies / 100; + return pounds; +} + +console.log(toPounds(50)); +console.log(toPounds(100)); +console.log(toPounds(150)); +console.log(toPounds(250)); +console.log(toPounds(350)); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..16d5c867a 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -1,7 +1,10 @@ function pad(num) { + console.log("num",num); return num.toString().padStart(2, "0"); } + + function formatTimeDisplay(seconds) { const remainingSeconds = seconds % 60; const totalMinutes = (seconds - remainingSeconds) / 60; @@ -11,6 +14,8 @@ 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 @@ -18,17 +23,18 @@ 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 +// 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, this is because they assign remainingSeconds to num for the last time pad is called. -// 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 +// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer - "do you mean the return value for pad?" +// 01, this is the result of calling pad for the last time. diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..c86c5bc06 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -7,8 +7,12 @@ function formatAs12HourClock(time) { if (hours > 12) { return `${hours - 12}:00 pm`; } + if (hours === 0) { + return `${12}:00 am`; + } return `${time} am`; } +console.log("formatAs12HourClock",formatAs12HourClock("17:00")) const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; @@ -23,3 +27,34 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("00:00"); +const targetOutput3 = "12:00 am"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("00:00"); +const targetOutput4 = "12:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +const currentOutput5 = formatAs12HourClock("20:30"); +const targetOutput5 = "08:30 am"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); + +const currentOutput6 = formatAs12HourClock("23:55"); +const targetOutput6 = "11:55 am"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +); +// test mid-day +// test 08:30 +// test 11:55 \ No newline at end of file