diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..2cc6ce4b16 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,20 @@ // Predict and explain first... // =============> write your prediction here - +// The Error will occur because we are trying to create a variable with same name in same scope. // 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)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } - +let str = 'hello'; +console.log(capitalise(str)); // =============> write your explanation here // =============> write your new code here +//By capitilise(str)-> we have created a function with parameter str. +// Inside the function, `str` is already declared as a local variable because it is a parameter. +// We can't declare another variable with the same name using `let` in the same scope. +// It was giving an error because we were trying to declare `str` again using `let`. +// I corrected it by removing `let` because we don't need to create another variable with the same name; we just have to assign a new value to the existing `str`. +// Outside the function, I created another variable with the same name, `str`. We can do this because it is in a different scope. diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..5633050255 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,19 +2,43 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// There're two errors. +//The first error occurs because the function has a parameter called decimalNumber, and we are trying to declare another variable with the same name using const. We cannot declare the same variable twice in the same scope. +//The second error occurs because decimalNumber is a local variable inside the function. Therefore, we cannot use it outside the function’s scope unless we declare another variable called decimalNumber outside the function. // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { +/*function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); +console.log(decimalNumber);*/ + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} +const decimalNumber= 0.9; // I have checked it by assigning different values, and it gives different output for different inputs. +console.log(decimalNumber); +console.log(convertToPercentage(decimalNumber)); // =============> write your explanation here // Finally, correct the code to fix the problem // =============> write your new code here + +/* +By writing function convertToPercentage(decimalNumber), we have created a function with a parameter called 'decimalNumber'. +Inside the function, decimalNumber is already declared as a local variable because it is a parameter. Therefore, we cannot declare another variable with the same name using const in the same scope. +However, we can assign a new value to the parameter without using const. For example: +decimalNumber = 0.5; +If we assign 0.5 to the parameter inside the function, the value passed as the argument will be overwritten inside that function call. Therefore, even if we pass 0.8 as the argument, the percentage will be calculated using 0.5, and the function will return 50%. +This reassignment only changes the local parameter inside the function. It does not change the const decimalNumber = 0.9 variable declared outside the function. Therefore, the outside variable will still remain 0.9. +In the original code, we also tried to print decimalNumber outside the function, but no variable with that name had been declared in the outside scope. This would cause a ReferenceError. +In the corrected code, I created a new const variable called decimalNumber outside the function and assigned it the value 0.9. This variable is different from the function parameter because they are in different scopes, so they can have the same name. +Finally, I removed the line that reassigned 0.5 to the parameter, so the function now uses the value passed as the argument. Since the value passed is 0.9, the function returns 90%. +Finally, console.log(decimalNumber) prints 0.9, and console.log(convertToPercentage(decimalNumber)) prints 90%. +*/ \ 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 aad57f7cfe..ecab91e3cd 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,23 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +//We cannot use number as a parameter. It should be any variable. -function square(3) { +/*function square(3) { return num * num; -} - +}*/ // =============> write the error message here - +//The error message is, 'Unexpected number'. // =============> explain this error message here - +// it says unexpected in terms, javaScript doesn't expect number/value as a parameter. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num){ + return num*num; +} + +const result = square(3); +console.log(result); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..dedba67684 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,21 @@ // Predict and explain first... // =============> write your prediction here - -function multiply(a, b) { +// In this example function is being called but as there's no return statement so it will return undefined. +// and a *b multiplication. and the line on line 10 +/*function multiply(a, b) { console.log(a * b); } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); - +*/ // =============> write your explanation here - +// I fixed it by adding a return statement and create another variable outside of the function that stores, +//value of function. // Finally, correct the code to fix the problem // =============> write your new code here +function multiply(a, b) { + return (a*b); +} +const result = multiply(10,32); +console.log(`The result of multiplying 10 and 32 is ${result}`); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..e600ba77ed 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,24 @@ // Predict and explain first... // =============> write your prediction here - -function sum(a, b) { +//It will give an error as we have to mention what are we returning, by simply putting the return statement doesn't automatically +//return anything, except undefined, and then we are doing an addition of two number but we haven't return the result of it, +//when the function will be called in line 11, it will print undefined with the sentence mentioned in literals. +/*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 +//I firstly fixed the return statement. +//outside of the function I create another variable that stores function value. + // Finally, correct the code to fix the problem // =============> write your new code here + +function sum(a, b) { + return a+b; +} +const result = sum(10,32); +console.log(`The sum of 10 and 32 is ${result}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..d9c662005e 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,8 +2,10 @@ // Predict the output of the following code: // =============> Write your prediction here - -const num = 103; +// we are using the constant variable num, and it is declared as a global variable. The function is declared with no +// parameter, but, while calling a function we're passing an argument. It can't use that argument as function is declare +// with no parameter so it will use the global variable whenever the function is being called. +/*const num = 103; function getLastDigit() { return num.toString().slice(-1); @@ -11,14 +13,27 @@ function getLastDigit() { 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 806 is ${getLastDigit(806)}`);*/ // Now run the code and compare the output to your prediction // =============> write the output here +// it's returning 3 for all three digits which is wrong for that digits // Explain why the output is the way it is // =============> write your explanation here +//This is happening because function is declare with no parameters as I predict and my prediction is totally right as javaScript +//doesn't allow to use argument when there's no parameters. // 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)}`); + // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem +// it wasn't working because we are passing argument without declaring function with a parameter and function was using +//global variable whenever the function is being called, so it was returning the last digit of that value stored in global variable. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..3910cf666b 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,12 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file + const squareHeight = height * height; + const div = weight / squareHeight; + const result = div.toFixed(1); + return result; +} +const height = 2.3; +const weight = 64; +const result = calculateBMI(weight, height); +console.log(result); \ 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 5b0ef77ad9..5bd8fa2c61 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,12 @@ // 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 ConvertintoSnakeUpperCase(str){ + return str + .toUpperCase() + .replaceAll(" ", "_"); +} +let sentence = "hello world i am maryam"; +let result = ConvertintoSnakeUpperCase(sentence); +console.log(result); \ 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 6265a1a703..19a42d79ee 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,29 @@ // 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"); + const result = `£${pounds}.${pence}`; + return result; +} +const penceString1 = toPounds("663p"); +const penceString2 = toPounds("98p"); +const penceString3 = toPounds("986p"); +const penceString4 = toPounds("9p"); +const penceString5 = toPounds("15p"); +const penceString6 = toPounds("100p"); +const penceString7 = toPounds("1p"); + + +console.log(penceString1); +console.log(penceString2); +console.log(penceString3); +console.log(penceString4); +console.log(penceString5); +console.log(penceString6); +console.log(penceString7); \ No newline at end of file diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..fe0d95a004 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -1,19 +1,21 @@ -function pad(num) { - let numString = num.toString(); - while (numString.length < 2) { +function pad(num) { //1)num=0, 2)num=0 3) num=1 + let numString = num.toString(); // (last time) numString = "1" + while (numString.length < 2) { //"01" it will return 01 numString = "0" + numString; } return numString; } -function formatTimeDisplay(seconds) { - const remainingSeconds = seconds % 60; - const totalMinutes = (seconds - remainingSeconds) / 60; - const remainingMinutes = totalMinutes % 60; - const totalHours = (totalMinutes - remainingMinutes) / 60; +function formatTimeDisplay(seconds) { + const remainingSeconds = seconds % 60; //61%60 = 1 + const totalMinutes = (seconds - remainingSeconds) / 60; //1-1=0 + const remainingMinutes = totalMinutes % 60; //0 + const totalHours = (totalMinutes - remainingMinutes) / 60; // 0 - return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; + 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 @@ -22,17 +24,21 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here - +//3 in console.log // 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)num=0, 2)num=0 3) num=1, when num is called for last time that is when pad(remainingSeconds), +//here remainingseconds value = 1 so num is assigned with value of 1 // 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 +//when pad called for the last time, the value of num is 1 +//it will convert into string by toString method "1" as its less than 2, it satisfies the while loop condition and, +// will concatenate with "0" the final value of variable numString would be "01", and will return "01". diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..82230b75e2 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,8 +4,17 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3,5); + if (hours === 0 || hours===24){ + return `12:${minutes} am`; + } + if (hours === 12){ + return `${hours}:${minutes} pm`; + } if (hours > 12) { - return `${hours - 12}:00 pm`; + const convertHours = hours - 12; + const formatHours = String(convertHours).padStart(2, "0"); + return `${formatHours}:${minutes} pm`; } return `${time} am`; } @@ -16,10 +25,57 @@ 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("24:00"); +const targetOutput4 = "12:00 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); +const currentOutput5 = formatAs12HourClock("12:00"); +const targetOutput5 = "12:00 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); +const currentOutput6 = formatAs12HourClock("13:30"); +const targetOutput6 = "01:30 pm"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +); +const currentOutput7 = formatAs12HourClock("15:45"); +const targetOutput7 = "03:45 pm"; +console.assert( + currentOutput7 === targetOutput7, + `current output: ${currentOutput7}, target output: ${targetOutput7}` +); +const currentOutput8 = formatAs12HourClock("20:15"); +const targetOutput8 = "08:15 pm"; +console.assert( + currentOutput8 === targetOutput8, + `current output: ${currentOutput8}, target output: ${targetOutput8}` +); +const currentOutput9 = formatAs12HourClock("12:30"); +const targetOutput9 = "12:30 pm"; +console.assert( + currentOutput9 === targetOutput9, + `current output: ${currentOutput9}, target output: ${targetOutput9}` +); +const currentOutput10 = formatAs12HourClock("00:30"); +const targetOutput10 = "12:30 am"; +console.assert( + currentOutput10 === targetOutput10, + `current output: ${currentOutput10}, target output: ${targetOutput10}` +); \ No newline at end of file