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..edb5afd458 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,19 +2,47 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// There're two errors. +// First Error will be, the function having a parameter decimalNumber, and we are trying to declare another variable with same which we can't be able to do. +// And decimalNumber is a local variable , we cannot use it outside of the function/scope. + // 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) { + decimalNumber = 0.5; + const percentage = `${decimalNumber * 100}%`; + return percentage; +} +const decimalNumber= 0.9; +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 function convertToPercentage(decimalNumber) -> we have created a function with the parameter decimalNumber. +// Inside the function, `decimalNumber` is already declared as a local variable because it is a parameter. +// We can't declare another variable with the same name using `const` in the same scope. +// we need to assign a value to the variable. +// Outside of the function we are trying to print the value of decimalNumber but we haven't declare the variable nor assign a value. +// And decimalNumber is a new variable as its outside of the scope now, so if we want we can create a variable with the name used before in a function +// because its in different scope. +// I have created a const variable and assigned a value to it. +// And lastly, I have called function and passed decimalNumber as an argument and print the value what's function is returning. +// The percentage is calculated by decimalNumber = 0.5 not 0.9 +// As we passed decimalNumber as an argument with the value of 0.9 but inside the function we are assigning a value to the +// decimalNumber, its value will be overwritten with parameter's local value and percentage will be calculated with decimalNumber=0.5 +// If we don't assign a value inside the function then percentage will be calculated on the basis of the argument we passed, +// though outside of const decimalNumber will be unchanged and remained 0.9. \ 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..f0ad21966e 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 / height; + 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..d23b87cd84 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,17 @@ // 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 SnakeUpperCase(str){ + var result = str.toUpperCase(); + let strLength = result.length; + for (let i=0; i 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..607d9dd874 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -23,3 +23,31 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); +const currentOutput3 = formatAs12HourClock("00:00"); +const targetOutput3 = "00:00 am"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("24:00"); +const targetOutput4 = "12:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +const currentOutput5 = formatAs12HourClock("12:00"); +const targetOutput5 = "12:00 am"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput}` +); + +const currentOutput5 = formatAs12HourClock(":00"); +const targetOutput5 = "12:00 am"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput}` +); +