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 d8fd1873339a7b47409d112e91c8c4c49b351dac Mon Sep 17 00:00:00 2001 From: saff-coder Date: Mon, 3 Nov 2025 14:16:37 +0000 Subject: [PATCH 02/11] completed the function now it should count how many char in a string --- Sprint-3/2-practice-tdd/count.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..0e960e890 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,11 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + count++; + } + } + return count; // this replaces 'return 5' } module.exports = countChar; From 5c3f09f68cbe7065e332f7ae60370f6d068133cf Mon Sep 17 00:00:00 2001 From: saff-coder Date: Mon, 3 Nov 2025 22:17:41 +0000 Subject: [PATCH 03/11] Updated the CountChar function --- Sprint-3/2-practice-tdd/count.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 0e960e890..c61bfdce7 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,11 +1,12 @@ -function countChar(stringOfCharacters, findCharacter) { - let count = 0; - for (let i = 0; i < stringOfCharacters.length; i++) { - if (stringOfCharacters[i] === findCharacter) { - count++; +function countChar(str, char) { + let count = 0; + + for (let i = 0; i < str.length; i++) {//loop through each character in the string + if (str[i] === char) {//check if the character matches the target character + count = count + 1; } } - return count; // this replaces 'return 5' -} -module.exports = countChar; + return count;//return the final count +} +module.exports = countChar; \ No newline at end of file From 26147072c308fdd972526c9523c91414568e43b7 Mon Sep 17 00:00:00 2001 From: saff-coder Date: Mon, 3 Nov 2025 22:21:14 +0000 Subject: [PATCH 04/11] implement countChar test done with Different Scenario --- Sprint-3/2-practice-tdd/count.test.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..8a2f9e060 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -1,19 +1,29 @@ // implement a function countChar that counts the number of times a character occurs in a string const countChar = require("./count"); + + // Given a string str and a single character char to search for, // When the countChar function is called with these inputs, -// Then it should: +// Scenario: Single Occurrence +test("counts a single occurrence of a character", () => { + expect(countChar("hello", "h")).toEqual(1); + expect(countChar("world", "d")).toEqual(1); + }); // Scenario: Multiple Occurrences // Given the input string str, // And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'), // When the function is called with these inputs, // Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa'). +test("counts multiple occurrences of a character", () => { + expect(countChar("banana", "a")).toEqual(3); + expect(countChar("mississippi", "s")).toEqual(4); + }); test("should count multiple occurrences of a character", () => { const str = "aaaaa"; - const char = "a"; - const count = countChar(str, char); + const Char = "a"; + const count = countChar(str,Char); expect(count).toEqual(5); }); @@ -22,3 +32,11 @@ test("should count multiple occurrences of a character", () => { // And a character char that does not exist within the case-sensitive str, // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str. +// Scenario: No Occurrences +test("should return 0 if the character is not in the string", () => { + const str = "hello"; + const char = "x"; // character not present in str + const count = countChar(str, char); + expect(count).toEqual(0); +}); + From c8c9bc13063259d4b071ecec4be7e32350646c8c Mon Sep 17 00:00:00 2001 From: saff-coder Date: Mon, 3 Nov 2025 22:21:44 +0000 Subject: [PATCH 05/11] function Updated --- Sprint-3/2-practice-tdd/repeat.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..c4505cc66 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,8 @@ -function repeat() { - return "hellohellohello"; +function repeat(word, times) { + if (times < 0) { + throw new Error("Count must be a non-negative number"); + } + return word.repeat(times); } module.exports = repeat; From 620ee390839c8689fd689052eb00c5def553357b Mon Sep 17 00:00:00 2001 From: saff-coder Date: Mon, 3 Nov 2025 22:23:13 +0000 Subject: [PATCH 06/11] test is done in different cases --- Sprint-3/2-practice-tdd/repeat.test.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..7c1488b13 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -20,13 +20,23 @@ test("should repeat the string count times", () => { // Given a target string str and a count equal to 1, // When the repeat function is called with these inputs, // Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. - +test("should repeat the string count times", () => { +const str = "hello"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("hello"); +}); // case: Handle Count of 0: // Given a target string str and a count equal to 0, // When the repeat function is called with these inputs, // Then it should return an empty string, ensuring that a count of 0 results in an empty output. - +test("should return empty string when times is 0", () => { + expect(repeat("hello", 0)).toEqual(""); +}); // case: Negative Count: // Given a target string str and a negative integer count, // When the repeat function is called with these inputs, // Then it should throw an error or return an appropriate error message, as negative counts are not valid. +test("should throw an error when count is negative", () => { + expect(() => repeat("hello", -3)).toThrow("Count must be a non-negative number"); +}); From 745b6e9153fad1dff2c6d666eb92d972b26f7bf4 Mon Sep 17 00:00:00 2001 From: saff-coder Date: Mon, 3 Nov 2025 22:23:50 +0000 Subject: [PATCH 07/11] function updated --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..db4bc3ff5 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,21 @@ function getOrdinalNumber(num) { - return "1st"; + const lastTwoDigits = num % 100; // check the last 2 digits for 11,12,13 + const lastDigit = num % 10; + + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return num + "th"; + } + + switch (lastDigit) { + case 1: + return num + "st"; + case 2: + return num + "nd"; + case 3: + return num + "rd"; + default: + return num + "th"; + } } module.exports = getOrdinalNumber; From d6f14a8779d0888f468e67c9bc35d30cf82a3593 Mon Sep 17 00:00:00 2001 From: saff-coder Date: Mon, 3 Nov 2025 22:24:20 +0000 Subject: [PATCH 08/11] test is done with different Numbers --- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index dfe4b6091..bd1d35068 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -11,3 +11,13 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); +test("should return 'th' for 11, 12, 13", () => { + expect(getOrdinalNumber(11)).toBe("11th"); + expect(getOrdinalNumber(12)).toBe("12th"); + expect(getOrdinalNumber(13)).toBe("13th"); +}); +test("should return correct ordinal for numbers > 20", () => { + expect(getOrdinalNumber(21)).toBe("21st"); + expect(getOrdinalNumber(22)).toBe("22nd"); + expect(getOrdinalNumber(23)).toBe("23rd"); +}); \ No newline at end of file From 45f409b00a55fe75c2fa6a390f116f8274af9977 Mon Sep 17 00:00:00 2001 From: saff-coder Date: Mon, 3 Nov 2025 22:56:51 +0000 Subject: [PATCH 09/11] #reverted sprint 1commit --- 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 566278d0b5b4caadec189a7416dd68fe38c482f7 Mon Sep 17 00:00:00 2001 From: saff-coder Date: Sat, 8 Nov 2025 22:37:13 +0000 Subject: [PATCH 10/11] The test was edited --- Sprint-3/2-practice-tdd/count.test.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 8a2f9e060..c2c8d0fd7 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -16,17 +16,18 @@ test("counts a single occurrence of a character", () => { // When the function is called with these inputs, // Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa'). test("counts multiple occurrences of a character", () => { - expect(countChar("banana", "a")).toEqual(3); - expect(countChar("mississippi", "s")).toEqual(4); - }); - -test("should count multiple occurrences of a character", () => { - const str = "aaaaa"; - const Char = "a"; - const count = countChar(str,Char); - expect(count).toEqual(5); + expect(countChar("aaaaa", "a")).toEqual(5); + expect(countChar("banana", "a")).toEqual(3); + expect(countChar("mississippi", "s")).toEqual(4); }); +//test("should count multiple occurrences of a character", () => { + // const str = "aaaaa"; + //const Char = "a"; + //const count = countChar(str,Char); + // expect(count).toEqual(5); +//}); + // Scenario: No Occurrences // Given the input string str, // And a character char that does not exist within the case-sensitive str, From 407bf05712fcd3af1f09b4990a19bc1bf9aa0b92 Mon Sep 17 00:00:00 2001 From: saff-coder Date: Sun, 9 Nov 2025 00:32:26 +0000 Subject: [PATCH 11/11] more testing scenarios added --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 1 + .../2-practice-tdd/get-ordinal-number.test.js | 53 ++++++++++++++----- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index db4bc3ff5..fe45ac0eb 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -15,6 +15,7 @@ function getOrdinalNumber(num) { return num + "rd"; default: return num + "th"; + } } diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index bd1d35068..3b9a9907c 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,23 +1,48 @@ const getOrdinalNumber = require("./get-ordinal-number"); + + // In this week's prep, we started implementing getOrdinalNumber // continue testing and implementing getOrdinalNumber for additional cases // Write your tests using Jest - remember to run your tests often for continual feedback -// Case 1: Identify the ordinal number for 1 -// When the number is 1, -// Then the function should return "1st" - -test("should return '1st' for 1", () => { +// Case 1: Numbers ending in 1 (but not 11) +test("append 'st' to numbers ending in 1, except those ending in 11", () => { expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(101)).toEqual("101st"); + expect(getOrdinalNumber(111)).toEqual("111th"); // exception +}); + +// Case 2: Numbers ending in 2 (but not 12) +test("append 'nd' to numbers ending in 2, except those ending in 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(102)).toEqual("102nd"); + expect(getOrdinalNumber(112)).toEqual("112th"); // exception }); -test("should return 'th' for 11, 12, 13", () => { - expect(getOrdinalNumber(11)).toBe("11th"); - expect(getOrdinalNumber(12)).toBe("12th"); - expect(getOrdinalNumber(13)).toBe("13th"); +// Case 3: Numbers ending in 3 (but not 13) +test("append 'rd' to numbers ending in 3, except those ending in 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(103)).toEqual("103rd"); + expect(getOrdinalNumber(113)).toEqual("113th"); // exception }); -test("should return correct ordinal for numbers > 20", () => { - expect(getOrdinalNumber(21)).toBe("21st"); - expect(getOrdinalNumber(22)).toBe("22nd"); - expect(getOrdinalNumber(23)).toBe("23rd"); -}); \ No newline at end of file +// Case 4: Numbers ending in 4–9 or exceptions (11, 12, 13) +test("append 'th' to numbers ending in 4–9 or ending with 11, 12, or 13", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(9)).toEqual("9th"); + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); +}); + // Optional: edge cases + test("should handle 0 and large numbers correctly", () => { + expect(getOrdinalNumber(0)).toBe("0th"); + expect(getOrdinalNumber(1000001)).toBe("1000001st"); // ends with 1, not teen + expect(getOrdinalNumber(1000012)).toBe("1000012th"); // ends with 12 -> teen + + }); + + +