diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..45eef2de2 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,6 +1,6 @@ // Predict and explain first... // =============> write your prediction here - +// the str is already declared as a parameter of the function, so we cannot declare it again inside the function. This will throw a syntax error as we can't declare a variable with the same name as a parameter in the same scope. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +10,10 @@ function capitalise(str) { } // =============> write your explanation here +// the error is occurring because we are trying to declare a variable with the same name as a parameter in the same scope. This is not allowed in JavaScript and will throw a syntax error. To fix this, we can simply remove the let keyword and just reassign the value to the str parameter directly, like this: + // =============> 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..65deddda7 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -2,6 +2,7 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// the error will occur because we are trying to declare a variable with the same name as a parameter in the same scope. Another error is that we are trying to log a variable that is not defined in outer scope, but only in the function scope. // Try playing computer with the example to work out what is going on @@ -15,6 +16,16 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here - +// the first step is the function is defined and reads the function as it appears. +// the second step is to run the function if it is called, but it is not called in this case, only the console.log is called, which is trying to log a variable that is not defined in the global scope, and hence this will throw a reference error first. But because the function is not called, the syntax error of declaring a variable with the same name as a parameter will not be thrown until the function is called. // 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(0.5)); +*/ diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..713f79cef 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,26 @@ // Predict and explain first BEFORE you run any code... - +// it will throw a syntax error because we are trying to put a number in the parameter of the function where it should be only a variable name. // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here - +// this error will occur because a number is being used as a parameter name in the function definition, which is not allowed in JavaScript. Parameter names must be valid variable names, and numbers cannot be used as variable names. Therefore, the code will throw a syntax error when it is run. Another error is that the variable num is not defined in the function, so it will throw a reference error when trying to return num * num. function square(3) { return num * num; } // =============> write the error message here - +// Uncaught SyntaxError: Illegal return statement +// Uncaught SyntaxError: Unexpected number // =============> explain this error message here - +// This error message indicates that there is a syntax error in the code that entails both an illegal return statement and an unexpected number. The illegal return statement error occurs because the function is trying to return a value from a function that is not properly defined. The unexpected number error occurs because the parameter name of the function is a number, which is not allowed in JavaScript. // Finally, correct the code to fix the problem // =============> write your new code here +/* +function square(num) { + return num * num; +} +*/ diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..a172a940c 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,7 +1,7 @@ // Predict and explain first... // =============> write your prediction here - +// as we are not returning anything from the function, it is not possible to retrieve the returned value from this function, so it will throw undefined. function multiply(a, b) { console.log(a * b); } @@ -9,6 +9,11 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here - +// however when the function is run, it displays 320 and undefined next to the The result of multiplying 10 and 32. So obviusly the 320 is displayed on the console, because that's the output of the function, but it is undefined when we try to retrieve it for use in the template literals. so we need to return the value inside a function in order to use it outside. // 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..763339a83 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,6 +1,6 @@ // Predict and explain first... // =============> write your prediction here - +// the function is returned without calculating anything, and as we know after return, the function stops executing and anything after return is not executable. so it will throw undefined. function sum(a, b) { return; a + b; @@ -9,5 +9,11 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// the function has to also return a value or the output in order to use this same value in the global scope. // 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..57dd88ece 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -2,7 +2,7 @@ // Predict the output of the following code: // =============> Write your prediction here - +// the num is declared in the global scope and the function will use it despite when it is called with different argruments and thus will bring the result to become only 3. const num = 103; function getLastDigit() { @@ -15,10 +15,20 @@ 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 in all the 3 function calls // Explain why the output is the way it is // =============> write your explanation here +// because it is the global scope declaration of num the function will use it as it doesn't have its own num declared inside. // Finally, correct the code to fix the problem // =============> write your new code here +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)}`); +// the num can be declared as a parameter so as the function can work for any argument passed to it. // 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-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..bf68b2e32 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/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 + return (weight / (height * height)).toFixed(1); } +console.log(calculateBMI(75, 1.85)); diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..8fd79f4db 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,8 @@ // 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 capitaliseStrings(str) { + return str.toUpperCase().replaceAll(" ", "_"); +} +console.log(capitaliseStrings("yonatan teklemariam weldelslassie")); diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..4727e3cd0 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/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("123p")); // £1.23 +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("5p")); // £0.05 +console.log(toPounds("50p")); // £0.50 +console.log(toPounds("1000p")); // £10.00 diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..91217313e 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -22,17 +22,18 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here - +// pad will be called 3 times, once for each of the totalHours, remaininngMinutes, and remainingSeconds variables that are passed to the pad function in the return statement of formatTimeDisplay. // 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 value assigned to num when pad is called for the first time is 0, which is the value of totalHours when formatTimeDisplay is called with an input of 61. This is because 61 seconds is equal to 1 minute and 1 second, which means there are 0 hours, 1 minute, and 1 second. Therefore, totalHours is 0, and this value is passed to the pad function as the argument num in return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;}. // c) What is the return value of pad when it is called for the first time? // =============> write your answer here - +// the return value of pad when it is called for the first time 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 - +// when pad is called for the last time in this program, the value assigned to num is 1, which is the value of remainingSeconds. // 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 +// the return value of pad when it is called for the last time in this program is "01". diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..6d4ad23b1 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -4,22 +4,78 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`; + } + if (hours === 12) { + return `${hours}:${minutes.toString().padStart(2, "0")} pm`; + } + if (hours === 0) { + return `12:${minutes.toString().padStart(2, "0")} am`; } - return `${time} am`; + return `${hours}:${minutes.toString().padStart(2, "0")} am`; } const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +const targetOutput = "8: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}`, +); +const currentOutput3 = formatAs12HourClock("15:30"); +const targetOutput3 = "3:30 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}`, +); +const currentOutput4 = formatAs12HourClock("12:10"); +const targetOutput4 = "12:10 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}`, +); +const currentOutput5 = formatAs12HourClock("08:45"); +const targetOutput5 = "8:45 am"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}`, +); +const currentOutput6 = formatAs12HourClock("09:05"); +const targetOutput6 = "9:05 am"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}`, +); +const currentOutput7 = formatAs12HourClock("17:45"); +const targetOutput7 = "5:45 pm"; +console.assert( + currentOutput7 === targetOutput7, + `current output: ${currentOutput7}, target output: ${targetOutput7}`, +); +const currentOutput8 = formatAs12HourClock("00:00"); +const targetOutput8 = "12:00 am"; +console.assert( + currentOutput8 === targetOutput8, + `current output: ${currentOutput8}, target output: ${targetOutput8}`, +); +const currentOutput9 = formatAs12HourClock("11:59"); +const targetOutput9 = "11:59 am"; +console.assert( + currentOutput9 === targetOutput9, + `current output: ${currentOutput9}, target output: ${targetOutput9}`, +); +const currentOutput10 = formatAs12HourClock("23:59"); +const targetOutput10 = "11:59 pm"; +console.assert( + currentOutput10 === targetOutput10, + `current output: ${currentOutput10}, target output: ${targetOutput10}`, );