From b64f45a3ee92920a75796128280fe40a81d3a657 Mon Sep 17 00:00:00 2001 From: sab325 Date: Sun, 9 Feb 2025 15:00:12 +0000 Subject: [PATCH 1/6] sprint2 key-errors --- Sprint-2/1-key-errors/0.js | 21 +++++++++++++++------ Sprint-2/1-key-errors/1.js | 23 +++++++++++++++-------- Sprint-2/1-key-errors/2.js | 19 ++++++++++++------- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..6c824a5f7 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,22 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here: +// there is promblem declaring the string twice. // 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)}`; - return str; -} +//function capitalise(str) { + // let str = `${str[0].toUpperCase()}${str.slice(1)}`; + // return str; + +//} + +// =============> write your explanation here: It say's syntax error "str" has been declared twice. We can change variable name in tp text or anything else -// =============> write your explanation here // =============> write your new code here + +function capitalise(text) { + let capitalisedText = `${text[0].toUpperCase()}${text.slice(1)}`; + return capitalisedText; +} +console.log(capitalise("hello world")); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..4b11b4c25 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,27 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> write your prediction here: const decimalNumber = 0.5; this line should be declare outside of 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 +// =============> write your explanation here: In line 9 const decimalNumber = 0.5; should not be in their because it will redeclare in side function which will be error, it will be local scope if its inside a function. declaring outside of code make global scope, can call through out the code. // Finally, correct the code to fix the problem // =============> write your new code here +const decimalNumber = 0.5; +function convertToPercentage(decimalNumber){ + const percentage = `${decimalNumber*100}%`; + return percentage; + +} +console.log(decimalNumber); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..181ec7974 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,23 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> write your prediction of the error here: because parameter should use as num . -function square(3) { - return num * num; -} +//function square(3) { + // return num * num; +//} + +// =============> write the error message here: syntax error: unexpected number. -// =============> write the error message here +// =============> explain this error message here: We cannot use parameter as num like 3 or 4 in inside function as argument we should use num, so we can re call many times through out the code. -// =============> explain this error message here // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num){ + return num * num; +} +console.log(square(3)); +console.log(square(4)); From d9cd2365865ab394cfbe930ad4433f03de3f151b Mon Sep 17 00:00:00 2001 From: sab325 Date: Sun, 9 Feb 2025 16:54:34 +0000 Subject: [PATCH 2/6] 2-mandatory-debug --- Sprint-2/2-mandatory-debug/0.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..b0bdba4b7 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,16 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here: there is no return statement -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 +// =============> write your explanation here; +//multiply function is logged with out define so the result will be undefined, need add return statement to complete it. // 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)}`); \ No newline at end of file From 74649af9057db65a0a602c8a2390d7c0602c5d74 Mon Sep 17 00:00:00 2001 From: sab325 Date: Sun, 9 Feb 2025 18:02:56 +0000 Subject: [PATCH 3/6] sprint2 debugging --- Sprint-2/2-mandatory-debug/1.js | 15 ++++++++------- Sprint-2/2-mandatory-debug/2.js | 19 +++++++++++++++---- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..6c03a2a9c 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,14 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here: there is ; error -function sum(a, b) { - return; - a + b; -} -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here: function will be undefined because of ";" by removing ; placing a+b in same line will return function. // 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)}`); \ 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 57d3f5dc3..5dfdf6a65 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,7 @@ -// Predict and explain first... +// Predict and explain first...because of const num, it won't change output. // Predict the output of the following code: -// =============> Write your prediction here +// =============> Write your prediction here: 3 const num = 103; @@ -14,11 +14,22 @@ 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 +// =============> write the output here: 3 for all console.log. // Explain why the output is the way it is -// =============> write your explanation here +// =============> write your explanation here: 3 for all of the the console.log because it won't take any argument it will always take const argument. // 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 +because of line 6. \ No newline at end of file From fe81c5769f5f6fd6802268037afe09160da27c11 Mon Sep 17 00:00:00 2001 From: sab325 Date: Sun, 9 Feb 2025 18:45:10 +0000 Subject: [PATCH 4/6] 3-mandatory-implement --- Sprint-2/3-mandatory-implement/1-bmi.js | 8 ++++++- Sprint-2/3-mandatory-implement/2-cases.js | 5 +++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 21 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..8adb9bc43 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,10 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file +} +function calculateBMI(weight, height){ + let bmi = weight /(height * height); + return parseFloat(bmi.toFixed(1)); +} + +console.log(calculateBMI(70, 2.99)); \ 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 5b0ef77ad..e1f4dc018 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/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 +const text = `hello there`; +console.log(text.toUpperCase()); + +const sentence = `lord of the rings`; +console.log(sentence.toUpperCase()); \ 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 6265a1a70..096ecaf57 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,24 @@ // 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(`The last digit of 399p is ${toPounds("399p")}`); +console.log(`The last digit of 52p is ${toPounds("52p")}`); +console.log(`The last digit of 1099p is ${toPounds("1099p")}`); \ No newline at end of file From af447a3d904cd31967e73435d62781b5243c6284 Mon Sep 17 00:00:00 2001 From: sab325 Date: Tue, 11 Feb 2025 12:25:52 +0000 Subject: [PATCH 5/6] 4-mandatory-interpret --- Sprint-2/4-mandatory-interpret/time-format.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..baae34072 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -10,6 +10,8 @@ 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 @@ -17,18 +19,18 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> write your answer here: 3 times // 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 +// =============> write your answer here: 0 // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> 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 +// =============> write your answer here: 1, when pad is called for the last time, pad(remainingSecond) is remainingSecond=1 // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer -// =============> write your answer here +// =============> write your answer here: It will be 01 as we used padStart(2, "0") which will make 2 digit for example if value was 1 it will return as 01. From cb674b4aa065d9d9ef8fe21d65e51236808e69f4 Mon Sep 17 00:00:00 2001 From: sab325 Date: Sun, 23 Feb 2025 14:29:20 +0000 Subject: [PATCH 6/6] Made changes --- Sprint-2/2-mandatory-debug/1.js | 5 +++- Sprint-2/2-mandatory-debug/2.js | 11 ++++--- Sprint-2/3-mandatory-implement/2-cases.js | 4 +-- Sprint-2/3-mandatory-implement/3-to-pounds.js | 30 +++++++++---------- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 6c03a2a9c..45daa7d65 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -11,4 +11,7 @@ function sum(a, b) { } -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); \ No newline at end of file +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +//Between "return value" and "function", which one is undefined? + +//return value will be undefined if function have not return statement it will automatically returns undefined. \ 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 5dfdf6a65..275089586 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -4,19 +4,18 @@ // =============> Write your prediction here: 3 const num = 103; - 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 806 is ${getLastDigit(806)}`); // Now run the code and compare the output to your prediction // =============> write the output here: 3 for all console.log. // Explain why the output is the way it is -// =============> write your explanation here: 3 for all of the the console.log because it won't take any argument it will always take const argument. +// =============> write your explanation here: 3 for all of the the console.log because it won't take any argument it will always take global variable num = 103. // Finally, correct the code to fix the problem // =============> write your new code here @@ -28,8 +27,12 @@ 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)}`); +// Does the function toPounds() return the last digit of the given parameter? +// No, it doesn't return toPounds() of the last digit of the given parameter. + + // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem -because of line 6. \ No newline at end of file +//because of line 6. \ 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 e1f4dc018..0b032d3fd 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -15,7 +15,7 @@ // 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 const text = `hello there`; -console.log(text.toUpperCase()); +console.log(text.toUpperCase().replace(/ /g, "_")); const sentence = `lord of the rings`; -console.log(sentence.toUpperCase()); \ No newline at end of file +console.log(sentence.toUpperCase().replace(/ /g, "_")); \ 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 096ecaf57..57706ca60 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,24 +4,24 @@ // 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 -); +function toPounds(penceString) { + const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 + ); -const pence = paddedPenceNumberString - .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); + 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(`The last digit of 399p is ${toPounds("399p")}`); console.log(`The last digit of 52p is ${toPounds("52p")}`); -console.log(`The last digit of 1099p is ${toPounds("1099p")}`); \ No newline at end of file +console.log(`The last digit of 1099p is ${toPounds("1099p")}`);