From 5ed6319c42e6fb53f35965d9f2ab8fdc34893481 Mon Sep 17 00:00:00 2001 From: Ayodeji Ayorinde Date: Fri, 17 Apr 2026 16:03:30 +0100 Subject: [PATCH 1/2] Exercises redone --- Sprint-1/1-key-exercises/1-count.js | 4 +++ Sprint-1/1-key-exercises/2-initials.js | 4 ++- Sprint-1/1-key-exercises/3-paths.js | 17 ++++++++-- Sprint-1/1-key-exercises/4-random.js | 8 +++++ Sprint-1/2-mandatory-errors/0.js | 7 +++-- Sprint-1/2-mandatory-errors/1.js | 10 ++++++ Sprint-1/2-mandatory-errors/2.js | 8 +++++ Sprint-1/2-mandatory-errors/3.js | 15 +++++++++ Sprint-1/2-mandatory-errors/4.js | 8 +++-- .../1-percentage-change.js | 31 ++++++++++++++++--- .../3-mandatory-interpret/2-time-format.js | 25 +++++++++++++++ Sprint-1/3-mandatory-interpret/3-to-pounds.js | 26 ++++++++++++++++ 12 files changed, 151 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..3e42ecf50 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,7 @@ 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 +//ANS: +// Line 3 is incrementing the variable. +// It evaluates the expression on the right using the variable's current state, +// then uses the = operator to store that new result back into the variable, effectively updating it. diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..18c70dfa8 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]}`; + +console.log(initials); // Output: "CKJ" // https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..c83975718 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -11,13 +11,26 @@ const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; const lastSlashIndex = filePath.lastIndexOf("/"); +// 1. Get the 'base' (file.txt) 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 = ; +//const dir = ; +//const ext = ; + +// 2. Get the 'dir' (/Users/mitch/cyf/Module-JS1/week-1/interpret) +// We slice from the start up to the last slash +const dir = filePath.slice(0, lastSlashIndex); + +// 3. Get the 'ext' (.txt) +// We find the last dot and slice from there to the end +const lastDotIndex = filePath.lastIndexOf("."); +const ext = filePath.slice(lastDotIndex); + +console.log(`Dir: ${dir}`); +console.log(`Ext: ${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..4cd5a54ee 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,11 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // 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 + +//ANS: +//num represents a randomly generated integer between a specified minimum (1) and maximum (100), inclusive. +//Math.random()This function returns a floating-point (decimal) number that is greater than or equal to $0$ but strictly less than $1$ +//(maximum - minimum + 1) represents the total number of possible integers within the range (from 1 to 100) +// Math.random() * 100: multiplication of random decimal by range. +// Adding the minimum value (1) to the result "shifts" the entire range upward so it doesn't start at zero. +//Math.floor() rounds a number down to the nearest whole integer. \ 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..18441a8d5 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,5 @@ -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? + +//ANS: +//Use two forward slashes // \ 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..10e1fb0df 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,13 @@ const age = 33; age = age + 1; + + +//ANS: +//Use let +//let allows you to create a variable whose value can be reassigned as many times as you like. + +let age = 33; // Use 'let' instead of 'const' +age = age + 1; + +console.log(age); \ 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..edc2464e5 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,11 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + + +//ANS: +//"Cannot access 'cityOfBirth' before initialization." +//SOlution: + +const cityOfBirth = "Bolton"; // 1. Define it first +console.log(`I was born in ${cityOfBirth}`); // 2. Use it second diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..ee00d584e 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -4,6 +4,21 @@ 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 + +//ANS: code will throw an error // 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? + +//ANS: +//The .slice() method is a String (and Array) method. cardNumber is defined as a Number. Numbers in JavaScript do not have a .slice() property + // Then try updating the expression last4Digits is assigned to, in order to get the correct value + + +//ANS: +const cardNumber = 4533787178994213; + +// Convert to string first, then slice +const last4Digits = cardNumber.toString().slice(-4); + +console.log(last4Digits); // "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..478f1ee87 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,6 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +//const 12HourClockTime = "20:53"; +//const 24hourClockTime = "08:53"; + +//FIx: +const time12Hour = "08:53 PM"; +const time24Hour = "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..a624a1284 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -12,11 +12,32 @@ 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 - -// 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? - +//ANS:There are 6 function calls in total. +carPrice.replaceAll(",", "")(Line 4) +Number(...)(Line 4) +priceAfterOneYear.replaceAll("," "")(Line 5) +Number(...)(Line 5) +console.log(...)(Line 10) +percentageChange inside the template literal(the engine calls a string conversion here). + // 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? + //ANS: error here -> priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + //FIX: + priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); // c) Identify all the lines that are variable reassignment statements - +//ANS: +carPrice = Number(carPrice.replaceAll(",", "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); // d) Identify all the lines that are variable declarations - +//ANS: +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? +//ANS: +//This expression is performing Data Cleaning and Type Conversion. +carPrice.replaceAll(",", "") // This finds every comma in the string "10,000" and removes it, resulting in the string "10000". + +Number(...) //This takes that cleaned string and converts it into an actual Number data type. + +//The Purpose: Computers cannot perform mathematical calculations(like division or subtraction) on strings that contain formatting characters like commas. diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..e78b399cb 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,39 @@ 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? +//ANS: There are 6 variable declarations. +//movieLength +//remainingSeconds +//totalMinutes +//remainingMinutes +//totalHours +//result // b) How many function calls are there? +//ANS: One explicit 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 +//ANS: The % symbol is the Remainder Operator, according to the documentation, it returns the remainder left over when one operand is divided by a second operand. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +//ANS: +//This expression calculates the total number of whole minutes contained within the movieLength. +//(movieLength - remainingSeconds): This subtracts the "leftover" seconds so that the number is perfectly divisible by 60. + +// The / 60: This divides that number by 60 to convert the unit from seconds to minutes. + // e) What do you think the variable result represents? Can you think of a better name for this variable? +//ANS: The result variable represents the duration of the movie formatted as a timestamp (HH:MM:SS). +//Better names for this variable: +//formattedDuration +//MovieTimestamp +//timeString // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +//ANS: Mathematically: It will work for any positive number. +//It does not handle padding. If remainingSeconds is 5, the output will look like 2:26:5. Standard timestamps expect two digits (e.g., 2:26:05). +//If movieLength is a negative number or a non-number (like a string), the math will result in NaN (Not a Number) or unexpected logic. diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..2ef5b10b7 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,29 @@ 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); +//Purpose: To strip away the "p" character so we are left with only the digits. +//Rationale: You cannot perform formatting or math on the letter "p".substring(0, length - 1) says "start at the beginning and take everything except the very last character."Result: "399" + +//3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +//Purpose: To ensure the string is at least 3 characters long by adding a leading "0" if necessary. +//Rationale: This is a "safety" step.If the input was "5p", the previous step would give us "5".By padding it to "005", we ensure there are always enough digits to represent both pounds and pence(e.g., £0.05). +//Result: "399"(No change here because it's already 3 digits, but "5" would become "005"). + +//4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); +//Purpose: To extract the digits that represent the "pounds" portion of the price. +//Rationale: In a pence - to - pounds conversion, everything except the last two digits represents the pounds.By taking the substring from the start up to the "length minus 2," we grab that "left side" of the decimal point. +// Result: "3" + +//5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); +//Purpose: To extract the last two digits(the pence) and ensure they are formatted correctly. +// Rationale: * .substring(length - 2) grabs the final two characters. + +// .padEnd(2, "0") is a double - safety measure; if for some reason we only had one digit, it would add a zero to the end(e.g., turning "5" into "50"). +// Result: "99" + +//6. console.log("£${pounds}.${pence}"); +//Purpose: To combine the processed pieces into a human - readable currency format. +// Rationale: This uses a template literal to inject the pounds and pence variables into a string, separated by a decimal point and prefixed with a "£" symbol. +// Final Output: "£3.99" \ No newline at end of file From 85fdb3d9c029f1b0cd6148ef638b3eee0f22dcc7 Mon Sep 17 00:00:00 2001 From: Ayodeji Ayorinde Date: Fri, 17 Apr 2026 16:12:20 +0100 Subject: [PATCH 2/2] Added Stretch --- Sprint-1/4-stretch-explore/chrome.md | 8 +++++++- Sprint-1/4-stretch-explore/objects.md | 11 ++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..5f0512f49 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? - +## Got "chrome://new-tab-page says" popup +## 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`. What effect does calling the `prompt` function have? +## Got "chrome://new-tab-page says" popup +## Got "What is your name?" +## Got a box for input What is the return value of `prompt`? +## promt: ƒ prompt() { [native code] } +## myName: Ayo \ No newline at end of file diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..634395c60 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? - +## ƒ log() { [native code] } Now enter just `console` in the Console, what output do you get back? - +## console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} Try also entering `typeof console` - +## 'object' Answer the following questions: What does `console` store? +## Objects +## console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +## console.log - Prints the text to the Console as a log message +## console.assert() writes an error message to the console if the assertion is false +## The dot(.) allows the access of the log method inside the console object \ No newline at end of file