diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..fe55c658c6 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// =============> my prediction is that this code is not functioning because of that it declared parameter in the function. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -9,5 +9,9 @@ function capitalise(str) { return str; } -// =============> write your explanation here -// =============> write your new code here +// =============> write your explanation here : the error message is indicating that str already declared in the parameter and we can't redeclare it. +/* =============> write your new code here + function capitalise(str) { + const capitalizeFirstLetter =`${str[0].toUpperCase()}${str.slice(1)}`; + return capitalizeFirstLetter; + } */ diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..2cf976d9c6 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,7 +1,8 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> write your prediction here : /* my prediction is that parameter was declared inside the function +// and also local variable can not be logged globally. */ // Try playing computer with the example to work out what is going on @@ -14,7 +15,16 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +// =============> write your explanation here : /* in this code the computer first read the function and +// and store the variables and try to execute the function in case of the +// above code the computer first take the parameter and do the next step +// it reads the const decimalNUmber that is error so it will execute +// identifier error. */ // Finally, correct the code to fix the problem -// =============> write your new code here +//=============> write your new code here : /*let newDecimalNumber = 0.5; +// function convertToPercentage(decimalNumber) { +// const percentage = `${decimalNumber * 100}%`; +// return percentage; +// } +// console.log(convertToPercentage(newDecimalNumber)); */ diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..593f0fb98c 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,24 @@ // Predict and explain first BEFORE you run any code... - +// the function parameter is assigned to constant number the result will be variable error. // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> write your prediction of the error here : variable error. function square(3) { return num * num; } // =============> write the error message here +//syntaxError: Unexpected number // =============> explain this error message here +// the error message is explaining that the parameter is assigned to constant number rather than variable. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> write your new code here : function square(num){ +// return num * num; +// } diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..1bce89bf8a 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here : this code didn't declare the two global variables and the result will be undefined. function multiply(a, b) { console.log(a * b); @@ -8,7 +8,13 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here - +// =============> write your explanation here : the code have two number variables but they are not declared global in this case the +// global console.log have undefined value // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> write your new code here let a = 10; +// let b = 32; +// function multiply(a, b) { +// (a * b); +// } + +// console.log(`The result of multiplying 10 and 32 is ${(a * b)}`); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..19499f81fe 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here: the two number variables are not declared and return is not defined. function sum(a, b) { return; @@ -8,6 +8,13 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here: two number values 10 and 32 are not defined global and the return is not assigned to any +// return function. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> write your new code here : let a = 10; +// let b = 32; +// function sum(a, b) { +// a + b; +// } + +// console.log(`The sum of 10 and 32 is ${a + b}`); diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..32be0e14ea 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,7 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> Write your prediction here: my prediction is that the code will log value of 3 because it have constant variable. const num = 103; @@ -14,11 +14,23 @@ 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 +// =============> write the output here : all the console.log values are 3. + // Explain why the output is the way it is -// =============> write your explanation here +// =============> write your explanation here:it is because the global variable is constant number 103. + // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> write your new code here : let firstNum = 42; +// let secondeNum = 105; +// let thirdNum = 806; + +// 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..f29affcc73 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -13,7 +13,12 @@ // Given someone's weight in kg and height in metres // Then when we call this function with the weight and height // It should return their Body Mass Index to 1 decimal place - +let weight = 70; +let height = 1.73; function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file + // return the BMI of someone based off their weight and height + const squaringHeight = height * height; + const resultBMI = weight / squaringHeight; + return Number(resultBMI.toFixed(1)); +} +console.log(calculateBMI(weight, height)); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..a54a8eb8ee 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,9 @@ // 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 upperSnakeCase(inputString) { + return inputString.toUpperCase().split(" ").join("_"); +} + +console.log(upperSnakeCase("lord of the rings")); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..efb959df46 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,25 @@ // 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("1p")); +console.log(toPounds("23p")); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8c..df34ba6d45 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -10,25 +10,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 +// =============> 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 +// =============> write your answer here :0 // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> 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 +// =============> write your answer here: 1 because remainingSeconds of the return value is the last time pad will be called for console. // 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 +// =============> write your answer here: the return value will be "01" because 61 seconds has 1 second left after converting to minutes. diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..5be8b4af2d 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,22 +4,50 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(-2); if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${String(hours - 12).padStart(2, "0")}:${minutes}pm`; + } else if (hours == 12) { + return `12:${minutes}pm`; + } else if (hours === 0) { + return `12:${minutes}am`; } - return `${time} am`; + return `${String(hours).padStart(2, "0")}:${minutes}am`; } - -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +let currentOutput = formatAs12HourClock("00:01"); +let targetOutput = "12:01am"; console.assert( currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}` ); -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; +const currentOutput2 = formatAs12HourClock("11:59"); +const targetOutput2 = "11:59am"; console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); +const currentOutput3 = formatAs12HourClock("12:00"); +const targetOutput3 = "12:00pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); +currentOutput = formatAs12HourClock("00:00"); +targetOutput = "12:00am"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); +currentOutput = formatAs12HourClock("04:12"); +targetOutput = "04:12am"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); +currentOutput = formatAs12HourClock("16:12"); +targetOutput = "04:12pm"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +);