diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..552605d40 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,12 @@ // Predict and explain first... -// =============> write your prediction here +// =============> I expect the function capitalise will turn first letter of string given by user into capital letter. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +// =============> The result was a Syntax error because str has been declared as parameter of function captialise before let reassign str. +// =============> function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } - -// =============> write your explanation here -// =============> write your new code here diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..6a42a9427 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 +// =============> SyntaxError will occurs // Try playing computer with the example to work out what is going on +// =============> decimalNumber first declared as parameter, it cannot be reassigned as const. +// Also, console.log try to run decimalNumber as a variable while decimalNumber was assigned as a parameter. +// Finally, correct the code to fix the problem +// =============> write your new code here function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } - -console.log(decimalNumber); - -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here +console.log(convertToPercentage(0.5)); diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..9b96755f0 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,17 @@ - // 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 - -function square(3) { - return num * num; -} +// =============> I expect SyntaxError will result -// =============> write the error message here +// =============> SyntaxError: Unexpected number -// =============> explain this error message here +// =============> Numerical character "3" cannot be named as parameter of a function. // 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..2a0737b02 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,18 @@ // Predict and explain first... -// =============> write your prediction here +// =============> Undefined at the end of sentence. console.log in local function will result the product. But console.log in global scope will not call function multiply. -function multiply(a, b) { - console.log(a * b); -} - -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +//function multiply(a, b) { +// console.log(a * b); +//} -// =============> write your explanation here +//console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +// =============> The function multiply does not really carry out the multiplication. It only logs the result of a*b. That's why calling multiply become 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)}`); diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..978eb0ef4 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,18 @@ // Predict and explain first... -// =============> write your prediction here +// =============> I predict a SyntaxError in original code -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 +// =============> Expression a + b is not assigned to 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)}`); \ No newline at end of file diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..705cd3563 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,11 +1,30 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> Code returns last digit of 103 which is the value of num assigned in global scope. +//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)}`); + +// Now run the code and compare the output to your prediction +// =============> +// 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 +// =============> The function refers to the value of `num` assigned in global scope rather than getLastDigit, because 'num' was not a parameter in function name. +// Finally, correct the code to fix the problem +// =============> const num = 103; -function getLastDigit() { +function getLastDigit(num) { return num.toString().slice(-1); } @@ -13,12 +32,5 @@ 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. // Explain why getLastDigit is not working properly - correct the problem diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..e410c6f10 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -16,4 +16,17 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height + return (weight / Math.pow(height, 2)).toFixed(1); } + +console.log( + `BMI base off weight in kg and height in metres is ${calculateBMI(70, 1.73)}`, +); + +//draft pseudocode below +//name of function calculateBMI +//key word return +//parameters : weight, height +//formula weight / height x height +//or weight / sqr height (by Method: Math.pow(parameter,power)) +//by Method .toFixed(1) to return BMI in string in one decimal place diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..bb1ada7b4 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,14 @@ // 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 + +//pseudocode draft below +//Name of function upperSnakeCase +//Parameter (userInputString) type: String +//Method .toUpperCase() .replaceAll("","_") +//print by console.log (`${userInputString()`}) + +function upperSnakeCase(userInputString) { + return userInputString.toUpperCase().replaceAll(" ", "_"); +} +console.log(upperSnakeCase("hello world")); diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..a049229fa 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,35 @@ // 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 name toPound +//identifier of prev code : +// 1 penceString - user input ; Type: String, count length by .length +// 2 penceStringWithoutTrailingP ; cut the tail p by .substring () +// 3 paddedPenceNumberString ; assign and prescribe target length of pence in three characters length by .padStart() +// 4 pounds ; assign pounds from first character of paddedPenceNumberString, cut the last two character by .substring +// 5 pence ; assign pence starting from last two character of paddedPenceNumberString +// Parameter to name wholePenceStringWithP + +function toPound(wholePenceStringWithP) { + const penceStringWithoutTrailingP = wholePenceStringWithP.substring( + 0, + wholePenceStringWithP.length - 1, + ); + + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2, + ); + + const pence = paddedPenceNumberString.substring( + paddedPenceNumberString.length - 2, + ); + + return `£${pounds}.${pence}`; +} +console.log(toPound("399p")); +console.log(toPound("1399p")); +console.log(toPound("99p")); +console.log(toPound("9p")); \ No newline at end of file diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..da5cf085c 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -15,24 +15,25 @@ 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 // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> Three 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 when it 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. The last pad call takes `remainingSeconds` as its argument, and 61 % 60 =1 , so num 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 +// =============> 01. `numString` starts as "1", which has one character length. Since 1 < 1, the while lines run and pad "0", giving "01". Now this new `numString` has two characters length, so the while loop stops and returns "01". diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..eac4c14da 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -4,22 +4,56 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + const minutes = time.slice(-2); + if (hours > 22) { + return `${hours - 12}:${minutes} pm`; + } else if (hours > 12) { + return `0${hours - 12}:${minutes} pm`; + } else if (hours > 9) { + return `${hours}:${minutes} am`; + } else if (hours < 1) { + return `12:${minutes} am`; } - return `${time} am`; + + return `0${hours}:${minutes} am`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +const currentOutput = formatAs12HourClock("08:01"); +const targetOutput = "08:01 am"; console.assert( currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` + `current output: ${currentOutput}, target output: ${targetOutput}`, ); const currentOutput2 = formatAs12HourClock("23:00"); const targetOutput2 = "11:00 pm"; console.assert( currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` + `current output: ${currentOutput2}, target output: ${targetOutput2}`, ); + +const currentOutput3 = formatAs12HourClock("00:01"); +const targetOutput3 = "12:01 am"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}`, +); + +const currentOutput4 = formatAs12HourClock("11:01"); +const targetOutput4 = "11:01 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}`, +); + +console.log(formatAs12HourClock("08:01")); +console.log(formatAs12HourClock("23:55")); +console.log(formatAs12HourClock("00:01")); +console.log(formatAs12HourClock("11:01")); +console; + +//Two patches +//First hour input > 24, current algo is hours - 12 , e.g. 25-12 and print 13:00pm , rather than 1:00am (+1) +//to Define `hoursOfOneDay` = 24 +//2 min input +//to Define