From 8aee2345bd0b8e5eeba1a49b281f310dbeb6945d Mon Sep 17 00:00:00 2001 From: Hilolarustam Date: Wed, 18 Jun 2025 19:44:18 +0100 Subject: [PATCH 1/3] London | May-2025 |KhilolaRustamova | GitHomeworkFixErrors| --- Sprint-1/1-key-exercises/1-count.js | 2 ++ Sprint-1/1-key-exercises/2-initials.js | 6 ++--- Sprint-1/1-key-exercises/3-paths.js | 9 ++++---- Sprint-1/1-key-exercises/4-random.js | 32 ++++++++++++++++++++++++++ Sprint-1/2-mandatory-errors/0.js | 16 ++++++++++++- Sprint-1/2-mandatory-errors/1.js | 5 ++++ Sprint-1/2-mandatory-errors/2.js | 6 +++++ Sprint-1/2-mandatory-errors/3.js | 7 ++++++ Sprint-1/2-mandatory-errors/4.js | 13 ++++++++++- 9 files changed, 87 insertions(+), 9 deletions(-) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..716690c7c 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,5 @@ 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 +// The = operator updates the value of count by assigning it a new value so count is not =0 anymore it is = to 1 + diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..12005fbc4 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -3,9 +3,9 @@ let middleName = "Katherine"; 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 = ``; +// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution // https://www.google.com/search?q=get+first+character+of+string+mdn +//Get the first character of each name using [0] +let initials = firstName[0] + middleName[0] + lastName[0]; \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..9beb5f52f 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -10,14 +10,15 @@ // (All spaces in the "" line should be ignored. They are purely for formatting.) const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; -const lastSlashIndex = filePath.lastIndexOf("/"); -const base = filePath.slice(lastSlashIndex + 1); +const lastSlashIndex = filePath.lastIndexOf("/"); // 55 +const base = filePath.slice(lastSlashIndex + 1); // file.txt 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); +const dotIndex=filePath.lastIndexOf("."); +const ext = base.slice(dotIndex+1); // 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..4dd6b214a 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -3,6 +3,38 @@ const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; + +Math.random() + +This gives you a random decimal number between 0 (inclusive) and 1 (not including 1). + +Example output: 0.3, 0.89, 0.01, etc. + +(maximum - minimum + 1) + +This calculates how many possible numbers there are between minimum and maximum, including both. + +In this case: 100 - 1 + 1 = 100 possible numbers. + +Math.random() * (maximum - minimum + 1) + +Multiplies the random decimal by 100. This gives a number between 0 and just under 100. + +Example: If Math.random() gives 0.66, then 0.66 * 100 = 66. + +Math.floor(...) + +Rounds the result down to the nearest whole number. + +So 66.8 becomes 67. + ++ minimum + +This shifts the range from 0–99 to 1–100. + +So if Math.floor(...) gave 67, then adding 1 gives 68. + + // 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 diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..5056067e6 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,16 @@ 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 +We don't want the computer to run these 2 lines - how can we solve this problem? +Single-line comment +Use // at the beginning of the line: + + +// This is just an instruction for the first activity +// We don't want the computer to run these 2 lines +Multi-line comment +Use /* */ for multiple lines: + +/* +This is just an instruction for the first activity. +We don't want the computer to run these 2 lines. +*/ + diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..418b3f5af 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,8 @@ const age = 33; age = age + 1; +// to be able to reassign the value we need to use let instead of const because the value of const can not be changed after it is set. +let age = 33; +age = age + 1; + + diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..928109a6f 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,9 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +// this code has order problem. const and let has to have it`s value has to be declared before you use it. +// to fix it we will change the order of the code. + +const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..dcdf7b5c9 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -7,3 +7,10 @@ const last4Digits = cardNumber.slice(-4); // Then run the code and see what error it gives. // Consider: Why does it give this error? Is this what I predicted? If not, what's different? // Then try updating the expression last4Digits is assigned to, in order to get the correct value + + +This will give an error because cardNumber is a number, and .slice() is a method that only works on strings or arrays — not numbers. +for this to work we need to change the card number to a string + +const cardNumber = "4533787178994213"; // use quotes to make it a string +const last4Digits = cardNumber.slice(-4); diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..cf980125e 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,13 @@ const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const 24hourClockTime = "08:53"; + +//Variable names in JavaScript cannot start with a number. +// so we have to change 12 to twelve and 24 to twentyFour + +const twelveHourClockTime = "08:53"; // 12-hour time (8:53 AM) +const twentyFourHourClockTime = "20:53"; // 24-hour time (8:53 PM) + +// we can also put it this way: + +const clock12 = "08:53"; +const clock24 = "20:53"; \ No newline at end of file From 66dbe935253c4329875f5d77b5eb030a7d7e20f6 Mon Sep 17 00:00:00 2001 From: Hilolarustam Date: Thu, 19 Jun 2025 13:45:52 +0100 Subject: [PATCH 2/3] percentage and time format tasks --- .../1-percentage-change.js | 55 +++++++++++++++++++ .../3-mandatory-interpret/2-time-format.js | 28 +++++++++- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..7f3cde350 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -20,3 +20,58 @@ console.log(`The percentage change is ${percentageChange}`); // d) Identify all the lines that are variable declarations // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + + +//answers + +// a) in total we have 5 function calls here: +Line 4: carPrice.replaceAll(",", "") + +Line 4: Number(...) + +Line 5: priceAfterOneYear.replaceAll("," "") — this line has an error (missing comma) + +Line 5: Number(...) + +Line 8: console.log(...) + + +//b) on line 5 there is a missing coma between arguments. (",""") should be (",","") + +//c) The variable reassignment statements are: + + Line 4: carPrice = Number(carPrice.replaceAll(",", "")); + +Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + +Line 8: console.log(`The percentage change is ${percentageChange}`); + +//d) The variable declarations are: + +// Line 1: let carPrice = "10,000"; + +// Line 2: let priceAfterOneYear = "8,543"; + +// Line 6: const priceDifference = carPrice - priceAfterOneYear; + +// Line 7: const percentageChange = (priceDifference / carPrice) * 100; + +// e) The expression + +carPrice.replaceAll(",", "") removes all commas from the string (e.g., turns "10,000" into "10000"). + +Number(...) converts the resulting string "10000" into a number type 10000. + +Purpose: To convert a formatted string number with commas into an actual number so you can do arithmetic on it. + + + + + + + + + + + + diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..acc24087f 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,36 @@ 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? +1) const movieLength = 8784; // length of movie in seconds + +2) const remainingSeconds = movieLength % 60; +3) const totalMinutes = (movieLength - remainingSeconds) / 60; + +4) const remainingMinutes = totalMinutes % 60; +5) const totalHours = (totalMinutes - remainingMinutes) / 60; + +6)const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; // b) How many function calls are there? +1)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 - +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Ope // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +This line is finding the number of seconds left over after dividing the total movie length by 60 seconds (1 minute). -// e) What do you think the variable result represents? Can you think of a better name for this variable? +movieLength is the total time of a movie in seconds. +60 is the number of seconds in a minute. + +% is the modulus operator — it gives you the remainder after division. + + +// e) What do you think the variable result represents? Can you think of a better name for this variable? +It represents the movie length formatted as hours, minutes, and seconds. +✅ A better name would be: +formattedTime +durationString +movieTimeFormatted // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +yes it works for different numbers \ No newline at end of file From 38306712fca12981aaa3014a6a63292c689e1d25 Mon Sep 17 00:00:00 2001 From: HilolaRustamova Date: Fri, 18 Jul 2025 20:50:29 +0100 Subject: [PATCH 3/3] task doe --- Sprint-1/3-mandatory-interpret/3-to-pounds.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..736d2b9a7 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -4,18 +4,27 @@ const penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1 ); +//Removes the trailing "p" from the string to isolate the numeric part. + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +//Ensures the numeric string is at least 3 characters long by padding it with leading zeroes if necessary. + const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2 ); - +//Extracts the pounds part from the padded string.Takes all but the last two digits to represent whole pounds. +//"399" → "3" const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); + // Extracts the last two digits to represent the pence (fractional part). + // "399" → "99" console.log(`£${pounds}.${pence}`); +// Formats and prints the final price in pounds and pence with a currency symbol. +// For "399p", output is: £3.99 // This program takes a string representing a price in pence // The program then builds up a string representing the price in pounds @@ -25,3 +34,6 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +//Removes the trailing "p" from the string to isolate the numeric part. +