diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..4515b71c7 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,25 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here :there is a syntax error because string is decleared two tiimes. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +//while calling a function capitalise occured a error. The error is occured due to declared of Str two times. + +// /*function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +// } +// capitalise("Ramesh");*/ -function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; -} // =============> write your explanation here +// to resolve this error we need to declared a new variable in step 9. after declaring a new variable it runs and give outputs. +//which first letter is capital and other are remain same. // =============> write your new code here +function capitalise(str) { + let ab = `${str[0].toUpperCase()}${str.slice(1)}`; + return ab; +} +capitalise("ramesh"); +// The correct code is given above i pass a value ramesh after running this function the output become Ramesh. + diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..3630a2561 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,20 +1,30 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> I think when we run the programme it will show the type error because decimal number is reassigning inside the function. // 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 - +// while running this function is shows SyntaxError because decimal number variable is declared two times which JavaScript does not allow. +// and in step 15 we need call function name not decimal number. // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +console.log(convertToPercentage(0.5)); +// After solving this the output is 50%. diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..825c45061 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,30 @@ // Predict and explain first BEFORE you run any code... +// It show the reference error cause function not assigned the variable so that, code is stuck. // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +// } // =============> write the error message here +// the message is SyntaxError: Unexpected number. // =============> explain this error message here +// The error message is occured due to not assigning the variable in function. TO solve this problems we need to assign the num variable in function. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} + +console.log(square(3)); +// The output of this programme is 9. + diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..f0efc31b1 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,22 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here : In step six we need to write return type and output become 320. -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 +// =============> write your explanation +// There should be return but code is perfectly run without changing the return type instead of console.log in line 6. -// Finally, correct the code to fix the problem +// Finally, correct the code to fix the problem +// In the section there has no output error but console is for getting output in print but in function there should be a return type so that after correcting this code +// the output become The result of multiplying 10 and 32 is 320. // =============> 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)}`); \ No newline at end of file diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..c055291bf 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,20 @@ // Predict and explain first... // =============> write your prediction here +// As per this question the answer is not defined but answer should be 42. +// 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 +//In this question return type has no value so we need to put a+b in step 5. The step 5 will be return a+b;. // 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)}`); +// After correcting this code we get the exact output 42. Without correction the output is The sum of 10 and 32 is undefined. diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..4aa9eefcc 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,24 +1,40 @@ -// Predict and explain first... +// Predict and explain first +// As per the code output should be the last digits of the given number . // Predict the output of the following code: // =============> Write your prediction here +// +// The output always be 3 3 3 3 3 3 3 but i thought output should be the last digits. -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 +// The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 3 // Explain why the output is the way it is // =============> write your explanation here +// In this variable num is constant and there is no any declaration in function name. Due to this reasons result always be 3. // 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. +// This program should tell the user the last digit each number. // Explain why getLastDigit is not working properly - correct the problem +// To solve this problems we need to write variable in function. diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..f5fcf2653 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -16,4 +16,9 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height + let squared_height = height * height; + const bmi = weight/squared_height; + return bmi.toFixed(1); } +console.log(`The BMi of the person is ${calculateBMI(70,1.73)}`); +// The output of the program is 'The BMi of the person is 23.4'. \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..bc1cbe00c 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 toUpperSnakeCase(str){ + return str.replaceAll(" ", "_").toUpperCase(); +} +console.log(toUpperSnakeCase("hello there")); +// The output of the given code is HELLO_THERE \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..d9bc919a0 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,20 @@ // 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("399p")); +console.log(toPounds("2p")); diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..1bd46e36c 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -14,6 +14,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 @@ -22,17 +23,24 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// The pad is called 3 times in step 15. // 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 which is assigned to num when pad is called for the first time is 0. // c) What is the return value of pad when it is called for the first time? // =============> write your answer here +// The value of pad when it is called for the first time "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 +// The value assign to num is 1. +// because the last call to pad is pad(remainingSeconds), and remainingSeconds is 1 +// when formatTimeDisplay(61) is called. // 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 is "01" because the last call to pad is pad(1). +// The pad function add a zero to make the number two digits. \ No newline at end of file