diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..6270f3139 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,24 @@ // Predict and explain first... -// =============> write your prediction here +// =============> I guess we would'nt need to say let in line 8 because str is already decleared as an argument +// within the function capitalise(str). +// // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +//function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +//} + +//capitalise("hello") +// =============> A syntaxError occured as 'str' is already declared. +// =============> write your new code here + 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 +console.log(capitalise("hello")); \ 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..6010e38c8 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,36 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here + +// =============> I think, the function convertToPercentage is not called at line 16, and +// there is no any importance of declaring cons decimalNumber as it is already declared. // 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); + +// =============> A syntaxError appeared indicating 'decimalNumber' has already been declared. + +// 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); +console.log(convertToPercentage(0.5)); +console.log(convertToPercentage(0.27)); +console.log(convertToPercentage(0.76)); -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here +// The new code run smoothly and retuned "50%" +// decimalNumber is now taken as a parameter. \ 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..46203eb73 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,23 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> The num should be inside the function square (num) not 3 and square function should be + // called later as square(3) -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +//} -// =============> write the error message here +// =============> function suqare(3) SyntaxError: Unexpected number -// =============> explain this error message here +// =============> 3 was not expected... instead a declaration or paramater like num was expected. // 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-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..9ca5d3300 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,22 @@ // Predict and explain first... -// =============> write your prediction here +// =============> In line 6 instead of console.log, I would expect retun a*b; the code will result in + // just printing a*b -function multiply(a, b) { - console.log(a * b); -} +// 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 +// =============> After running it returned 320 The result of multiplying 10 and 32 is undefined +// Because the multiply parameters were not taken as paramaters within a retunr. // 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 37cedfbcf..382a93f57 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,14 @@ // Predict and explain first... -// =============> write your prediction here +// =============> The semi colon ; after retunr will results in syntax error. 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 +// =============> Unlike my expectation the code run without error as "The sum of 10 and 32 is undefined" // Finally, correct the code to fix the problem // =============> write your new code here + +// we just need to delete the ; after return and bring a+b side by side as retun a+b; diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..70db4043d 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,11 +1,27 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> The code will first return 3 as string and after calling the function getLastDigit, + // it will not return anything because every number pased will be changed to string. -const num = 103; +// const num = 103; -function getLastDigit() { +// 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 three times.. +// // =============> my prediction was somehow closer but not exact, the case is parameter num needs to be with the getLastDifit +// function instead of const num = 103. +// Finally, correct the code to fix the problem +// =============> write your new code here + +function getLastDigit(num) { return num.toString().slice(-1); } @@ -13,12 +29,7 @@ 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 + +//the case is parameter num needs to be with the getLastDigit function instead of const num = 103. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..5bf9f78b7 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,16 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file + + const bmi = weight/(height*height); + return Number(bmi.toFixed(1)); + + // return the BMI of someone based on their weight and height +} + +console.log("The BMI is",calculateBMI(70, 1.73)); +console.log(typeof calculateBMI(70,1.73)); + +// As I see it in the console it looks like a number whilit was a string. +// the code has been updated to return a number as -----return Number(bmi.toFixed(1)); +// changing the string into number. \ 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..fd2f1c1f3 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,19 @@ // 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 capSnakeCase(str) { + + const upper = str.toUpperCase(); + const snake = upper.replace(/ /g, "_"); + + return snake; +} + +console.log(capSnakeCase("lord of the rings")); + +// Step-1 ----> Get a string +// Step-2 ----> change to upper case +// Step-3 ----> Replace space with underscore +// Step-4 ----> Return capSnakeCase \ 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..7021c10f8 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,20 @@ // 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")) +console.log(toPounds("5p")) +console.log(toPounds("5678p")) + diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..e01da2788 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,24 +11,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 is called three (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: the leftover second is assigned to num as remainingSeconds 61%60 // 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 +// =============> 01 the remainingSeconds from passed from pad to num is changed to string and padded as 01 diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..b7892c168 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -1,25 +1,96 @@ // 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. +// 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`; +// } +// return `${time} am`; +//} + +// const currentOutput = formatAs12HourClock("08:00"); +// const targetOutput = "08:00 am"; +// console.assert( +// currentOutput === 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}` +// ); + +// const currentOutput3 = formatAs12HourClock("00:00"); +// const targetOutput3 = "12:00 am"; +// console.assert( +// currentOutput3 === targetOutput3, +// `current output: ${currentOutput3}, target output: ${targetOutput3}` +// ); +// const currentOutput4 = formatAs12HourClock("12:00"); +// const targetOutput4 = "12:00 pm"; +// console.assert( +// currentOutput4 === targetOutput4, +// `current output: ${currentOutput4}, target output: ${targetOutput4}` +// ); + +// const currentOutput5 = formatAs12HourClock("13:00"); +// const targetOutput5 = "01:00 pm"; +// console.assert( +// currentOutput5 === targetOutput5, +// `current output: ${currentOutput5}, target output: ${targetOutput5}` + +// ); + +// const currentOutput7 = formatAs12HourClock("25:00"); +// const targetOutput7 = "01:00 am"; +// console.assert( +// currentOutput7 === targetOutput7, +// `current output: ${currentOutput7}, target output: ${targetOutput7}` + +// ) + +// console.assert(formatAs12HourClock("08:00") === "08:00 am"); +// console.assert(formatAs12HourClock("23:00") === "11:00 pm"); +// console.assert(formatAs12HourClock("22:00") === "10:00 pm"); + +// Modified Code: function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + let hours = Number(time.slice(0, 2)); + const minutes = time.slice(-2); + + let suffix; + if (hours >= 12) { + suffix = "pm"; + } else { + suffix = "am"; } - return `${time} am`; + + hours = hours % 12 || 12; // convert 0 to 12, 13 to 1 it returns the remainder + // for eg. taking 12/12 the remainder is 0 + // similarly if 13 is taken 13/12 , the remainder is 1, the operator || helps us to return the remainder after + // a division + + const formattedHours = hours.toString().padStart(2, "0"); + + return `${formattedHours}:${minutes} ${suffix}`; + } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +const currentOutput6 = formatAs12HourClock("23:00"); +const targetOutput6 = "11:00 pm"; console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; -console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` -); +) +console.log(currentOutput6) + + + +// Updated const minutes = time.slice(3) --- to const minutes = time.slice(-2) \ No newline at end of file