From 15c4f3ba130c32fbccc493cf2f274dda5d58b34f Mon Sep 17 00:00:00 2001 From: Rawan Almutairi Date: Thu, 8 Oct 2026 21:20:48 +0100 Subject: [PATCH 1/3] Completed exercises work --- Sprint-3/1-key-errors/0.js | 40 ++++++++++++++++-- Sprint-3/1-key-errors/1.js | 34 +++++++++++---- Sprint-3/1-key-errors/2.js | 23 ++++++---- Sprint-3/1-key-errors/Terminal.log | 0 Sprint-3/2-mandatory-debug/0.js | 24 ++++++++--- Sprint-3/2-mandatory-debug/1.js | 28 ++++++++++--- Sprint-3/2-mandatory-debug/2.js | 38 +++++++++++++---- Sprint-3/3-mandatory-implement/1-bmi.js | 9 +++- Sprint-3/3-mandatory-implement/2-cases.js | 10 +++++ Sprint-3/3-mandatory-implement/3-to-pounds.js | 42 +++++++++++++++++++ Sprint-3/4-mandatory-interpret/time-format.js | 10 ++++- Sprint-3/5-stretch-extend/format-time.js | 11 +++++ Sprint-3/5-stretch-extend/test.js | 17 ++++++++ 13 files changed, 246 insertions(+), 40 deletions(-) create mode 100644 Sprint-3/1-key-errors/Terminal.log create mode 100644 Sprint-3/5-stretch-extend/test.js diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..d1d70bb95 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -3,11 +3,45 @@ // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +// The error is SyntaxError: Identifier 'str' has already been declared +//in line 9 and 10 str is declared as a parameter of capitalise and again when let is used to initalise it +//syntax error means something in the code violate javascript rules +//in this case str has been declared twice +//I had to search solutions for this case which I encountered several times. +//it looks like a common error + +//function capitalise(str) { +//let str = `${str[0].toUpperCase()}${str.slice(1)}`; +//return str; +//} + +// =============> write your explanation here +// =============> write your new code here +//the solution would be one of these: +//the easiest solution would be to remove let and not to declare it again, since it's already declared inside a function as a parameter +//other solutions I will list them later function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } +console.log(capitalise("hi")); + +// Predict and explain first... +// =============> write your prediction here +// A function capitalise is caolled, and a variable called str is initialised +//the variable str value is assigned using the string methods toUpperCase and slice + +//than a return function is called to show the str value +//I predict that the input str characters get all capitalsed becuase toUpperCase method +//it starts applying this from index 0 or first character and the slice method is telling it to leave every character afrter the index 1 as it is + +//so where does it end? +//another method str.slice is used to extract part of the string and returning it +//here it has a start index only which is 1 and the end index omitted +//because the end index omitted it will start from index 1 to the end of the string + +//I predict the value returned to be nothing because no value was assigned to str +// call the function capitalise with a string input +// interpret the error message and figure out why an error is occurring -// =============> write your explanation here -// =============> write your new code here diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..3b11504c0 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,20 +1,40 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> write your prediction here +//this has the same issue as the one I encountered in the previous example. decimalNumber is declared twice +//so I'm expecting a syntaxError, the identifier decimalNumber has been declared twice // Try playing computer with the example to work out what is going on +//function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; + +// return percentage; +//} + +//console.log(decimalNumber); + +// =============> write your explanation here +//the solution is to make the variable declared only once as a parameter +// and than remove the declaration from line 11 by removing let and reassigning its value +// Finally, correct the code to fix the problem +// =============> write your new code here + function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; + decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); - -// =============> write your explanation here +console.log(convertToPercentage(0.8)); +//ok I had to correct my solution because a reference error is caused by +//calling a parameter that only identified inside the function +//the solution would be to change line 18 to call the function and input +//input the value we want to convert because no point of declaring identifier twice +//what I notice by connicting both examples: the previous example the identifier was declared twice inside the function which caused a syntaxError +//in this example there is also declaring identifier that only identified inside a function , so the program doesn't recognise it which cause +//cause a reference error -// Finally, correct the code to fix the problem -// =============> write your new code here diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..daf05896c 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,29 @@ - // Predict and explain first BEFORE you run any code... // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +//ok, according to javaScript rules or convention for variables, we can't start with a number +//I'm expecting a syntaxErrror because its against javaScript rules -function square(3) { - return num * num; -} +//function square(3) { +// return num * num; +//} // =============> write the error message here - +//SyntaxError: Unexpected number +//not bad for a prediction-_- // =============> explain this error message here - +//it means javaCript can't identify what this number represent +//it's not part of the built in rules // Finally, correct the code to fix the problem - +//we just change number 3 to a the word three and assign its value to num // =============> write your new code here +function square(three) { + num = three; + return num * num; +} +console.log(square(4)); +//so far this sprint is easy-_- diff --git a/Sprint-3/1-key-errors/Terminal.log b/Sprint-3/1-key-errors/Terminal.log new file mode 100644 index 000000000..e69de29bb diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..f0c3886df 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,26 @@ // Predict and explain first... // =============> write your prediction here +//inside the function console.log doesn't have the value of a and b parameters +//if I think like the computer than I will see a function declared with parameters a and b +//next line I will see a print function of the parameters a and b with operator * +//I'm not a computer but I think if I was , I will print a * b +//than I will call the multiply function again to show the message +//The result of multiplying 10 and 32 is 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 +//ok, I was werong in my prediction about console.log not using the parameters value outside the function, which I correctr my understanduing Headers.apply +//what happens is the result is printed twice, the solution would be to remove the console.log from inside the function and use return instead while keeping console.log function call or in line 15 // Finally, correct the code to fix the problem -// =============> write your new code here +// ===========> 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)}`); diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..b164dd649 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,29 @@ // Predict and explain first... // =============> write your prediction here +//ok, I predict a syntaxError: a and b are not identified, and I think this because +//because the use of return function, I haven't encountered a similar case but I think it has to do +//with what the return function do, it returns the values of the parameters so +//so when a and b are used there is an issue happining but I can't say what because I don't have the information +//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 +//ok I see this error "The sum of 10 and 32 is undefined" +//I had to search what return do that cause this to be able to explain it correctly. +// this is my explaination before searching " I'm suspecting the error is caused by the return function returning the parameters values so when we use a + b; the program doesn't find what we are looking for because it was already returned" +//after searching I understand it caused becaused return means exiting the function , so line 10 will never be excuted + // Finally, correct the code to fix the problem -// =============> write your new code here +//the correction would be to include a + b inside return function so the value become identified +// ===========> write your new code here + +function sum(a, b) { + return a + b; +} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..92d128b31 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -2,23 +2,45 @@ // Predict the output of the following code: // =============> Write your prediction here +//ok the issue is clear, const is used to initialise the variable num +//at the same time num supposed to take several inputs or change its inputs +//to get the last digit of each number. However, its declared before the function using const so its value can't be reassigned +//so I'm assuming its value will always be excuted in the calculation -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 +//Because num was declared as a constant value 103 , so the assigned value of num is 103 + +//so out output of the last digit will calculate num when the input is 103 only +//no matter how many numbers we include in the brackets, the excution of the function depend on the declared num value +//num is declared with const and has the value 103 +//the solution is to declare num as a parameter inside the function // Finally, correct the code to fix the problem -// =============> write your new code here +// ===========> 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 the declared declared variable num is assigned a constant value 103. +//the solution is to delete the loine before the function and declare num as a parameter inside the function diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..f3c9fae60 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -14,6 +14,13 @@ // Then when we call this function with the weight and height // It should return a string of their Body Mass Index to 1 decimal place +//function calculateBMI(weight, height) { +// return the BMI of someone based off their weight and height +//} + function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height + //I had to search about how to include Math.floor and decimal digits to do line 30 correctly + return Math.floor((weight / (height * height)) * 10) / 10; } + +console.log(`Your Bmi is ${calculateBMI(70, 1.73)}`); diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..6645133ce 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,13 @@ // 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 capitalise_all_letters(str) { + //I used toUpperCase string method to capitalise all the string + //I used replace to replace first space " " in the string and replace it with "_" + return str.toUpperCase().replace(" ", "_"); +} + +console.log(`The result is ${capitalise_all_letters("hello world")}`); + +// so far its easy -_- \ 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..8b972009a 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,45 @@ // 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 + +//I got some help from CoPilot in debugging some parts. +// hard one + +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")); + +//This part we are removing the last character from a string. We are removing p from 399p + +//,padStart(3, "0"); it means make sure the string is 3 chaeacter , +// if not, add zero to the front +//substring(0) +//paddedPenceNumberString.length - 2 means take all except the last two characters + +//why do we do this code line? because pence is the decimals in pound +//so first we take all except the last two digets, means we take the pound part + +//here we are dealing with the pence or decimal part of the pound +//line 35, 36 mean we create a variable pence and assign it the last two digits of (paddedPrnceString) + +//here we make sure the pence value is 2 digits, if not add zero to the end + +//this part is the money format, we created a template literal to add the value of pounds to pence +//we seprate them with a dot diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..627e546cb 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -15,6 +15,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 @@ -22,17 +24,21 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> 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 +//pad(totalHour) +// // c) What is the return value of pad when it 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 - +//pad(remainingSeconds) // 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 +//01 diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..a81861093 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -2,8 +2,15 @@ // Make sure to do the prep before you do the coursework // 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. +//these are my commints +//we created a function with a parameter time to format 12 hour clock function formatAs12HourClock(time) { + //we created a variable hours and assigned its value to be the object Number + //Number object can be any number + //to find number value we use the slice method to take the value from start to index 2 const hours = Number(time.slice(0, 2)); + //here we created if statement, if the variable hours value bigger then 12 + //then return the value of hours -12 ans add two 00z after with pm if (hours > 12) { return `${hours - 12}:00 pm`; } @@ -23,3 +30,7 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +export {formatAs12HourClock}; + +// I will solve it later \ No newline at end of file diff --git a/Sprint-3/5-stretch-extend/test.js b/Sprint-3/5-stretch-extend/test.js new file mode 100644 index 000000000..451988adf --- /dev/null +++ b/Sprint-3/5-stretch-extend/test.js @@ -0,0 +1,17 @@ + + +import {formatAs12HourClock} from "./Sprint-3\5-stretch-extend\format-time.js"; + +import {formatAs12HourClock} from "./Sprint-3\5-stretch-extend\format-time.js"; +import assert from "node:assert"; +import test from "node:test"; + +test("correctly convert time after 12:00", function(){ + assert.equal(formatAs12HourClock("23:00"), "11:00 pm"); +}); + + + + +const hours = require("fast-check"); +console.log(hours); \ No newline at end of file From dbb267b47e770dc9a638c5c3208373f053cec669 Mon Sep 17 00:00:00 2001 From: Rawan0-0 Date: Fri, 9 Oct 2026 21:33:48 +0100 Subject: [PATCH 2/3] Refactor calculateBMI to use toFixed for precision Updated calculateBMI function to return a formatted string with one decimal place. --- Sprint-3/3-mandatory-implement/1-bmi.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index f3c9fae60..d301eb40d 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -19,8 +19,12 @@ //} function calculateBMI(weight, height) { - //I had to search about how to include Math.floor and decimal digits to do line 30 correctly - return Math.floor((weight / (height * height)) * 10) / 10; + return (weight / (height * height)).toFixed(1); } console.log(`Your Bmi is ${calculateBMI(70, 1.73)}`); + +//why I used toFixed? +//Its a javaScript method used to format a number's digit based on my request +//it rounds the number and fill the digit with zeros if needed +//also the parenthises mean, it will return the result as a text or string From cb6bae6ac15de233e749acf4fce196985ef57384 Mon Sep 17 00:00:00 2001 From: Rawan0-0 Date: Fri, 9 Oct 2026 21:34:52 +0100 Subject: [PATCH 3/3] Refactor capitalise_all_letters to use replaceAll Updated the capitalise_all_letters function to use replaceAll for replacing spaces with underscores. Added additional console log for testing. --- Sprint-3/3-mandatory-implement/2-cases.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 6645133ce..1f80a0812 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -18,9 +18,14 @@ function capitalise_all_letters(str) { //I used toUpperCase string method to capitalise all the string //I used replace to replace first space " " in the string and replace it with "_" - return str.toUpperCase().replace(" ", "_"); + return str.toUpperCase().replaceAll(" ", "_"); } -console.log(`The result is ${capitalise_all_letters("hello world")}`); +console.log( + `The result is ${capitalise_all_letters("hello world we are here")}`, +); -// so far its easy -_- \ No newline at end of file +// so far its easy -_- +//I used replaceAll to replace every space with _ +//I used Copilot like a teacher to help me when I'm stuck +//one day I will create AI models -_-1