diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..aef978c77 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/1-key-exercises/1-count.js @@ -1,6 +1,9 @@ let count = 0; count = count + 1; +console.log(count); // 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 using the assignment operator (=) to update the value of count. +// It reads the current value of count, adds 1 to it, and then stores the result back into count. +// So the value changes from 0 to 1. diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..804978abd 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..43eddfd46 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -17,7 +17,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 dir = ; -const ext = ; +const dir = filePath.slice(0, lastSlashIndex); +const ext = filePath.slice(filePath.lastIndexOf(".") + 1); +console.log({ dir, 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..644752d31 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -2,6 +2,7 @@ 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 diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..214eb7cc7 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,7 @@ -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 only for human consumption. +// We don't want the computer to run these 2 lines - how can we solve this problem? +// +// To stop JavaScript from running them, turn them into comments: +// +// const age = 33; +// age = age + 1; diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..4a89a12a3 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; +// Use let instead of const so the value can be reassigned. 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..cf934c587 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -1,5 +1,6 @@ // 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}`); const cityOfBirth = "Bolton"; +// Declare the variable before using it in the template literal. +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..08d02e30f 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -1,5 +1,7 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +// Convert the number to a string before slicing to get the last four digits. +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 diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..b0ea867af 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"; +// Variable names cannot begin with a number, so use descriptive names instead. +const twelveHourClockTime = "8:53pm"; +const twentyFourHourClockTime = "20:53"; +console.log(twelveHourClockTime); +console.log(twentyFourHourClockTime); diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..4dac49ada 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,27 @@ 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: +// - line 4: carPrice.replaceAll(",", "") +// - line 5: priceAfterOneYear.replaceAll(",", "") +// - line 4: Number(...) +// - line 5: Number(...) +// - line 9: 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 occurs at line 5 because the syntax is invalid: replaceAll("," "") is missing a comma between the arguments. +// Fix it by writing replaceAll(",", ""). // c) Identify all the lines that are variable reassignment statements +// - line 4: carPrice = Number(...) +// - line 5: priceAfterOneYear = Number(...) // 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; // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// It removes the commas from the string, then converts the cleaned string into a number. +// This is necessary because the prices are stored as strings like "10,000" and "8,543", and we need to perform arithmetic on them. diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..45134753d 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,25 @@ 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 +// % is the remainder operator. It gives the remainder when movieLength is divided by 60. +// For 8784 seconds, 8784 % 60 = 24, so there are 24 seconds remaining after full minutes are counted. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// totalMinutes = (movieLength - remainingSeconds) / 60 +// This removes the extra seconds left over after full minutes are taken out, then divides by 60 to convert the remaining whole seconds into total minutes. +// For 8784 seconds, this gives 146 minutes. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// result represents the time formatted as hours:minutes:seconds. +// A better name could be timeString or formattedTime. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// It works for any non-negative number of seconds, because it calculates hours, minutes, and seconds using division and remainders. +// It will not format correctly for negative values, and it assumes the value is in seconds rather than another unit. diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..66333adac 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,11 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" +// 2. penceString.substring(0, penceString.length - 1): removes the final "p" character so only the digits remain +// 3. padStart(3, "0"): ensures the value has at least 3 characters by adding leading zeros if needed +// 4. pounds = the first part of the padded string, excluding the last two digits, so the whole pounds part is preserved +// 5. pence = the final two digits, padded to 2 characters, to keep a valid pence value +// 6. console.log(`£${pounds}.${pence}`): prints the formatted result as a pounds and pence string + +// For "399p", the steps work like this: +// "399p" -> "399" -> "399" -> pounds = "3" and pence = "99" -> output "£3.99" diff --git a/Sprint-2/4-stretch-explore/chrome.md b/Sprint-2/4-stretch-explore/chrome.md index 962580b82..ec410d2f4 100644 --- a/Sprint-2/4-stretch-explore/chrome.md +++ b/Sprint-2/4-stretch-explore/chrome.md @@ -8,8 +8,12 @@ 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? +It shows a pop-up dialog box with the message "Hello world!" and pauses the browser until the user closes it. 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 displays a dialog box asking the user for input. + What is the return value of `prompt`? +The return value is the text entered by the user as a string. If the user cancels the prompt, it returns `null`. diff --git a/Sprint-2/4-stretch-explore/objects.md b/Sprint-2/4-stretch-explore/objects.md index 0216dee56..78a51d8c9 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -13,4 +13,11 @@ Try also entering `typeof console` Answer the following questions: What does `console` store? +`console` stores the browser's debugging object. It contains methods such as `log`, `error`, `warn`, and `assert` that let us print values and inspect program state. + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +The `.` is the property access operator. It means "look inside the `console` object and use the `log` or `assert` method stored there". +So `console.log(...)` means “call the `log` method on the `console` object”. + +Try also entering `typeof console`. +This returns `"object"`, because `console` is an object containing methods.