diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..8437e730c 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/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 + +// Line 3 takes the current value of count (0), adds 1 to it (0 + 1 = 1), and uses the assignment operator (=) to reassign and store this new value (1) back into the count variable. \ No newline at end of file diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..5d6fa589b 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -5,6 +5,7 @@ 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]; +console.log(initials); // 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..5f87d5e97 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -17,7 +17,9 @@ 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 ext = filePath.slice(filePath.lastIndexOf(".")); +console.log(dir); +console.log(ext); // https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-2/1-key-exercises/4-random.js b/Sprint-2/1-key-exercises/4-random.js index 292f83aab..3c42df6b7 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -3,7 +3,13 @@ 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? -// 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 +console.log(num); +// what does num represent? +// num represents a random whole number (integer) between minimum (1) and maximum (100) inclusive. + +//Breakdown of the expression: +// 1. Math.random() generates a decimal number from 0 (inclusive) up to, but not including, 1. +// 2. (maximum - minimum + 1) calculates the range size (100 - 1 + 1 = 100). +// 3. Math.random() * 100 scales the random decimal to a range between 0 and 99.999... +// 4. Math.floor() rounds that value down to the nearest whole integer (0 to 99). +// 5. + minimum (+ 1) shifts the final value up into the target range of 1 to 100. diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..65ad3030d 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +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? \ 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-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..6ef2ea979 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -1,4 +1,6 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; + +console.log(age); diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..642071c16 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -1,5 +1,7 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? +// Answer: cityOfBirth was referenced before it was declared (ReferenceError). -console.log(`I was born in ${cityOfBirth}`); 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..a13cdaf79 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -1,6 +1,8 @@ -const cardNumber = 4533787178994213; +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 diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..c24e9c352 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,5 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +const twelveHourClockTime = "8:53pm"; +const twentyFourHourClockTime = "20:53"; + +console.log(twelveHourClockTime); +console.log(twentyFourHourClockTime); \ No newline at end of file diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..a4446ee7a 100644 --- a/Sprint-2/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-2/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,20 @@ 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 +// Answer: There are 5 function/method calls across 3 lines: +// - Line 4: replaceAll(",", "") and Number(...) +// - Line 5: replaceAll(",", "") and Number(...) +// - Line 10: 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? +// 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? +// Answer: On line 5, there is a typo in replaceAll(",", "") where the comma is outside the quotes or contains empty space before the closing quote (""")). +// Fix line 5 by changing it to: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); // c) Identify all the lines that are variable reassignment statements +// Answer: Lines 4 and 5 (carPrice = ... and priceAfterOneYear = ...) // d) Identify all the lines that are variable declarations +// Answer: Lines 1, 2, 7, and 8 (let carPrice, let priceAfterOneYear, const priceDifference, const percentageChange) -// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// e) Describe what the expression Number(carPrice.replaceAll(",", "")) is doing – what is the purpose of this expression? +// Answer: .replaceAll(",", "") removes the comma from the price string ("10,000" becomes "10000"), and Number(...) converts that clean string into a numeric value so mathematical calculations can be performed on it \ 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..9363c4814 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,19 @@ 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? +// Answer: 6 variable declarations (movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, result). // b) How many function calls are there? +// Answer: 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 +// Answer: % is the remainder (modulo) operator. It calculates the leftover seconds when movieLength is divided into full minutes (60-second chunks). // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// Answer: It subtracts the leftover seconds from the total movie length to get a exact multiple of 60, then divides by 60 to convert that duration into whole minutes. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// Answer: It represents the movie length formatted as a HH:MM:SS time string. A clearer name would be formattedTime or formattedMovieLength. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// Answer: It works for all non-negative numbers representing total seconds. However, if movieLength is negative or not a number (e.g., a string or null), it will output invalid time values or NaN. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..fbe7e8785 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -23,5 +23,14 @@ console.log(`£${pounds}.${pence}`); // You need to do a step-by-step breakdown of each line in this program // 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": Initialises a string variable with the value "399p" representing the price in pence including the trailing letter 'p'. + +// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): Extracts the numeric portion of the pence string by taking characters from index 0 up to (but not including) the last character, removing the 'p' ("399"). + +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): Ensures the pence string is at least 3 digits long by adding leading zeros if necessary (e.g., "5" becomes "005"), ensuring there are enough digits to extract both pounds and pence. + +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): Extracts the pounds portion of the price by taking all digits except the last two ("3"). + +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): Extracts the last two digits representing pence ("99") and ensures it is padded to 2 digits. + +// 6. console.log(`£${pounds}.${pence}`): Formats and prints the final price string with the pound symbol, pounds amount, period, and pence amount ("£3.99"). \ No newline at end of file diff --git a/Sprint-2/4-stretch-explore/chrome.md b/Sprint-2/4-stretch-explore/chrome.md index 962580b82..3629b5ef1 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? - +Answer: Calling alert("Hello world!") displays a pop-up dialog box in the browser window containing the message "Hello world!" and an "OK" button that pauses script execution until dismissed. 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? +Answer: Calling prompt("What is your name?") displays a pop-up dialog box containing the message, a text input field, an "OK" button, and a "Cancel" button. + What is the return value of `prompt`? +Answer: It returns the string entered by the user in the input field when they click "OK". If the user clicks "Cancel" or closes the dialog, it returns null. diff --git a/Sprint-2/4-stretch-explore/objects.md b/Sprint-2/4-stretch-explore/objects.md index 0216dee56..42f9ba535 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -5,12 +5,18 @@ 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? +Answer: It outputs the function definition itself, showing `ƒ log() { [native code] }`. Now enter just `console` in the Console, what output do you get back? +Answer: It returns the `console` object containing various built-in properties and logging methods. Try also entering `typeof console` +Answer: It outputs `"object"`. Answer the following questions: What does `console` store? +Answer: `console` is a built-in global object that stores properties and methods used to output messages, errors, and warnings to the browser console. + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +Answer: The dot (`.`) is the property accessor operator. It is used to access specific methods (`log`, `assert`) attached to the `console` object. \ No newline at end of file