diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..ac455e67d 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,24 @@ // Predict and explain first... -// =============> write your prediction here +/* =============> I predict that the function would make 1st character uppercase and +then adds the rest of the string using slice. */ // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +//We cannot declare str again because it has already been declared as a parameter of the function. + +/* OLD CODE + function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } + */ // =============> write your explanation here // =============> write your new code here + +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..cb5e75d5a 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,10 +1,11 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> We will get a SyntaxError as decimalNumber has already been declared as a parameter of the function. // Try playing computer with the example to work out what is going on +/* function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -12,9 +13,17 @@ function convertToPercentage(decimalNumber) { return percentage; } -console.log(decimalNumber); +console.log(decimalNumber); */ // =============> write your explanation here +// We don't need to redeclare decimalNumber as it's value comes from function parameter, +// also console.log wont work as decimalNumber is only created inside the function. // 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)); diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..1b52a029c 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -3,18 +3,22 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> SyntaxError because function parameter must be a name not a number or value. The error occurs because 3 is a number. +/* function square(3) { return num * num; -} +} */ -// =============> write the error message here +// =============> SyntaxError: Unexpected number -// =============> explain this error message here +// =============> This error message is cause by trying to assign a number as a parameter, a parameter needs to be identifier such as num. // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} +console.log(square(5)) diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..5b64d853b 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,24 @@ // Predict and explain first... -// =============> write your prediction here +// =============> We will log the result in a console but function won't return it as a value. +// console.log only displays the result, it does not return it from the function. +/* function multiply(a, b) { console.log(a * b); } + console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +*/ -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +//We need to return a * b so the result can be used where the function is called. + // 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..38b99b4ed 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,20 @@ // Predict and explain first... -// =============> write your prediction here - +// =============> return on its own means that the function ends without returning a value, so the result is undefined. +/* function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +*/ // =============> write your explanation here +// return on it's own means that the function wont refer to parameters and will come up as undefined. a+ b is never reached because it comes after 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)}`); diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..90e6423c4 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,8 +1,11 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 3 +/* const num = 103; function getLastDigit() { @@ -12,13 +15,31 @@ 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 +//The output is this way as there is a num assigned before the function and function don't use the parameter num, +//so the output would only be the return of slice value for const num before 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 +//Function was not working correctly as it was returning num slice for const num that was assigned before the function, function should use parameter num to work correctly. \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..e9c9f02b4 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,9 @@ // It should return a string of their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height + let bmiNum = weight / (height * height) + bmiNum=bmiNum.toFixed(1) + return bmiNum } + +console.log(calculateBMI(70,1.73)); diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..5414a655b 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/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 + +const str= "hello there" + +function strToUpperCase(str) { + return str.toUpperCase().replaceAll(" ","_") +} +console.log(strToUpperCase("lord of the rings")); \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..358f827ce 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,46 @@ // 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 + +// In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js + +// You will need to take this code and turn it into a reusable block of code. +// 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"; + +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"); + +console.log(`£${pounds}.${pence}`); +*/ + +function toPounds(penceString) { + let noP = penceString.substring(0,penceString.length -1) + let paddedP = noP.padStart(3,"0") + let pounds= paddedP.substring(0, paddedP.length -2) + + let pence= paddedP.substring(paddedP.length -2).padEnd(2,"0") + + return `£${pounds}.${pence}` +} +console.log(toPounds("399p")) +console.log(toPounds("5p")) +console.log(toPounds("52p")) +console.log(toPounds("1244p")) \ 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..cc5a52b36 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -15,24 +15,26 @@ 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 +// =============> pad function will be called 3 times because it's been called 3 times in return statement. // 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 +// =============> Starting value is 0 // c) What is the return value of pad when it is called for the first time? -// =============> write your answer here +// =============> returning value of pad when its called for the 2nd time is "00" :numString = "0" + numString: // 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 +// =============> When pad is called for the last time it's value is 1, The last call is pad(remainingSeconds), and remainingSeconds 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 +// =============> // Return value is "01", num is 1, which becomes "1". Since its length is less than 2, the while loop adds "0" to the beginning, making it "01". diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..57d853ff4 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -4,8 +4,13 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = Number(time.slice(3, 5)); if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`; + } else if (hours === 12) { + return `${hours}:${minutes.toString().padStart(2, "0")} pm`; + } else if (hours === 0) { + return `12:${minutes.toString().padStart(2, "0")} am`; } return `${time} am`; } @@ -14,12 +19,63 @@ const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 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}`, ); + +formatAs12HourClock("19:00"); +let output = formatAs12HourClock("19:00"); +let target = "07:00 pm"; +console.assert( + output === target, + `current output: ${output}, target output: ${target}`, +); +console.log(formatAs12HourClock("19:00")); +// //works +formatAs12HourClock("9:00"); +output = formatAs12HourClock("9:00"); +target = "9:00 am"; +console.assert( + output === target, + `current output: ${output}, target output: ${target}`, +); +console.log(formatAs12HourClock("9:00")); +// //works +formatAs12HourClock("12:00"); +output = formatAs12HourClock("12:00"); +target = "12:00 pm"; +console.assert( + output === target, + `current output: ${output}, target output: ${target}`, +); + +console.log(formatAs12HourClock("12:00")); +//Function needs else if statement to includes code behavior when "12:00" will be the argument value. + +console.log(formatAs12HourClock("9:00")); +// //works +formatAs12HourClock("00:00"); +output = formatAs12HourClock("00:00"); +target = "12:00 am"; +console.assert( + output === target, + `current output: ${output}, target output: ${target}`, +); +console.log(formatAs12HourClock("00:00")); +//Function needs else if statement to includes code behavior when "00:00" will be the argument value. + +formatAs12HourClock("19:37"); +output = formatAs12HourClock("19:37"); +target = "07:37 pm"; +console.assert( + output === target, + `current output: ${output}, target output: ${target}`, +); +console.log(formatAs12HourClock("19:37")); +//For the test to pass I had to create a minutes variable to extract the minutes from the input. When minutes were converted to a number, zero at the start of the string was removed, so padStart(2, "0") was used to add it back.