diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..20b7bc25a 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 +// =============> ANSWER +// It takes a string and change its first letter to capitial letter aida -----> Aida // 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; +// } + +// // =============> ANSWER +// SyntaxError: Identifier 'str' has already been declared +// It is not allow to redeclare the parameter str inside the function + +// // =============> write your new code here +let name = "aida"; + function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + return `${str[0].toUpperCase()}${str.slice(1)}`; } -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("aida")); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..ec4a2b7c1 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... +// =============> ANSWER +// convertToPercentage function takes a number and multiplaited it to 100,to give the percentage and then return it. +// but it doesn't work because we did'nt call it in the main part. +// Also we can not declare a variable which has been declared already // Why will an error occur when this program runs? -// =============> write your prediction here +// we can not declare a variable which has been declared already +// SyntaxError: Identifier 'decimalNumber' has already been 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}%`; +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; - return percentage; -} +// return percentage; +// } -console.log(decimalNumber); +// console.log(decimalNumber); -// =============> write your explanation here +// =============> ANSWER +// It doesn't work because we did'nt call the function. // Finally, correct the code to fix the problem + // =============> write your new code here + +const decimalNumber = 0.5; +function convertToPercentage(decimalNumber) { + 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 aad57f7cf..b9c2aa7b9 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,27 @@ - -// Predict and explain first BEFORE you run any code... +// This function does'nt work because we can not set literal value as a parameter. // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// An error for number instead of parameter -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +// } // =============> write the error message here +// SyntaxError: Unexpected number + // =============> explain this error message here +// JavaScript is expecting a variable name (parameter) not a literal value // 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-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..041332654 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,23 @@ // Predict and explain first... +// function should multiply a by b and print the answer. and it DOESN'T return . +// So in the Console.log we don't have the result. // =============> write your prediction here -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 +// we don't have result for function because there is no return in function . "The result of multiplying 10 and 32 is 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-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..d2e600991 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,22 @@ // Predict and explain first... // =============> write your prediction here +// The code doesn't work because the expression a + b comes after the return statement +// Once the compiler reaches return it exit the function. +// There is no error. we just don't have answer +// 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 +// There is no error. we just do not have correct answer.===> The sum of 10 and 32 is undefined // 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-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..5e07a7b78 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,43 @@ // Predict and explain first... -// Predict the output of the following code: // =============> Write your prediction here +// This code should return the last digit of each number but again it doesn't return a correct number because it ask to slice(-1) of "num" +// Predict the output of the following code: +// 3 +// 3 +// 3 -const num = 103; +// const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +// 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)}`); +// 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 +// 3 3 3 // Explain why the output is the way it is +// + // =============> write your explanation here +// because we didn't send our parameter to 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 diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..82f029b36 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,11 @@ // 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 + let squaredHeight = height * height; + let BMI = weight / squaredHeight; + BMI = Number(BMI.toFixed(1)); + return BMI; +} +let result = calculateBMI(70, 1.73); + +console.log(`The BMI for a person weighing 70kg and 1.73m tall is ${result}`); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..806ef19fb 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,11 @@ // 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 UPPER_SNAKE_CASE(NewWord) { + let UpperWord = NewWord.toUpperCase(); + let result = UpperWord.replaceAll(" ", "_"); + return result; +} + +console.log(UPPER_SNAKE_CASE("hello aida hope you are well")); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..67f227695 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,36 @@ // 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 = 0; +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 }; +} + +for (let i = 0; i < 5; i++) { + let randomNumber = Math.floor(Math.random() * 100001); + + PenceRandom = randomNumber + "p"; + + let result = toPounds(PenceRandom); + + console.log( + `Random: ${randomNumber}p → Pounds: £${result.pounds}.${result.pence}` + ); +} diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..40587a116 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -10,6 +10,7 @@ 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 @@ -17,18 +18,23 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> 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 +// =============> It is 0 // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> it is 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 +// =============> It is 1 +// The last time pad() is called, it's for the seconds value. Since remainingSeconds = 1, num is assigned 1. +// pad(1) returns "01" to format the time as hh:mm:ss // 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 last call to pad() formats the seconds. +// remainingSeconds is 1, so pad(1) is called.Inside pad, 1 becomes "1" (string), and since it's a single digit, it's padded to "01". +// So the return value is "01". diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..860038b90 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -3,15 +3,29 @@ // 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`; + const [hourStr, minuteStr] = time.split(":"); + let hours = Number(hourStr); + let suffix = "am"; + + if (hours === 0) { + hours = 12; + } else if (hours === 12) { + suffix = "pm"; + } else if (hours > 12) { + hours -= 12; + suffix = "pm"; } - return `${time} am`; + + const formattedHour = String(hours).padStart(2, "0"); + return `${formattedHour}:${minuteStr} ${suffix}`; } const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; +console.log( + `The current time is 08:00 in 24-hour style, and this is ${targetOutput} in 12-hour style.` +); + console.assert( currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}` @@ -19,7 +33,40 @@ console.assert( const currentOutput2 = formatAs12HourClock("23:00"); const targetOutput2 = "11:00 pm"; +console.log( + `The current time is 23:00 in 24-hour style, and this is ${targetOutput2} in 12-hour style.` +); console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("00:00"); +const targetOutput3 = "12:00 am"; +console.log( + `The current time is 00:00 in 24-hour style, and this is ${targetOutput3} in 12-hour style.` +); +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("13:40"); +const targetOutput4 = "01:40 pm"; +console.log( + `The current time is 13:40 in 24-hour style, and this is ${targetOutput4} in 12-hour style.` +); +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +const currentOutput5 = formatAs12HourClock("22:15"); +const targetOutput5 = "10:15 pm"; +console.log( + `The current time is 22:15 in 24-hour style, and this is ${targetOutput5} in 12-hour style.` +); +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +);