From b95254c4ee016e4dcd961f07648e98479fe991e8 Mon Sep 17 00:00:00 2001 From: JenniferIsidienu Date: Fri, 25 Jul 2025 12:08:19 +0100 Subject: [PATCH] Sprint 1 assignment --- Sprint-1/1-key-exercises/1-count.js | 6 ++++ Sprint-1/1-key-exercises/2-initials.js | 3 +- Sprint-1/1-key-exercises/3-paths.js | 13 +++++++-- Sprint-1/1-key-exercises/4-random.js | 7 +++++ Sprint-1/2-mandatory-errors/0.js | 4 ++- Sprint-1/2-mandatory-errors/1.js | 9 ++++-- Sprint-1/2-mandatory-errors/2.js | 9 +++++- Sprint-1/2-mandatory-errors/3.js | 14 ++++++++- Sprint-1/2-mandatory-errors/4.js | 7 +++-- .../1-percentage-change.js | 29 ++++++++++++++++++- .../3-mandatory-interpret/2-time-format.js | 26 +++++++++++++++++ Sprint-1/3-mandatory-interpret/3-to-pounds.js | 18 ++++++++++++ Sprint-1/4-stretch-explore/chrome.md | 4 +++ Sprint-1/4-stretch-explore/objects.md | 10 +++++++ 14 files changed, 146 insertions(+), 13 deletions(-) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..33f4c0336 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,9 @@ 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 +// Line 3 is updating the value of the count variable by adding 1 to its current value. +// The = operator is used for assignment, meaning it takes the value on the right (count + 1) +// and assigns it to the variable on the left (count). +// The expression count + 1 calculates the new value, and the = operator updates +// the count variable with this new value. +// The final value of the count variable is now 1. \ 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..b88a01d60 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,8 @@ 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); // "CKJ" \ 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..2cbf3fb80 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -12,12 +12,19 @@ const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; const lastSlashIndex = filePath.lastIndexOf("/"); const base = filePath.slice(lastSlashIndex + 1); -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 = ; +// Get the directory path (everything before the base) +const dir = filePath.slice(0, lastSlashIndex); + +// Get the extension (everything after the last dot in the base) +const lastDotIndex = base.lastIndexOf("."); +const ext = base.slice(lastDotIndex); + +console.log(`The base part of ${filePath} is ${base}`); +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..926c84795 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -2,8 +2,15 @@ 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 integer between 1 and 100 +// The expression Math.random() generates a random floating-point number between 0 (inclusive) and 1 (exclusive). +// Multiplying this by (maximum - minimum + 1) scales the range to the desired size. +// Adding minimum shifts the range to start at the minimum value. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..c65712a64 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,4 @@ 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? + +We can use comment to ignore the two lines so the computer doesn't run them. diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..a31c6fe63 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,7 @@ -// trying to create an age variable and then reassign the value by 1 -const age = 33; -age = age + 1; +// Trying to create an age variable and then reassign the value by 1 + +let age = 33; // Create a changeable variable +age = age + 1; // Increase the value by 1 + +console.log("New age:", age); diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..84ab89d08 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,12 @@ // 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}`); +// console.log(`I was born in ${cityOfBirth}`); +// const cityOfBirth = "Bolton"; + +// The error occurs because the variable `cityOfBirth` is being used before it has been declared. +// In JavaScript, variables declared with `const` must be initialized before they can be used. +// To fix this, we need to declare the variable before using it in the console.log statement. + 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..3ed530cdc 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,7 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = String(cardNumber).slice(-4); +console.log(last4Digits); + // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +9,13 @@ 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 + +// The code will not work because `cardNumber` is a number, +// and the `slice` method is not available on numbers. +// The `slice` method is a string method, so we need to convert +// `cardNumber` to a string first. +// The prediction is correct, and the error stated: +// cardNumber.slice is not a function" +// After converting `cardNumber` to a string, the code now works correctly. +// The last4Digits variable now correctly stores the last 4 digits of cardNumber +// The Output is "4213" \ 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..15da3c206 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,5 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const twelveHourClockTime = "08:53"; +const twentyFourHourClockTime = "20:53"; + +console.log("12-hour clock time:", twelveHourClockTime); +console.log("24-hour clock time:", twentyFourHourClockTime); \ 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..e775de767 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,38 @@ 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 +// There are 5 function calls in this file: +// 1. `Number(carPrice.replaceAll(",", ""))` Line 4 +// 2. `carPrice.replaceAll(",", ""))` Line 4 +// 3. `Number(priceAfterOneYear.replaceAll(",", ""))` Line 5 +// 4. `priceAfterOneYear.replaceAll(",", "")` Line 5 +// 5. `console.log(...)` Line 10 // 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? +// The error is on line 5: +// `priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));` +// It occured because there is a missing comma between arguments in replaceAll() +// To fix this, change it to `replaceAll(",", "")` without the space. +// The corrected line should be: +// `priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));` + // c) Identify all the lines that are variable reassignment statements +// Line 4: `carPrice = Number(carPrice.replaceAll(",", ""));` +// Line 5: `priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));` +// Lines 4 and 5 are variable reassignment statements because they were first +// declared in lines 1 and 2. // d) Identify all the lines that are variable declarations +// Line 1: `let carPrice = "10,000";` +// Line 2: `let priceAfterOneYear = "8,543";` +// Line 7: `const priceDifference = carPrice - priceAfterOneYear;` +// Line 8: `const percentageChange = (priceDifference / carPrice) * 100;` +// Lines 1,2, 7 and 8 are variable declarations because they introduce new variables // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// The expression is converting the string representation of a car price, +// which includes commas, into a number. +// It first removes the commas from the string using `replaceAll(",", "")` +// and then converts the string into a number using the `Number()` function. + diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..d0cefb4bc 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,40 @@ 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 are 6 variable declarations: movieLength, remainingSeconds, +// totalMinutes, remainingMinutes, totalHours and result. // b) How many function calls are there? +// There is 1 function call: 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 +// The expression movieLength % 60 calculates the remainder when movieLength +// is divided by 60. +// It is used to find the remaining seconds after converting the total +// movie length from seconds to minutes. +// for instance, if movieLength is 8784 seconds, +// the expression 8784 % 60 will return 24, +// because 8784 divided by 60 is 146 with a remainder of 24. +// the expression will return 24, which means there are 24 seconds left after +// converting the total movie length to minutes // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// The expression assigned to totalMinutes converts the total movie length +// from seconds to minutes by removing the remaining seconds and dividing by 60. +// For example, if movieLength is 8784 seconds, +// the expression (8784 - 24) / 60 will return 146, +// which means the movie is 146 minutes long. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// The variable result represents the total movie length showing in +// "hours, minutes and seconds". +// A better name could be movieDuration. + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// Yes, this code will work for all values of movieLength. +// The code can handle any positive integer value for movieLength. +// It will correctly calculate the total hours, minutes, and seconds except +// for negative or decimal values. + diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..396d06996 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,21 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +// 2. const penceStringWithoutTrailingP = penceString.substring( +// 0, penceString.length - 1): removes the trailing 'p' from the string. + +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0") +// : pads the string with leading zeros to ensure it has at least 3 characters. + +// 4. const pounds = paddedPenceNumberString.substring +// (0, paddedPenceNumberString.length - 2): extracts the pounds part of the string +// by taking all characters except the last two because it represents the pence. + +// 5. const pence = paddedPenceNumberString.substring +// (paddedPenceNumberString.length - 2).padEnd(2, "0"): +// extracts the pence part of the string by taking the last two characters +// and ensuring it has exactly 2 characters by padding with zeros if necessary. + +// 6. console.log(`£${pounds}.${pence}`): prints the final formatted string +// in pounds and pence e.g. £3.99. diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..ead3ac22b 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -11,8 +11,12 @@ In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +It creates a popup message box in the browser that displays the message. 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`. What effect does calling the `prompt` function have? +It shows a popup dialog with a text input field and message. Also, the user can type something and press OK or Cancel. + What is the return value of `prompt`? +Once the user types something and clicks OK, the input text is returned as a string. If the user clicks Cancel, it returns null. diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..54b6e0599 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,22 @@ 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? +The output is: +ƒ log() { [native code] } Now enter just `console` in the Console, what output do you get back? +The output is: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} Try also entering `typeof console` +The output is:'object' Answer the following questions: What does `console` store? +It stores function for logging information, debugging and displaying messages in the browser's developer console. + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +`console.log` means to use the log function that belongs to the console object to print messages to the console.” +`console.assert` means to use the assert method from the console object to test if something is true. +The `.` is called dot notation and it is used It is used in javascript to access properties or methods of an object. +