diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..6dac6f54be 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,6 @@ // Predict and explain first... // =============> write your prediction here +//the function is there for capitalise the given string // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -9,5 +10,11 @@ function capitalise(str) { return str; } +console.log(capitalise("hello")) // =============> write your explanation here + // Identifier 'str' has already been declared - means the variable already exist - with the let you don't need to declare this 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..f831363d6d 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,6 +2,7 @@ // Why will an error occur when this program runs? // =============> write your prediction here + //the function is there to convert the given number to percentage // Try playing computer with the example to work out what is going on @@ -15,6 +16,19 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here + //the function is there to convert the given number to percentage + //the error will come up again as the decimalNumber has already been declared - we don't need to assign this as a variable again + //the console log missing the assigned number with the brackets + //also we would need to call the function not the parameter + // 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(convertToPercentage(decimalNumber)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..9edaf07a14 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,5 +1,6 @@ // Predict and explain first BEFORE you run any code... + //function is there to calculate the square parameter // this function should square any number but instead we're going to get an error @@ -10,11 +11,18 @@ function square(3) { } // =============> write the error message here + // we are returning the num - but the parameter already given as an argument not a parameter 3 - + // for the function to recognize num we would need to give this to the function as a parameter // =============> explain this error message here + //unexpected number - the function has an unexpected argument // Finally, correct the code to fix the problem // =============> write your new code here + //function square(num) { + //return num * num; + //} + //square(3) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..5465dc2a45 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,4 +1,6 @@ // Predict and explain first... +//function is there to multiply the given arguments in the console log line 10 + // =============> write your prediction here @@ -9,6 +11,11 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here - + // we would have an error message as we would need to return the statement // 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-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..13916f6184 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,18 @@ // Predict and explain first... // =============> write your prediction here +// the function will sum the given parameters function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here + // unable to return nothing - the statement is not given - has to be in the same line with the return // Finally, correct the code to fix the problem + // =============> write your new code here + // function sum(a, b) { + //return a + b; + //} diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..07068917bd 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,11 +1,16 @@ // Predict and explain first... + // the function should get the last digit of the given parameter - + // but the parameter is the already existing variable // Predict the output of the following code: // =============> Write your prediction here + //already assigned variable num will be returned as we not defined the parameter inside the return + const num = 103; -function getLastDigit() { + +function getLastDigit(num) { return num.toString().slice(-1); } @@ -15,10 +20,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 + //every time we call the function we return the same number as this is already assigned inside line 10 // Explain why the output is the way it is // =============> write your explanation here + //we need to give the parameter to the function to be able to use the arguments in the console log // 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..e4b949f081 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,6 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file + return (weight / Math.pow(height, 2)).toFixed(1) +} +console.log(calculateBMI(56, 1.60)) \ 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..7d1027786e 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -5,6 +5,11 @@ // Implement a function that: +function upperSnakeCase(srt){ + return srt.toUpperCase().replaceAll(" ", "_") +} +console.log(upperSnakeCase("hello there")) + // Given a string input like "hello there" // When we call this function with the input string // it returns the string in UPPER_SNAKE_CASE, so "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 6265a1a703..bc08ee4334 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,29 @@ // 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 convertToPound(srt) { + const penceStringWithoutTrailingP = srt.substring(0, srt.length - 1); + + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); + + return pounds; +} + +function convertToPence(srt) { + const penceStringWithoutTrailingP = srt.substring(0, srt.length - 1); + + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + return pence; +} + +console.log(`£${convertToPound("399p")}.${convertToPence("399p")}`); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..27bed3b619 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -9,30 +9,36 @@ function pad(num) { function formatTimeDisplay(seconds) { const remainingSeconds = seconds % 60; const totalMinutes = (seconds - remainingSeconds) / 60; + const remainingMinutes = totalMinutes % 60; const totalHours = (totalMinutes - remainingMinutes) / 60; 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 +// =============> +// the function formatTImeDisplay calls 3 times the pad function -pad(totalHours) pad(remainingMinutes) pad(remainingSeconds) // 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 +// =============> the number 0 = which is the calculation of const totalHours = (totalMinutes - remainingMinutes) / 60; // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> "00" = the function pad(-receives the number 0 or the value of totalHours) and converts this to a string = +// than the while loop kicks in and runs while the conditions is true and exit once it returns as false +// numString = "0" + numString; this line will add a a "0" to our new string if our condition is true - (numString.length < 2) // 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 +// =============> number 1 - the calculation of const remainingSeconds = seconds % 60; // 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" pad(1) - converts to a string "1" and adds the "0" + with the while loop to our newly created string diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..6a8844a9c5 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -1,25 +1,58 @@ // This is the latest solution to the problem from the prep. // 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 pad(num) { + let numString = num.toString(); + while (numString.length < 2) { + numString = "0" + numString; + } + return numString; +} function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + const minutes = time.slice(3, 5); + if (hours === 0) { + return `12:${minutes} am`; + } else if (hours > 12) { + return `${pad(hours - 12)}:${minutes} pm`; + } else if (hours === 12) { + return `12:${minutes} pm`; } - return `${time} am`; + return `${pad(hours)}:${minutes} am`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +const currentOutput = formatAs12HourClock("06:00"); +const targetOutput = "06:00 am"; console.assert( currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}` ); -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; +const currentOutput2 = formatAs12HourClock("12:00"); +const targetOutput2 = "12:00 pm"; 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("22:59"); +const targetOutput4 = "10:59 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +const currentOutput5 = formatAs12HourClock("21:30"); +const targetOutput5 = "09:30 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +);