From 719ac3852288d2292f42882120ea88e5c579b5a2 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Tue, 8 Sep 2026 14:25:51 +0100 Subject: [PATCH 1/2] completed tasks on key-exercises, mandatory-errors --- Sprint-2/1-key-exercises/1-count.js | 1 + Sprint-2/1-key-exercises/2-initials.js | 2 +- Sprint-2/1-key-exercises/3-paths.js | 8 ++++---- Sprint-2/1-key-exercises/4-random.js | 2 ++ Sprint-2/2-mandatory-errors/0.js | 5 +++-- Sprint-2/2-mandatory-errors/1.js | 5 ++++- Sprint-2/2-mandatory-errors/2.js | 5 +++++ Sprint-2/2-mandatory-errors/3.js | 6 ++++++ Sprint-2/2-mandatory-errors/4.js | 4 ++++ 9 files changed, 30 insertions(+), 8 deletions(-) diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..b0f0ef3b5 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/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 +// line 3 is updating the value of the count variable by adding 1 to the current value of count. The = operator is used to assign the new value (count + 1) back to the count variable, effectively incrementing its value by 1. diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..d5b6c5cc9 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -5,6 +5,6 @@ const 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. -const initials = ``; +const initials = firstName[0] + middleName[0] + lastName[0]; // the expected output is "CKJ" // https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-2/1-key-exercises/3-paths.js b/Sprint-2/1-key-exercises/3-paths.js index ab90ebb28..140fe2a64 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -16,8 +16,8 @@ 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 lastDotIndex = base.lastIndexOf("."); +const dir = filePath.slice(0, lastSlashIndex); +const ext = base.slice(lastDotIndex + 1, base.length); -const dir = ; -const ext = ; - -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +// https://www.google.com/search?q=slice+mdn diff --git a/Sprint-2/1-key-exercises/4-random.js b/Sprint-2/1-key-exercises/4-random.js index 292f83aab..1b4d4c4fe 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -4,6 +4,8 @@ const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // In this exercise, you will need to work out what num represents? +// num is a random integer between the minimum and maximum values (inclusive) and is a calculated value from this clearly explained expression below. // Try breaking down the expression and using documentation to explain what it means +/*The expresssion Math.random() generates a random floating-point number between 0 (inclusive) and 1 (inclusive). By multiplying this value by (maximum - minimum + 1), we scale it to the desired range. The Math.floor() function is then used to round down to the nearest whole number, and finally, we add the minimum value to shift the range to start from the minimum value instead of 0. The expected result is always a whole number between the minimum of 1 and the maximum of 101 (inclusive).*/ // 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 diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..4ad677159 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,3 @@ -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 +// by commenting out these two lines using // for single-line comments or /* */ for multi-line comments, we can prevent the computer from running them while still keeping the instructions visible for human readers or developers who may be reviewing the code.// +//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?// diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..cce20e443 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/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; +let age = 33; age = age + 1; + +or; +age += 1; diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..0c4150190 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -3,3 +3,8 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +// because we are trying to use the variable cityOfBirth before it has been declared and assigned a value. + +//here's the corrected code:// +//const cityOfBirth = "Bolton";// +//console.log(`I was born in ${cityOfBirth}`);// diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..7b8f00d46 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -4,6 +4,12 @@ const last4Digits = cardNumber.slice(-4); // 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 +// This will result in an error because the slice method is not defined for numbers. To fix this, we need to convert the cardNumber variable to a string before calling the slice method on it. // 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 + +//here is the corrected code:// +/*const cardNumber = "4533787178994213"; +const last4Digits = cardNumber.slice(-4); +*/ diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..f860666d9 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,6 @@ const 12HourClockTime = "8:53pm"; const 24hourClockTime = "20:53"; +// this will result in an error because variable names cannot start with a number. To fix this, we can rename the variables to start with a letter or an underscore. For this reason, we can change the variable names to something like hour12ClockTime and hour24ClockTime. This will make the variable names valid and prevent the error from occurring. +//here is the corrected code:// +/*const hour12ClockTime = "8:53pm"; +const hour24ClockTime = "20:53";*/ \ No newline at end of file From 181a0dbb5b71c20bb826226e0087a08f1a904012 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Wed, 9 Sep 2026 10:42:36 +0100 Subject: [PATCH 2/2] completed the tasks on interpret and stretch --- .../1-percentage-change.js | 9 ++-- .../3-mandatory-interpret/2-time-format.js | 41 +++++++++++++++++-- Sprint-2/3-mandatory-interpret/3-to-pounds.js | 4 +- Sprint-2/4-stretch-explore/chrome.md | 4 +- Sprint-2/4-stretch-explore/objects.md | 9 ++-- 5 files changed, 53 insertions(+), 14 deletions(-) diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..38c2dbf78 100644 --- a/Sprint-2/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-2/3-mandatory-interpret/1-percentage-change.js @@ -12,11 +12,12 @@ 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 - +// 5 function calls: Number(...), replaceAll(",", ""), replaceAll("," ""), Number(...), 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? - +// The error is occurring on line 5 because there is a missing comma in the replaceAll method. The correct syntax should be replaceAll(",", ""). // c) Identify all the lines that are variable reassignment statements - +// 2 variable reassignment statements: line 4 and line 5 // d) Identify all the lines that are variable declarations - +// 4 variable declarations: line 1, line 2, line 7, line 8 // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// The expression Number(carPrice.replaceAll(",", "")) is first using the replaceAll method to remove all commas from the carPrice string, resulting in a string that represents a number without any formatting. Then, the Number function is used to convert that string into a numeric value. The purpose of this expression is to convert the formatted string representation of the car price into a usable numeric value for calculations. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..585b30f48 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,47 @@ 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? - +// 6 variable declarations: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, result // b) How many function calls are there? - +// only one 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 of the division of movieLength by 60, which gives the number of seconds remaining after converting the total length of the movie from seconds to minutes. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? - +// line 4 calculates the total number of minutes in the movie by subtracting the remaining seconds from the total movie length in seconds and then dividing that value by 60, thus converting the total length of the movie from seconds to minutes. // 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 length of the movie in hours, minutes, and seconds format. A better name for this variable could be movieDuration or formattedMovieLength, as it more clearly describes the purpose of the variable. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +/* for positive integers:movieLength = 8784 → 2:26:24 + +movieLength = 60 → 0:1:0 + +movieLength = 3599 → 0:59:59 + +movieLength = 0 → 0:0:0 + +for negative integers: movieLength = -60 -> -1:59:0 +movieLength = -3599 -> -1:0:1 +movieLength = -8784 -> -3:33:36 +it works for negative integers as well, but the output may not be meaningful or should have validation to prevent negative values. + +for non-integer values or decimal values: movieLength = 60.5 → 0:1:0.5 +movieLength = 3599.9 → 0:59:59.9 +movieLength = 8784.7 → 2:26:24.7 +it works for non-integer values as well, but the output may not be meaningful or should have validation to prevent non-integer values. + +for non-numeric values: movieLength = "abc" → NaN:NaN:NaN +movieLength = null → 0:0:0 +movielength = undefined → NaN:NaN:NaN +it doesn't work for non-numeric values. + +for very large values: movieLength = 1000000000 → 277777:46:40 +movieLength = 1000000000000 → 277777777:46:40 +it works for very Large values as well, but the output may not be meaningful or should have validation to prevent very Large values. + +for very small values: movieLength = 0.0001 → 0:0:0 +movieLength = -0.0001 → 0:0:0 +movieLength = 0.0000001 → 0:0:0 +it works for very small values as well, but the output may not be meaningful or should have validation to prevent very small values. + diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..f47e26f2c 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -2,13 +2,13 @@ const penceString = "399p"; const penceStringWithoutTrailingP = penceString.substring( 0, - penceString.length - 1 + penceString.length - 1, ); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); const pounds = paddedPenceNumberString.substring( 0, - paddedPenceNumberString.length - 2 + paddedPenceNumberString.length - 2, ); const pence = paddedPenceNumberString diff --git a/Sprint-2/4-stretch-explore/chrome.md b/Sprint-2/4-stretch-explore/chrome.md index 962580b82..80713a054 100644 --- a/Sprint-2/4-stretch-explore/chrome.md +++ b/Sprint-2/4-stretch-explore/chrome.md @@ -8,8 +8,10 @@ Let's try an example. In the Chrome console, invoke the function `alert` with one argument, the string `"Hello world!"`; What effect does calling the `alert` function have? - +a pop-up box with a message of HELLO WORLD on the window 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? +the effect propmt displayed on the window is a pop-up box with an empty field to fill and with the string on top of it and when a user filled the field in. The filled in value gets displayed on the console. What is the return value of `prompt`? +// the return value of the prompt as explained above is the answer or the value the user puts in the prompted field on the window. diff --git a/Sprint-2/4-stretch-explore/objects.md b/Sprint-2/4-stretch-explore/objects.md index 0216dee56..b63a318ec 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -5,12 +5,15 @@ 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? - +ƒ log() { [native code] } Now enter just `console` in the Console, what output do you get back? - +with a dropdown menu icon next to console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} Try also entering `typeof console` - +'object' Answer the following questions: What does `console` store? +console is a regular javascript object provided by the browser or node and stores functions and/or properties. +As the dot next to it is a property accessor on an object, console is an object that is built to store functions. What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +The "." is a property access on the object and means get something inside this object.