From 73129858197a88a456280464710c166f1f201d0d Mon Sep 17 00:00:00 2001 From: saff-coder Date: Fri, 10 Oct 2025 14:06:28 +0100 Subject: [PATCH 01/11] All the tasks are complete --- Sprint-1/1-key-exercises/1-count.js | 1 + Sprint-1/1-key-exercises/2-initials.js | 4 +- Sprint-1/1-key-exercises/3-paths.js | 9 +++- Sprint-1/1-key-exercises/4-random.js | 45 +++++++++++++++++++ Sprint-1/2-mandatory-errors/0.js | 8 +++- Sprint-1/2-mandatory-errors/1.js | 4 ++ Sprint-1/2-mandatory-errors/2.js | 3 +- Sprint-1/2-mandatory-errors/3.js | 11 ++++- Sprint-1/2-mandatory-errors/4.js | 10 ++++- .../1-percentage-change.js | 19 +++++++- .../3-mandatory-interpret/2-time-format.js | 8 +++- Sprint-1/3-mandatory-interpret/3-to-pounds.js | 7 ++- Sprint-1/4-stretch-explore/chrome.md | 6 +++ Sprint-1/4-stretch-explore/objects.md | 5 +++ 14 files changed, 128 insertions(+), 12 deletions(-) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..7519f8829 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,4 @@ count = count + 1; // Line 1 is a variable declaration, creating the count variable with an initial value of 0 // Describe what line 3 is doing, in particular focus on what = is doing +//In line 3 we are taking the current value of count (which is 0) and adding 1 to it, and store the new value back into count" so now count is 1.the = is an assignment operator that Assigns values to variables so it takes the value on the right side (count + 1) and assigns it to the variable on the left side (count). \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..23a96ab68 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,9 @@ let lastName = "Johnson"; // Declare a variable called initials that stores the first character of each string. // This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. -let initials = ``; +let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`; // https://www.google.com/search?q=get+first+character+of+string+mdn +console.log(initials); //that should print "CKJ" +//so the code is going to check the first character of each variable that means “the character at position 0 diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..e0e2b5b0e 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,12 @@ console.log(`The base part of ${filePath} is ${base}`); // Create a variable to store the dir part of the filePath variable // Create a variable to store the ext part of the variable -const dir = ; -const ext = ; +const dir = filePath.slice(0, lastSlashIndex);// the slice is a method that cuts out part of a string and returns it as a new string. we use it here to get the dir part of the filePath +const ext = base.slice(base.lastIndexOf("."));//the ext is any thing after the last dot +//const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; +//const dir = "/Users/mitch/cyf/Module-JS1/week-1/interpret"; + +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The ext part of ${filePath} is ${ext}`); // https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..ee82575d1 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -2,8 +2,53 @@ const minimum = 1; const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +console.log(num); // In this exercise, you will need to work out what num represents? // Try breaking down the expression and using documentation to explain what it means // It will help to think about the order in which expressions are evaluated // Try logging the value of num and running the program several times to build an idea of what the program is doing + +//num is a random number between minimum and maximum (inclusive) +/*Math.random() + +This gives you a random decimal number between 0 (inclusive) and 1 (exclusive) — meaning it can be 0, but never exactly 1. + +Example outputs: +0.23 + +/*(maximum - minimum + 1) + +This calculates how many whole numbers are in the range, including both ends. + +Example: +100 - 1 + 1 = 100 + +So, there are 100 possible integers from 1 to 100. + +/*Math.random() * (maximum - minimum + 1) + +This scales the random decimal to the desired range size. + +Example if Math.random() gave 0.57: +0.57 * 100 = 57 +/*Math.floor(...) +The Math.floor() method in JavaScript is used to round a number down to the nearest integer, regardless of whether the number is positive or negative or has a decimal part. +Example: +Math.floor(57.8) → 57 + +/*+ minimum + +Finally, we shift the range up so it starts at 1 instead of 0. +0–99 becomes 1–100*/ + +//The order for evaluation is: +/*maximum - minimum + 1 + +Math.random() * (that result) + +Math.floor(...) + ++ minimum*/ + +//I run the code several times and got different numbers one was 60 the other one was 88 diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..bb4556c21 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,6 @@ -This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +//This is just an instruction for the first activity - but it is just for human consumption +//We don't want the computer to run these 2 lines - how can we solve this problem? + + +//if we have any instructions or explanations in our code without the computer executing them,we can use comments +//comments In javascript are created by using (//) for single line comments \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..8d492ab86 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,7 @@ const age = 33; age = age + 1; +// This will give an error because we only use const for Fixed values so we can change const to let +let age = 33; +age = age + 1; +console.log(age); // that should print 34 \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..023100053 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,6 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + //we have to declare the variable cityOfBirth with const or let before using it in the console.log statement diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..ec4223fd1 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,18 @@ const cardNumber = 4533787178994213; const last4Digits = cardNumber.slice(-4); - +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working // Before running the code, make and explain a prediction about why the code won't work +//the slice method is used for strings not numbers. // Then run the code and see what error it gives. +//Uncaught TypeError: cardNumber.slice is not a function // Consider: Why does it give this error? Is this what I predicted? If not, what's different? +//javascript can not slice a number // Then try updating the expression last4Digits is assigned to, in order to get the correct value +//I have added the console.log to see the output. +//there is another way to get the last 4 digits by using slice method is to convert the number to a string first by using toString() method +const last4Digits = cardNumber.toString().slice(-4); //it wil print "4213" +//If you want it to be a number, you can convert it back: +const last4Digits = Number(cardNumber.toString().slice(-4)); +Now last4Digits will be 4213 as a number. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..07b8dea17 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,10 @@ const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const 24hourClockTime = "08:53"; + +//because variables names cannot start with a number +//I have changed the variable names to start with a letter +const twelveHourClockTime = "08:53 PM"; +const twentyFourHourClockTime = "20:53"; +console.log(twelveHourClockTime); +console.log(twentyFourHourClockTime); +// that should print "08:53 PM" and "20:53" \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..3ec5c3051 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -2,7 +2,7 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ,"")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -12,11 +12,26 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below // a) How many function calls are there in this file? Write down all the lines where a function call is made - +//after running the code I found 5 function calls +//line 1: replaceAll +//line 2: replaceAll +//line 4: Number +//line 5: Number +//line 8: console.log // b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? +// after running the code i found an error in line 5 missing a comma in the replaceAll method to fix it I have added the comma // c) Identify all the lines that are variable reassignment statements +carPrice = Number(carPrice.replaceAll(",", "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); + // d) Identify all the lines that are variable declarations +let carPrice = "10,000"; +let priceAfterOneYear = "8,543"; +const priceDifference = carPrice - priceAfterOneYear; +const percentageChange = (priceDifference / carPrice) * 100; // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// Fist the car price is a string so we can't do any calculation on it yet +//so first we remove all the comma by using replaceAll method then we convert it to a number by using Number method \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..d1e665924 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,20 @@ console.log(result); // For the piece of code above, read the code and then answer the following questions // a) How many variable declarations are there in this program? +// there is 6 variable declarations // b) How many function calls are there? +// there is only 1 function call which is the last onw in line 10 console.log(result); // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +//modulus operator % gives the remainder after dividing one number by another.which is in this case8784 % 60 = 24 second + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +//we are converting the seconds into minutes by dividing by 60 // e) What do you think the variable result represents? Can you think of a better name for this variable? - +//Duration of the movie in hours, minutes, and seconds. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +//yes it will work for all values of movieLength because the code will always convert seconds into hours, minutes, and seconds format. \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..4cb4ce2c7 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -24,4 +24,9 @@ console.log(`£${pounds}.${pence}`); // Try and describe the purpose / rationale behind each step // To begin, we can start with -// 1. const penceString = "399p": initialises a string variable with the value "399p" +// 1. const penceString = "399p": a string variable with the value "399p" +//2.in line 3 because the value is a string we can't do any calculation on it yet we need to convert it to a number so the first step is to get rid of the letter P at the end so no matter how long the number is we get rid of the last character. +//3. in line 8 we make sure the number has 3 digits so if needed we add zeros at the start at the number +//4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);: by taking away the last 2 digit we will get the pounds in this example the amount is 399 so by taking away the 99 we will get 3 pounds +//6. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");: the same thing again now extracts the pence part by taking the last two digits if it is one digit we add 0 at the end to make it two digits +// in line 18 to print the result in the right format £ sign then pounds then a dot and then pence diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..cbc84b3be 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -11,8 +11,14 @@ In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +the browser shows a popup message box with your text — "Hello world!" Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`. +let myName = prompt("What is your name?"); +The browser shows a popup with a text input box and OK / Cancel buttons. + What effect does calling the `prompt` function have? +It display a dialog box that prompts the user for input What is the return value of `prompt`? +The text the user entered (string) if clicked OK, or null if Cancel diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..e2510a537 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,17 @@ In this activity, we'll explore some additional concepts that you'll encounter i Open the Chrome devtools Console, type in `console.log` and then hit enter What output do you get? +nothing Now enter just `console` in the Console, what output do you get back? +error message Try also entering `typeof console` Answer the following questions: What does `console` store? +console store Functions What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +console.log will print the message to the console +console.assert will print a message if the condition is false From c9700bf641aa1b54976e1053c678a835d476261e Mon Sep 17 00:00:00 2001 From: saff-coder Date: Thu, 23 Oct 2025 09:43:51 +0100 Subject: [PATCH 02/11] Key error done The codes are fixed, the error has been corrected and explained --- Sprint-2/1-key-errors/0.js | 11 ++++++++--- Sprint-2/1-key-errors/1.js | 17 ++++++++++++++--- Sprint-2/1-key-errors/2.js | 19 +++++++++++++++---- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..eb25856af 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// =============> This code will give an error message // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -9,5 +9,10 @@ function capitalise(str) { return str; } -// =============> write your explanation here -// =============> write your new code here +// =============> it gave an error because the variable str is being declared twice one with the function and another one with anew variable +// =============> this can be fixed by changing the variable name inside the function +function capitalise(str) { + let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalised; +} +console.log(capitalise("sophia")); // Output: "Sophia" \ 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..b6e7d7382 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,7 +1,7 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> I can see that the variable decimalNumber is being declared twice once as a function parameter and again as a constant // Try playing computer with the example to work out what is going on @@ -14,7 +14,18 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +// =============> so the function is to convert a decimal number into a percentage +// so if the number is 0.5 it is multiplied by 100 +// % sign is added at the end to make it 50% // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> the problem can be fixed by removing the redeclaration of the variable decimalNumber inside the function +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} + +// Call the function and store the result in a variable +const result = convertToPercentage(0.5); +console.log(result); + diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..c7090da81 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,29 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> First there is a number inside the function which is not allowed in JavaScript it should be a variable name +//the second mistake is num is not declared function square(3) { return num * num; } -// =============> write the error message here +// =============> Uncaught SyntaxError: Unexpected number +// =============> we put a number (3)where JavaScript expected a variable name +//Uncaught SyntaxError: Illegal return statement -// =============> explain this error message here +//we cant use return inside the function without declaring the function properly // Finally, correct the code to fix the problem -// =============> write your new code here + +// =============> +function square(num) { + return num * num; +} +console.log(square(3)); // the answer should be 9 +console.log(square(5)); // the answer should be 25 +console.log(square(10)); // the answer should be 100 + From f6931054cfa6a610e90cf9c3f865e62de917da14 Mon Sep 17 00:00:00 2001 From: saff-coder Date: Fri, 24 Oct 2025 09:27:35 +0100 Subject: [PATCH 03/11] all the mandatory debug is done The prediction and explanation were done --- Sprint-2/2-mandatory-debug/0.js | 14 ++++++++++---- Sprint-2/2-mandatory-debug/1.js | 10 +++++++--- Sprint-2/2-mandatory-debug/2.js | 18 +++++++++++++----- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..783868df3 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,7 @@ // Predict and explain first... -// =============> write your prediction here +// =============> console.log will print the result because log()will write the message to the console. +// function multiply(a, b) { console.log(a * b); @@ -8,7 +9,12 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here - +// =============> When testing the code in Node.js, the output was 320 without the message because it was undefined "The result of multiplying 10 and 32 is undefined". +//we need to use return to store the result then print it out. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> // Corrected Code: +function multiply(a, b) { + return a * b; // Use 'return' instead of 'console.log' +} +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); + diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..86cb04116 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// =============> i think there will be and error message because at doesn't show what value to return so it will be undefined. function sum(a, b) { return; @@ -8,6 +8,10 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// ============= it should be in the same line after return statement to return the value // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> corrected code: +function sum(a, b) { + return a + b; +} +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..0545a6e82 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 the output of the following code: -// =============> Write your prediction here +// =============> I predict that the code might not work because I am declaring num outside the function also I am giving num a constant value means it will always be 103 const num = 103; @@ -14,11 +14,19 @@ 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 code worked but the answer was 3 i predicted an error message. // Explain why the output is the way it is -// =============> write your explanation here +// =============> the output was 3 because at the beginning it we declared that num value as 103 // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> corrected code is running the code without giving num any value and add num in the function + +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 +// Explain why getLastDigit is not working properly - it was not working at the start because every time we run the code would slice the last number of the 103 because we declared num as a constant with value 103 From 15d0324c84ca65919a02fc4369dfd9ea6da7ed19 Mon Sep 17 00:00:00 2001 From: saff-coder Date: Fri, 24 Oct 2025 13:31:56 +0100 Subject: [PATCH 04/11] All the mandatory implementing done 1. Calculating the BMI 2. Change a sentence from lowercase to uppersnake case 3. Changing from Kg to lb --- Sprint-2/3-mandatory-implement/1-bmi.js | 9 ++++++++- Sprint-2/3-mandatory-implement/2-cases.js | 11 +++++++++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 13 +++++++++++++ 3 files changed, 32 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..c9cfa543f 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,11 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file +} +(calculateBMI(weight/hight*hight)); + let bmi = 85 / (1.54 * 1.54); + console.log(bmi) + //here the result was 35.833307439446366 I need to round it to 1 decimal place + console.log(bmi.toFixed(1)); + //now the output is 35.8 + \ 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..e2387d568 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,14 @@ // 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 + +// There is 2 things we need to do: change the space to_ and change the letters from lowercase to uppercase + +function toUpperSnakeCase(str) { + return str + .replaceAll(' ', '_') // replace spaces with underscores + .toUpperCase(); // make everything uppercase +} + +console.log(toUpperSnakeCase("hello there")); +console.log(toUpperSnakeCase("lord of the rings")); \ 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..cbaef1a3e 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,16 @@ // 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(kg) { + const pounds = kg * 2.20462; + + return pounds; +} +console.log(toPounds(1)); +console.log(toPounds(5)); +console.log(toPounds(10)); + // if I want to round the result to 2 decimal places I can use toFixed(2) + console.log(toPounds(1).toFixed(2)); + console.log(toPounds(5).toFixed(2)); + console.log(toPounds(10).toFixed(2)); \ No newline at end of file From 3178e1542d6a20a650778317d6ff413b56d92ffd Mon Sep 17 00:00:00 2001 From: saff-coder Date: Fri, 24 Oct 2025 17:13:49 +0100 Subject: [PATCH 05/11] Mandatory interpretation is done I run the code in Python Visualiser and answered all the questions --- Sprint-2/4-mandatory-interpret/time-format.js | 14 +++++++++----- 1 file changed, 9 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..1adacd2ca 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,22 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> 3 Times +pad(totalHours) +pad(remainingMinutes) +pad(remainingSeconds) + // 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 first value is totalHours // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> "00" because the target length is 2 digits // 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 this is last value is remaining in Seconds // 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 +// =============> 01 because the target length is 2 digits From a4cfc97a5ad3572dd459c8684d139263288c997f Mon Sep 17 00:00:00 2001 From: saff-coder Date: Sat, 25 Oct 2025 15:26:09 +0100 Subject: [PATCH 06/11] Revert "All the tasks are complete" This reverts commit 73129858197a88a456280464710c166f1f201d0d. --- Sprint-1/1-key-exercises/1-count.js | 1 - Sprint-1/1-key-exercises/2-initials.js | 4 +- Sprint-1/1-key-exercises/3-paths.js | 9 +--- Sprint-1/1-key-exercises/4-random.js | 45 ------------------- Sprint-1/2-mandatory-errors/0.js | 8 +--- Sprint-1/2-mandatory-errors/1.js | 4 -- Sprint-1/2-mandatory-errors/2.js | 3 +- Sprint-1/2-mandatory-errors/3.js | 11 +---- Sprint-1/2-mandatory-errors/4.js | 10 +---- .../1-percentage-change.js | 19 +------- .../3-mandatory-interpret/2-time-format.js | 8 +--- Sprint-1/3-mandatory-interpret/3-to-pounds.js | 7 +-- Sprint-1/4-stretch-explore/chrome.md | 6 --- Sprint-1/4-stretch-explore/objects.md | 5 --- 14 files changed, 12 insertions(+), 128 deletions(-) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 7519f8829..117bcb2b6 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,4 +4,3 @@ count = count + 1; // Line 1 is a variable declaration, creating the count variable with an initial value of 0 // Describe what line 3 is doing, in particular focus on what = is doing -//In line 3 we are taking the current value of count (which is 0) and adding 1 to it, and store the new value back into count" so now count is 1.the = is an assignment operator that Assigns values to variables so it takes the value on the right side (count + 1) and assigns it to the variable on the left side (count). \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 23a96ab68..47561f617 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,9 +5,7 @@ let lastName = "Johnson"; // Declare a variable called initials that stores the first character of each string. // This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. -let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`; +let initials = ``; // https://www.google.com/search?q=get+first+character+of+string+mdn -console.log(initials); //that should print "CKJ" -//so the code is going to check the first character of each variable that means “the character at position 0 diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index e0e2b5b0e..ab90ebb28 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,12 +17,7 @@ console.log(`The base part of ${filePath} is ${base}`); // Create a variable to store the dir part of the filePath variable // Create a variable to store the ext part of the variable -const dir = filePath.slice(0, lastSlashIndex);// the slice is a method that cuts out part of a string and returns it as a new string. we use it here to get the dir part of the filePath -const ext = base.slice(base.lastIndexOf("."));//the ext is any thing after the last dot -//const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; -//const dir = "/Users/mitch/cyf/Module-JS1/week-1/interpret"; - -console.log(`The dir part of ${filePath} is ${dir}`); -console.log(`The ext part of ${filePath} is ${ext}`); +const dir = ; +const ext = ; // https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index ee82575d1..292f83aab 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -2,53 +2,8 @@ const minimum = 1; const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; -console.log(num); // In this exercise, you will need to work out what num represents? // Try breaking down the expression and using documentation to explain what it means // It will help to think about the order in which expressions are evaluated // Try logging the value of num and running the program several times to build an idea of what the program is doing - -//num is a random number between minimum and maximum (inclusive) -/*Math.random() - -This gives you a random decimal number between 0 (inclusive) and 1 (exclusive) — meaning it can be 0, but never exactly 1. - -Example outputs: -0.23 - -/*(maximum - minimum + 1) - -This calculates how many whole numbers are in the range, including both ends. - -Example: -100 - 1 + 1 = 100 - -So, there are 100 possible integers from 1 to 100. - -/*Math.random() * (maximum - minimum + 1) - -This scales the random decimal to the desired range size. - -Example if Math.random() gave 0.57: -0.57 * 100 = 57 -/*Math.floor(...) -The Math.floor() method in JavaScript is used to round a number down to the nearest integer, regardless of whether the number is positive or negative or has a decimal part. -Example: -Math.floor(57.8) → 57 - -/*+ minimum - -Finally, we shift the range up so it starts at 1 instead of 0. -0–99 becomes 1–100*/ - -//The order for evaluation is: -/*maximum - minimum + 1 - -Math.random() * (that result) - -Math.floor(...) - -+ minimum*/ - -//I run the code several times and got different numbers one was 60 the other one was 88 diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index bb4556c21..cf6c5039f 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,6 +1,2 @@ -//This is just an instruction for the first activity - but it is just for human consumption -//We don't want the computer to run these 2 lines - how can we solve this problem? - - -//if we have any instructions or explanations in our code without the computer executing them,we can use comments -//comments In javascript are created by using (//) for single line comments \ No newline at end of file +This is just an instruction for the first activity - but it is just for human consumption +We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 8d492ab86..7a43cbea7 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,7 +2,3 @@ const age = 33; age = age + 1; -// This will give an error because we only use const for Fixed values so we can change const to let -let age = 33; -age = age + 1; -console.log(age); // that should print 34 \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index 023100053..e09b89831 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,6 +1,5 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? -const cityOfBirth = "Bolton"; console.log(`I was born in ${cityOfBirth}`); - //we have to declare the variable cityOfBirth with const or let before using it in the console.log statement +const cityOfBirth = "Bolton"; diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec4223fd1..ec101884d 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,18 +1,9 @@ const cardNumber = 4533787178994213; const last4Digits = cardNumber.slice(-4); -console.log(last4Digits); + // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working // Before running the code, make and explain a prediction about why the code won't work -//the slice method is used for strings not numbers. // Then run the code and see what error it gives. -//Uncaught TypeError: cardNumber.slice is not a function // Consider: Why does it give this error? Is this what I predicted? If not, what's different? -//javascript can not slice a number // Then try updating the expression last4Digits is assigned to, in order to get the correct value -//I have added the console.log to see the output. -//there is another way to get the last 4 digits by using slice method is to convert the number to a string first by using toString() method -const last4Digits = cardNumber.toString().slice(-4); //it wil print "4213" -//If you want it to be a number, you can convert it back: -const last4Digits = Number(cardNumber.toString().slice(-4)); -Now last4Digits will be 4213 as a number. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 07b8dea17..21dad8c5d 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,10 +1,2 @@ const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; - -//because variables names cannot start with a number -//I have changed the variable names to start with a letter -const twelveHourClockTime = "08:53 PM"; -const twentyFourHourClockTime = "20:53"; -console.log(twelveHourClockTime); -console.log(twentyFourHourClockTime); -// that should print "08:53 PM" and "20:53" \ No newline at end of file +const 24hourClockTime = "08:53"; \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index 3ec5c3051..e24ecb8e1 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -2,7 +2,7 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ,"")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -12,26 +12,11 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below // a) How many function calls are there in this file? Write down all the lines where a function call is made -//after running the code I found 5 function calls -//line 1: replaceAll -//line 2: replaceAll -//line 4: Number -//line 5: Number -//line 8: console.log + // b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? -// after running the code i found an error in line 5 missing a comma in the replaceAll method to fix it I have added the comma // c) Identify all the lines that are variable reassignment statements -carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); - // d) Identify all the lines that are variable declarations -let carPrice = "10,000"; -let priceAfterOneYear = "8,543"; -const priceDifference = carPrice - priceAfterOneYear; -const percentageChange = (priceDifference / carPrice) * 100; // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? -// Fist the car price is a string so we can't do any calculation on it yet -//so first we remove all the comma by using replaceAll method then we convert it to a number by using Number method \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index d1e665924..47d239558 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,20 +12,14 @@ console.log(result); // For the piece of code above, read the code and then answer the following questions // a) How many variable declarations are there in this program? -// there is 6 variable declarations // b) How many function calls are there? -// there is only 1 function call which is the last onw in line 10 console.log(result); // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators -//modulus operator % gives the remainder after dividing one number by another.which is in this case8784 % 60 = 24 second - // d) Interpret line 4, what does the expression assigned to totalMinutes mean? -//we are converting the seconds into minutes by dividing by 60 // e) What do you think the variable result represents? Can you think of a better name for this variable? -//Duration of the movie in hours, minutes, and seconds. + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer -//yes it will work for all values of movieLength because the code will always convert seconds into hours, minutes, and seconds format. \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 4cb4ce2c7..60c9ace69 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -24,9 +24,4 @@ console.log(`£${pounds}.${pence}`); // Try and describe the purpose / rationale behind each step // To begin, we can start with -// 1. const penceString = "399p": a string variable with the value "399p" -//2.in line 3 because the value is a string we can't do any calculation on it yet we need to convert it to a number so the first step is to get rid of the letter P at the end so no matter how long the number is we get rid of the last character. -//3. in line 8 we make sure the number has 3 digits so if needed we add zeros at the start at the number -//4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);: by taking away the last 2 digit we will get the pounds in this example the amount is 399 so by taking away the 99 we will get 3 pounds -//6. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");: the same thing again now extracts the pence part by taking the last two digits if it is one digit we add 0 at the end to make it two digits -// in line 18 to print the result in the right format £ sign then pounds then a dot and then pence +// 1. const penceString = "399p": initialises a string variable with the value "399p" diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index cbc84b3be..e7dd5feaf 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -11,14 +11,8 @@ In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? -the browser shows a popup message box with your text — "Hello world!" Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`. -let myName = prompt("What is your name?"); -The browser shows a popup with a text input box and OK / Cancel buttons. - What effect does calling the `prompt` function have? -It display a dialog box that prompts the user for input What is the return value of `prompt`? -The text the user entered (string) if clicked OK, or null if Cancel diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index e2510a537..0216dee56 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,17 +5,12 @@ In this activity, we'll explore some additional concepts that you'll encounter i Open the Chrome devtools Console, type in `console.log` and then hit enter What output do you get? -nothing Now enter just `console` in the Console, what output do you get back? -error message Try also entering `typeof console` Answer the following questions: What does `console` store? -console store Functions What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? -console.log will print the message to the console -console.assert will print a message if the condition is false From 17480c0fb332a31b88a3fa48fbcb7b7bb20de83e Mon Sep 17 00:00:00 2001 From: saff-coder Date: Sun, 26 Oct 2025 11:07:16 +0000 Subject: [PATCH 07/11] updated the code with(lbs) at the end This function converts the weight in kg to pounds --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index cbaef1a3e..b54176408 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -6,14 +6,11 @@ // You should call this function a number of times to check it works for different inputs function toPounds(kg) { - const pounds = kg * 2.20462; - - return pounds; + const pounds = kg * 2.20462; + return `${pounds.toFixed(2)} lbs`; // returns a string } -console.log(toPounds(1)); -console.log(toPounds(5)); -console.log(toPounds(10)); - // if I want to round the result to 2 decimal places I can use toFixed(2) - console.log(toPounds(1).toFixed(2)); - console.log(toPounds(5).toFixed(2)); - console.log(toPounds(10).toFixed(2)); \ No newline at end of file + +// Test cases +console.log(toPounds(1)); //the output "2.20 lbs" +console.log(toPounds(5)); //the output is "11.02 lbs" +console.log(toPounds(10)); //the output is "22.05 lbs" From d1dfef3a89e2c5aa7c6ee18b4a8dc9038a7db1ba Mon Sep 17 00:00:00 2001 From: saff-coder Date: Sun, 26 Oct 2025 12:22:39 +0000 Subject: [PATCH 08/11] The BMI function is updated --- Sprint-2/3-mandatory-implement/1-bmi.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index c9cfa543f..7cb2b2159 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -14,13 +14,17 @@ // Then when we call this function with the weight and height // It should return their Body Mass Index to 1 decimal place -function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height +// I have made a small change to the test cases to ensure they match the expected output to 1 decimal place + function calculateBMI(weight, height) { + // Calculate BMI using the formula: weight (kg) / (height (m) * height (m)) + const bmi = weight / (height * height); + + // Round to 1 decimal place and return + return bmi.toFixed(1); } -(calculateBMI(weight/hight*hight)); - let bmi = 85 / (1.54 * 1.54); - console.log(bmi) - //here the result was 35.833307439446366 I need to round it to 1 decimal place - console.log(bmi.toFixed(1)); - //now the output is 35.8 - \ No newline at end of file + +// Test cases +console.log(calculateBMI(85, 1.54)); // 35.8 +console.log(calculateBMI(60, 1.65)); // 22.0 +console.log(calculateBMI(72, 1.80)); // 22.2 +// now the function is reusable and correct. \ No newline at end of file From cf133333c8d658afa2d8b9e9bc6a93a18b13ee02 Mon Sep 17 00:00:00 2001 From: saff-coder Date: Sun, 26 Oct 2025 16:58:33 +0000 Subject: [PATCH 09/11] changed the function now the output is a number --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index b54176408..e5600556b 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -5,12 +5,17 @@ // You should call this function a number of times to check it works for different inputs +// I thought adding "lbs" need to be inside a string . +// i converted the string back to number + function toPounds(kg) { const pounds = kg * 2.20462; - return `${pounds.toFixed(2)} lbs`; // returns a string + return Number(pounds.toFixed(2)); } +console.log(`${toPounds(5)} lbs`); + -// Test cases -console.log(toPounds(1)); //the output "2.20 lbs" -console.log(toPounds(5)); //the output is "11.02 lbs" -console.log(toPounds(10)); //the output is "22.05 lbs" +// more Test cases +console.log(`${toPounds(1)} lbs`); // 2.20 lbs +console.log(`${toPounds(5)} lbs`); // 11.02 lbs +console.log(`${toPounds(10)} lbs`); // 22.05 lbs \ No newline at end of file From 33218f8f5c5e62d82fb6d46edbebf5e80473ef12 Mon Sep 17 00:00:00 2001 From: saff-coder Date: Sun, 26 Oct 2025 23:10:11 +0000 Subject: [PATCH 10/11] I return the result as a number I was able to test my data type using typeof --- Sprint-2/3-mandatory-implement/1-bmi.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 7cb2b2159..7234694e5 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -20,11 +20,12 @@ const bmi = weight / (height * height); // Round to 1 decimal place and return - return bmi.toFixed(1); + return Number(bmi.toFixed(1)); } // Test cases console.log(calculateBMI(85, 1.54)); // 35.8 console.log(calculateBMI(60, 1.65)); // 22.0 console.log(calculateBMI(72, 1.80)); // 22.2 -// now the function is reusable and correct. \ No newline at end of file +// now the function is reusable and correct. +console.log(typeof calculateBMI(85, 1.54)); \ No newline at end of file From 03351ddd364d2fd4999d5b60f269523a422ab43d Mon Sep 17 00:00:00 2001 From: saff-coder Date: Thu, 30 Oct 2025 16:24:58 +0000 Subject: [PATCH 11/11] The code implementing is complete --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 48 +++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index e5600556b..86d48ff1f 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -8,14 +8,46 @@ // I thought adding "lbs" need to be inside a string . // i converted the string back to number -function toPounds(kg) { - const pounds = kg * 2.20462; - return Number(pounds.toFixed(2)); -} -console.log(`${toPounds(5)} lbs`); +//function toPounds(kg) { + //const pounds = kg * 2.20462; + //return Number(pounds.toFixed(2)); +//} +//console.log(`${toPounds(5)} lbs`); // more Test cases -console.log(`${toPounds(1)} lbs`); // 2.20 lbs -console.log(`${toPounds(5)} lbs`); // 11.02 lbs -console.log(`${toPounds(10)} lbs`); // 22.05 lbs \ No newline at end of file +//console.log(`${toPounds(1)} lbs`); // 2.20 lbs +//console.log(`${toPounds(5)} lbs`); // 11.02 lbs +//console.log(`${toPounds(10)} lbs`); // 22.05 lbs + +// I have misunderstood the requirement + +// Original code from interpret/to-pounds.js + +function toPounds(penceString) { + // Remove the 'p' + const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); + + // Make sure there are at least 3 digits (e.g. "5" becomes "005") + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + + // Extract pounds (everything except the last two digits) + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); + + // Extract pence + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + // 5️⃣ Return the formatted value + return `£${pounds}.${pence}`; +} + +// ✅ Test the function with examples +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("5p")); // £0.05 +console.log(toPounds("50p")); // £0.50 +console.log(toPounds("1234p")); // £12.34