diff --git a/Sprint-2/debug/0.js b/Sprint-2/debug/0.js index b46d471a8..2839db6e4 100644 --- a/Sprint-2/debug/0.js +++ b/Sprint-2/debug/0.js @@ -1,7 +1,12 @@ // Predict and explain first... +//* When we execute this code, it calls the multiply function , we will have the log of the function by console.log inside of the +// function >(320). However because the function doesn't have return statement so the value in the template literal will be undefined. + + +//* To fix this we can put return statement and return the value to the template literal. function multiply(a, b) { - console.log(a * b); + return (a * b); } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); diff --git a/Sprint-2/debug/1.js b/Sprint-2/debug/1.js index df4020cae..95c55bce9 100644 --- a/Sprint-2/debug/1.js +++ b/Sprint-2/debug/1.js @@ -1,8 +1,11 @@ // Predict and explain first... +//* When this code run we will call the sum function with two parameters (a and b) by the template literal, inside of the function +// when return statement execute code will exit the function and any statement after return won't execute (line a + b) so it will ignore +// so value in the template literal would be undefined. +//* To fix this we need to pass the sum in the return statement function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); diff --git a/Sprint-2/debug/2.js b/Sprint-2/debug/2.js index bae9652a8..e5ead41e0 100644 --- a/Sprint-2/debug/2.js +++ b/Sprint-2/debug/2.js @@ -1,8 +1,11 @@ // Predict and explain first... +//* When we execute the code it will return the last digit of number 103 > 3 for all of number because num variable is defined in global +// scop and anytime we call the function it consider value of 103 for variable num rather than parameters given in template literal. -const num = 103; +//* To fix this we can remove fixed num (global variable) and define parameter with a valid name for the getLastDigit function -function getLastDigit() { + +function getLastDigit(num) { return num.toString().slice(-1); } @@ -12,3 +15,5 @@ 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 +//* Because the fixed variable num, every time we call the function it consider the value 3 that it's fixed and global. So it doesn't +// change when we call the function with different number , all the result (last digit) will be the same (3). \ No newline at end of file diff --git a/Sprint-2/errors/0.js b/Sprint-2/errors/0.js index 74640e118..1aedb5dfe 100644 --- a/Sprint-2/errors/0.js +++ b/Sprint-2/errors/0.js @@ -1,9 +1,19 @@ // Predict and explain first... +//*The capitalise function takes a string as input (str parameter here) and then will make the first letter of the string, capital but this +// *code has an error because it declared str inside of the function. + // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +//*"SyntaxError: Identifier 'str' has already been declared" +//* The reason that we have this error is we declared "str" as a parameter for the function and then inside the function we tried to +//* declare it again that is wrong! For fixing this problem we can remove let and then reassign the value. + function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } +let test = capitalise("hello") +console.log(test) // OUTPUT ; "Hello" + diff --git a/Sprint-2/errors/1.js b/Sprint-2/errors/1.js index 4602ed237..c64173587 100644 --- a/Sprint-2/errors/1.js +++ b/Sprint-2/errors/1.js @@ -1,13 +1,20 @@ // Predict and explain first... +//* This code will have errors like Syntax errors, reference error ... // Why will an error occur when this program runs? -// Try playing computer with the example to work out what is going on +//* 1. "SyntaxError: Identifier 'decimalNumber' has already been declared" > decimalNumber is declared as a parameter for the function +// but it declared again inside of the function that it cause syntax error. +//* 2. "ReferenceError: decimalNumber is not defined" > decimalNumber value is defined inside of the function so it is in a local scop +// then there is no access to it outside of the function so we will see reference error. +// Try playing computer with the example to work out what is going on +//* For fixing this problem we need to remove second declaration of decimalNumber and use it as a parameter directly. +//* Then we need to call the function in console.log to see the result. So we can put the value of 0.5 as a parameter in the function +// and call it function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); +console.log(convertToPercentage(0.5)); // OUTPUT ; "50%" diff --git a/Sprint-2/errors/2.js b/Sprint-2/errors/2.js index 814334d9e..3a88cf5e9 100644 --- a/Sprint-2/errors/2.js +++ b/Sprint-2/errors/2.js @@ -1,10 +1,14 @@ // Predict and explain first... +//* When we execute the code we will have Syntax error because of the number 3 in the function. We can not define a parameter value +// in the function, we are allowed to pass parameter name. Also num inside of the function is not defined and will show reference error // this function should square any number but instead we're going to get an error +//* To fix this code we need to remove number 3 and pass the parameter name "num". Also we need to call function, we can print it with +// console.log -function square(3) { +function square(num) { return num * num; } - +console.log(square(3)); // OUTPUT ; 9 diff --git a/Sprint-2/extend/format-time.js b/Sprint-2/extend/format-time.js index f3b83062d..23c330135 100644 --- a/Sprint-2/extend/format-time.js +++ b/Sprint-2/extend/format-time.js @@ -2,12 +2,34 @@ // 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)); + const hours = Number(time.slice(0, 2)); // extract hours + const minutes = (time.slice(3,5)); // extract minutes + + if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59 || isNaN(hours) || isNaN(minutes)) { + return null; // check invalid input and return null + } + + // For (00:00 am) + if (hours === 0) { + return `12:${minutes} am`; + } + + // For (12:00 pm) + if (hours === 12) { + return `12:${minutes} pm`; + } + + // For afternoon pm if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${String(hours - 12).padStart(2, '0')}:${minutes} pm`; } + + // For morning am + else{ return `${time} am`; } + } + const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; @@ -16,9 +38,50 @@ console.assert( `current output: ${currentOutput}, target output: ${targetOutput}` ); -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; +const currentOutput2 = formatAs12HourClock("23:01"); +const targetOutput2 = "11:01 pm"; console.assert( currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` + `current output2: ${currentOutput2}, target output2: ${targetOutput2}` +); + +const currentOutput3 = formatAs12HourClock("12:00"); +const targetOutput3 = "12:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output:3 ${currentOutput3}, target output3: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("18:10"); +const targetOutput4 = "06:10 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output4: ${currentOutput4}, target output4: ${targetOutput4}` +); + +// For invalid +const currentOutput5 = formatAs12HourClock("24:00"); +const targetOutput5 = null; +console.assert( + currentOutput5 === targetOutput5, + `current output5: ${currentOutput5}, target output5: ${targetOutput5}` +); + +// For (12:03 am) +const currentOutput6 = formatAs12HourClock("00:03"); +const targetOutput6 = "12:03 am"; +console.assert( + currentOutput6 === targetOutput6, + `current output6: ${currentOutput6}, target output6: ${targetOutput6}` +); + +// For (12:26 pm) +const currentOutput7 = formatAs12HourClock("12:06"); +const targetOutput7 = "12:06 pm"; +console.assert( + currentOutput7 === targetOutput7, + `current output7: ${currentOutput7}, target output7: ${targetOutput7}` ); + +console.log(formatAs12HourClock("19:00")); +console.log(formatAs12HourClock("18:00")); \ No newline at end of file diff --git a/Sprint-2/implement/bmi.js b/Sprint-2/implement/bmi.js index 259f62d48..e5d9a6476 100644 --- a/Sprint-2/implement/bmi.js +++ b/Sprint-2/implement/bmi.js @@ -13,3 +13,10 @@ // 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 + +function bodyMassIndex (weight, height){ // defining two parameters (weight and height) + let BMI = weight/Math.pow(height,2); // calculating BMI : Squaring height with Math.pow and then dividing wight by the height square + return BMI.toFixed(1); // returning BMI with 1 decimal place by toFixed method +} + +console.log(bodyMassIndex(70, 1.73)); // OUTPUT : 23.4 \ No newline at end of file diff --git a/Sprint-2/implement/cases.js b/Sprint-2/implement/cases.js index 9e56a27b6..ee18cdfd2 100644 --- a/Sprint-2/implement/cases.js +++ b/Sprint-2/implement/cases.js @@ -13,3 +13,16 @@ // You will need to come up with an appropriate name for the function // Use the string documentation to help you find a solution + + +function toUpperSnakeCase (str){ + + // Convert the string to uppercase and replace spaces with underscores + // str.replace(/ /gi, "_") > replace all of spaces with under score + //.toUpperCase() is a method that capitalize all letters + snakeCaseString = str.replace(/ /gi, "_").toUpperCase(); + return snakeCaseString; +} + +console.log(toUpperSnakeCase("hello there")); //OUTPUT: "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); //OUTPUT: "LORD_OF_THE_RINGS" \ No newline at end of file diff --git a/Sprint-2/implement/to-pounds.js b/Sprint-2/implement/to-pounds.js index 6265a1a70..aa7f86d2e 100644 --- a/Sprint-2/implement/to-pounds.js +++ b/Sprint-2/implement/to-pounds.js @@ -4,3 +4,27 @@ // 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){ + + //removing p from the end of the string + const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); + + //pad the string to be sure it has 3 digits and if not add 0 to the start + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + + //extract pound + const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); + + //extract pence + const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2); + + // return pound and pence in new format + return (`£${pounds}.${pence}`); +} + + console.log(toPounds("399p")); // OUTPUT: £3.99 + console.log(toPounds("4590p")); // OUTPUT: £45.90 + console.log(toPounds("34p")); // OUTPUT: £0.34 \ No newline at end of file diff --git a/Sprint-2/implement/vat.js b/Sprint-2/implement/vat.js index 3fb167226..0ae32ef38 100644 --- a/Sprint-2/implement/vat.js +++ b/Sprint-2/implement/vat.js @@ -8,3 +8,14 @@ // Given a number, // When I call this function with a number // it returns the new price with VAT added on + +function priceWithVAT(initialPrice){ + + // calculating new price with VAT : multiply initial price by 1.2 + let newPriceWithVAT = initialPrice * 1.2; + + //return new price with VAT + return newPriceWithVAT; +} + +console.log(priceWithVAT(50)); // OUTPUT: 60 \ No newline at end of file diff --git a/Sprint-2/interpret/time-format.js b/Sprint-2/interpret/time-format.js index c5a0c1619..8fea3c3c8 100644 --- a/Sprint-2/interpret/time-format.js +++ b/Sprint-2/interpret/time-format.js @@ -19,13 +19,19 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? +//* 3 times for totalHours, remainingMinutes and remainingSeconds. // Call formatTimeDisplay with an input of 61, now answer the following: +console.log(formatTimeDisplay(61)); // b) What is the value assigned to num when pad is called for the first time? +//* the first value assigned to num when pad is called is 0 > totalHours is 0 // c) What is the return value of pad is called for the first time? +//* first return value of pad is "00" > toString convert number to string and padStart(2, "0") make sure that it has two digits // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer +//*the last value assigned to num is 1 // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer +//* when we call pad(1), > it will convert to "1" and then padStart(2, "0") will change it to two digits so return value is "01" \ No newline at end of file