diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..4372a96b8 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/1-key-exercises/1-count.js @@ -3,4 +3,6 @@ let count = 0; 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: first it calculates count + 1 (using the current value of count), +// then the = assignment operator stores that new result back into the count variable. +// So count 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..09ec66f13 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -5,6 +5,8 @@ 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.charAt(0)}${middleName.charAt(0)}${lastName.charAt(0)}`; // https://www.google.com/search?q=get+first+character+of+string+mdn + +console.log(initials) \ No newline at end of file diff --git a/Sprint-2/1-key-exercises/3-paths.js b/Sprint-2/1-key-exercises/3-paths.js index ab90ebb28..7211b373d 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); +console.log(dir); +const dotIndex = base.lastIndexOf("."); +const ext = base.slice(dotIndex); -// 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..52005843f 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -2,8 +2,10 @@ 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 whole number between 1 and 100 \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..e6b744a05 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? diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..e57b2e979 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -1,4 +1,5 @@ // 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..c441b2e77 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"; +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..581157a9e 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); +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,4 @@ 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 +//updated expression last4Digits to a String. diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..6538b40f5 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"; +const clockTime12Hour = "8:53pm"; +const clockTime24Hour = "20:53"; +console.log(clockTime12Hour); +console.log(clockTime24Hour); + +//variable names can not start with numbers diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..eb056b854 100644 --- a/Sprint-2/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-2/3-mandatory-interpret/1-percentage-change.js @@ -2,21 +2,35 @@ 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; -console.log(`The percentage change is ${percentageChange}`); +console.log(`The price difference is ${priceDifference}`); + + +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: 6 function calls. // 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: line 5 {replaceAll(",", ""))} added , to fix the SyntaxError. // c) Identify all the lines that are variable reassignment statements +// Answer: carPrice = Number(carPrice.replaceAll(",", "")); line 4 +// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); line 5 // d) Identify all the lines that are variable declarations +// Answer: let carPrice = "10,000"; line1 +// let priceAfterOneYear = "8,543"; line 2 +// const priceDifference = carPrice - priceAfterOneYear; line 7 +// const percentageChange = (priceDifference / carPrice) * 100; line 8 // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// Answer: carPrice.replaceAll(",","") remove commas from the string. +// Number(...) changes the text to a number so subtraction and division work correctly. + diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..cff89a64e 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = 9325; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,14 +12,32 @@ 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. // b) How many function calls are there? +// Answer: 1 function call. // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// Answer: The % operator is the remainder (modulo) operator. +// movieLength % 60 gives the remainder after dividing the total seconds by 60. +// That remainder is the number of seconds left after removing complete minutes. +// Example: 8784 % 60 === 24. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// Answer: (movieLength - remainingSeconds) / 60 +// First it subtracts the leftover seconds so the value divides evenly by 60, +// then it divides by 60 to get the total number of whole minutes in the movie. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// Answer: result stores the movie length formatted as hours:minutes:seconds +// (for this value: "2:26:24"). +// A clearer name would be formattedTime. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// Answer: It works for non-negative whole numbers of seconds and correctly +// splits time into hours, minutes, and seconds. +// Limitations: +// - fractional values can produce messy decimals +// - negative values do not make sense for a movie length. +// Note: I have changed the movieLength from "8784" to "9325" for testing. \ 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..93262949f 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -23,5 +23,39 @@ 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" +//Answer: + +// 1. const penceString = "399p" +// Initialises a string variable with the value "399p" +// (price written in pence, with a trailing "p"). + +// 2. penceString.substring(0, penceString.length - 1) +// Removes the final character "p". +// For "399p" this becomes "399". +// Stored in penceStringWithoutTrailingP. + +// 3. penceStringWithoutTrailingP.padStart(3, "0") +// Ensures the string is at least 3 characters long by padding +// with "0" on the left if needed. +// "399" stays "399". +// Example: "5" would become "005", "42" would become "042". +// This makes it easier to always treat the last 2 digits as pence. + +// 4. paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2) +// Takes everything except the last 2 characters → the pounds part. +// For "399" this is "3". + +// 5. paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) +// Takes only the last 2 characters → the pence part. +// For "399" this is "99". + +// 6. .padEnd(2, "0") on the pence part +// Ensures pence is always 2 digits (adds "0" on the right if needed). +// "99" stays "99". +// Example: if pence were "5", it would become "50" with padEnd — +// (note: for money, padStart is usually more common for the numeric part; +// here the code uses padEnd as written). + +// 7. console.log(`£${pounds}.${pence}`) +// Prints the final price in pounds format. +// For this input: £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..2869acec4 100644 --- a/Sprint-2/4-stretch-explore/chrome.md +++ b/Sprint-2/4-stretch-explore/chrome.md @@ -13,3 +13,7 @@ Now try invoking the function `prompt` with a string input of `"What is your nam What effect does calling the `prompt` function have? What is the return value of `prompt`? +## Answers +- alert("Hello world!") shows a browser alert dialog with that message. +- prompt("What is your name?") shows an input dialog asking for a name. +- prompt returns the typed string if OK is pressed, or null if Cancel is pressed. diff --git a/Sprint-2/4-stretch-explore/objects.md b/Sprint-2/4-stretch-explore/objects.md index 0216dee56..302e0a8bf 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -14,3 +14,9 @@ Answer the following questions: What does `console` store? What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +## Answers +- console.log is a function (native code). +- console is an object containing logging methods. +- typeof console is "object". +- console stores the browser/devtools console API object. +- The dot `.` means property access: console.log is the log property (a function) on the console object.