From 333cdffac0a5babd221fbd577fa0150ae1517d11 Mon Sep 17 00:00:00 2001 From: Yama948 Date: Sat, 26 Sep 2026 00:05:02 +0200 Subject: [PATCH] sprint 2 solutions --- Sprint-2/1-key-exercises/1-count.js | 10 +++++++++- Sprint-2/1-key-exercises/2-initials.js | 10 +++++++++- Sprint-2/1-key-exercises/3-paths.js | 5 ++++- Sprint-2/1-key-exercises/4-random.js | 5 +++++ Sprint-2/2-mandatory-errors/0.js | 4 +++- Sprint-2/2-mandatory-errors/1.js | 9 +++++++++ Sprint-2/2-mandatory-errors/2.js | 3 +++ Sprint-2/2-mandatory-errors/3.js | 9 +++++++++ Sprint-2/2-mandatory-errors/4.js | 2 ++ .../3-mandatory-interpret/1-percentage-change.js | 6 ++++++ Sprint-2/3-mandatory-interpret/2-time-format.js | 13 +++++++++++++ Sprint-2/3-mandatory-interpret/3-to-pounds.js | 13 +++++++++++++ Sprint-2/4-stretch-explore/chrome.md | 8 ++++++++ Sprint-2/4-stretch-explore/objects.md | 10 ++++++++++ 14 files changed, 103 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..9e16b7f79 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/1-key-exercises/1-count.js @@ -3,4 +3,12 @@ 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 +// Describe what line 3 is doing, in particular focus on what = means +// += means "put this value in the box." + +Line 3 is saying: take whatever's currently in the `count` box (that's `0`), add 1 to it, and shove the answer back in the same box. + +So it goes: `0 + 1 = 1`, and now `count` holds `1` instead of `0`. + +That's really it — `=` in code just means "store this," not "these are the same thing." diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..6bbcf2f87 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -7,4 +7,12 @@ const lastName = "Johnson"; const initials = ``; -// https://www.google.com/search?q=get+first+character+of+string+mdn +// https://www.google.com/search?q=get+first+character+of+string +// + grab the first letter of each string using `[0]`, and glue them together: + +```javascript +const initials = firstName[0] + middleName[0] + lastName[0]; +``` + +`firstName[0]` grabs whatever's sitting at position 0 in the string — the first letter — so that's `"C"`. Same deal for the other two, giving you `"K"` and `"J"`. The `+` just sticks the three letters together into one string, so you end up with `"CKJ"` \ 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..bf42c2fec 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -20,4 +20,7 @@ console.log(`The base part of ${filePath} is ${base}`); 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 + +const dir = filePath.slice(0, lastSlashIndex); +const ext = base.slice(base.lastIndexOf(".")); \ 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..ef06b887b 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -7,3 +7,8 @@ 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 + +Math.random() gives you a random decimal between 0 and just-under-1 — like 0.628.... +Then you multiply that by 100 (which is maximum - minimum + 1), stretching it out into a decimal between 0 and just-under-100. +Math.floor() chops off everything after the decimal point, rounding down, you've got a whole number from 0 to 99. + + minimum (which is +1) shifts the whole thing up by one, so instead of 0–99 you get 1–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..c0a592240 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,4 @@ 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 +We don't want the computer to run these 2 lines - how can we solve this problem? + +by adding // infront of each line before writing a text/code \ 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..a657871fc 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -2,3 +2,12 @@ const age = 33; age = age + 1; + +const means "this value is locked in, you can't change it later." So when line 2 tries to reassign age, JavaScript throws: + +TypeError: Assignment to constant variable. +//instead use 'let' i.e let age = 33; +age = age + 1; + +console.log(age); // 34 +const for stuff that stays the same, let for stuff that's meant to change such as age. \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..923c81c92 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -3,3 +3,6 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +ReferenceError: Cannot access 'cityOfBirth' before initialization +JavaScript reads top to bottom, so at the point it hits that console.log, cityOfBirth doesn't exist yet. \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..50da89d1d 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -7,3 +7,12 @@ 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 + +cardNumber is just a plain number, and .slice() only exists on strings and arrays, not numbers +'TypeError: cardNumber.slice is not a function' +numbers just don't have a .slice() method built in. + +const cardNumber = 4533787178994213; +const last4Digits = cardNumber.toString().slice(-4); + +console.log(last4Digits); // "4213" \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..b9b067cdd 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,4 @@ const 12HourClockTime = "8:53pm"; const 24hourClockTime = "20:53"; + +it'll throw a SyntaxError before anything executes, because both variable names start with a number. \ 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..f956feadb 100644 --- a/Sprint-2/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-2/3-mandatory-interpret/1-percentage-change.js @@ -20,3 +20,9 @@ console.log(`The percentage change is ${percentageChange}`); // d) Identify all the lines that are variable declarations // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +a) 5 calls across 3 lines +b) error is on line 5, there's a missing comma between ',' and "" +c)reassignments on line 4 and 5 +d) declarations on lines 1,2,7,8 +e) carPrice starts as the string "10,000" — you can't do maths on that as-is, the comma would mess things up. So first, .replaceAll(",", "") strips out the comma, leaving "10000". Then Number(...) converts that string into an actual number, 10000, that you can subtract and divide like normal. \ 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..682f0b20c 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -23,3 +23,16 @@ console.log(result); // e) What do you think the variable result represents? Can you think of a better name for this variable? // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + + +a) There are 6 variable declarations: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result. + +b) There is 1 function call: console.log(result) on the last line. + +c) The % operator is the remainder (modulo) operator. It divides one number by another and returns whatever is left over after removing as many whole divisions as possible. movieLength % 60 divides the total seconds by 60 and returns the leftover seconds that don't make up a full minute. For example, 8784 % 60 equals 24, meaning after taking out all the full minutes, 24 seconds remain. + +d) Line 4 (totalMinutes = (movieLength - remainingSeconds) / 60) takes the total seconds and subtracts the leftover seconds calculated in the previous line, leaving a number of seconds that divides evenly into minutes. Dividing that by 60 converts it into the total number of whole minutes. For example, (8784 - 24) / 60 = 146 total minutes. + +e) The result variable represents the movie's length formatted as a string in hours:minutes:seconds format, e.g. "146:24:24" isn't quite right since totalHours still needs its own remainder split, but in this program's final form it produces something like "2:26:24". A more descriptive name would be formattedDuration or durationString, since "result" doesn't explain what it actually contains. + +f) The code works correctly for the underlying maths with any non-negative whole number of seconds, but it isn't fully robust: if minutes or seconds come out below 10, they display as a single digit instead of the conventional two digits (e.g. "2:6:4" instead of "2:06:04"). It also wouldn't behave correctly with negative numbers, since the % operator can return negative remainders in JavaScript, and decimal input would produce decimal values in the output since nothing in the code rounds or truncates. \ 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..2a1597484 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,16 @@ console.log(`£${pounds}.${pence}`); // 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 holding the price in pence, written with a trailing "p" the way a price tag might show it. + +2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): this chops off the last character of the string, removing the "p". substring(start, end) grabs everything from the start index up to (but not including) the end index, so here it takes everything except the final letter. "399p" becomes "399". + +3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): this pads the string with leading zeros if it's shorter than 3 characters. For "399" nothing changes since it's already 3 digits. This step exists as a safety net for prices under 100p — for example "45p" would become "45" after step 2, and padStart would turn that into "045", guaranteeing there are always at least 3 digits to work with. + +4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): this takes everything except the last 2 characters of the padded string. Since the last two digits always represent the pence, everything before them represents the pounds. For "399", this gives "3". + +5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): this takes just the last 2 characters of the string (the pence portion), then pads the end with a zero if it's somehow shorter than 2 characters as a safety measure. For "399", this gives "99". + +6. console.log(`£${pounds}.${pence}`): combines the pounds and pence pieces into a properly formatted currency string using a template literal, producing "£3.99". + diff --git a/Sprint-2/4-stretch-explore/chrome.md b/Sprint-2/4-stretch-explore/chrome.md index 962580b82..06c0100b5 100644 --- a/Sprint-2/4-stretch-explore/chrome.md +++ b/Sprint-2/4-stretch-explore/chrome.md @@ -13,3 +13,11 @@ 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`? + +Calling alert shows a popup box directly on the webpage with the message you passed in, along with an "OK" button. The rest of the page becomes unclickable and any code after it pauses until the popup is dismissed. + + + +Calling prompt shows a popup with a text input box, where the user can type a response, along with "OK" and "Cancel" buttons. + +The return value of prompt is whatever the user typed in, returned as a string. If the user clicks "Cancel" instead of "OK", it returns null instead. \ No newline at end of file diff --git a/Sprint-2/4-stretch-explore/objects.md b/Sprint-2/4-stretch-explore/objects.md index 0216dee56..12e3971fc 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -14,3 +14,13 @@ Answer the following questions: What does `console` store? What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? + +Typing console.log (no parentheses) and hitting enter shows something like ƒ log() { [native code] } + +Typing console on its own shows a big object with lots of properties, something like {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, ...} — + +Typing typeof console returns "object", confirming that console itself is an object. + +console is a built-in object provided by the browser that groups together a collection of related functions used for debugging like log, warn, error, and assert are all properties living inside this one object. + +The dot is called dot notation, and it's how you access something that lives inside an object. Since console is an object containing several functions, the dot lets you reach in and grab one specific one by name. So console.log means "go into the console object and get the log function," and console.assert means the same thing but for the assert function. Each of these is a different tool grouped together under the one console object.