diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..ea3bba947 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -2,5 +2,10 @@ 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 takes the current value of count (0), adds 1 to it, +// and assigns the result (1) back to the count variable using =. \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..5144635ae 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,16 @@ 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 = ``; + +// Solution logic + +// We want to combine those characters into one string. + +// So we concatenate: + +let 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-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..1076cacc4 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,15 @@ 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 = ; + +const dir = filePath.slice(0, lastSlashIndex); + +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..f6677159b 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -3,7 +3,10 @@ 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); + +// num is a random integer between 1 and 100 (inclusive). +// Math.random() returns a decimal in the range [0, 1). +// Multiplying by (maximum - minimum + 1) scales it to the size of the range. +// Math.floor(...) converts it to an integer. +// Adding minimum shifts the range to start at 1 instead of 0. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/answers.md b/Sprint-1/2-mandatory-errors/answers.md new file mode 100644 index 000000000..4417f3797 --- /dev/null +++ b/Sprint-1/2-mandatory-errors/answers.md @@ -0,0 +1,49 @@ +# Sprint 1 — Mandatory Errors (Explanations) + +## 0.js +- Error shown: +- Why it happens: +- MDN link (optional): +- Error shown: `SyntaxError: Unexpected identifier 'is'` on line 1. +- Why it happens: The file begins with plain English text that is not inside a comment or a quoted string. Node tries to interpret it as JavaScript code, but `This is ...` is not valid JS syntax, so parsing fails. +- MDN link (optional): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Unexpected_token + +## 1.js +- Error shown: +- Why it happens: +- MDN link (optional): +- Error shown: `TypeError: Assignment to constant variable.` on line 4. +- Why it happens: The variable `age` was declared with `const`, which prevents reassignment. The statement `age = age + 1` attempts to update the value, causing the error. +- MDN link (optional): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const + +## 2.js +- Error shown: +- Why it happens: +- MDN link (optional): +- Error shown: `ReferenceError: Cannot access 'cityOfBirth' before initialization`. +- Why it happens: The variable `cityOfBirth` is declared later in the file using `let` or `const`, but it is used before that declaration. JavaScript does not allow access to `let`/`const` variables before initialization (temporal dead zone). +- MDN link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_lexical_declaration_before_init + + +## 3.js +- Error shown: +- Why it happens: +- MDN link (optional): +### 3.js + +- Error shown: `TypeError: cardNumber.slice is not a function` +- Why it happens: The variable `cardNumber` is a number, and numbers do not have the `slice()` method. The `slice()` method is only available on strings and arrays. +- Concept: Methods depend on data types in JavaScript. +- MDN link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice + + +## 4.js +- Error shown: +- Why it happens: +- MDN link (optional): +### 4.js + +- Error shown: `SyntaxError: Invalid or unexpected token` +- Why it happens: The variable name `12HourClockTime` starts with a number. JavaScript identifiers cannot begin with a digit, so the parser throws a syntax error. +- Concept: Variable naming rules (identifiers must start with a letter, `_`, or `$`) +- MDN link: https://developer.mozilla.org/en-US/docs/Glossary/Identifier \ 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..ede0f73e6 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -2,8 +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; diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..85efdba75 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -22,4 +22,4 @@ 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 +// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/answers.md b/Sprint-1/3-mandatory-interpret/answers.md new file mode 100644 index 000000000..f0ae4cdaf --- /dev/null +++ b/Sprint-1/3-mandatory-interpret/answers.md @@ -0,0 +1,155 @@ +# Sprint 1 — Mandatory Interpret (Notes) + +## 1-percentage-change.js +- What the program does: +- Inputs: +- Output: +- Unfamiliar syntax (with link): +- What I changed / why: + + +## 2-time-format.js +- What the program does: +- Inputs: +- Output: +- Unfamiliar syntax (with link): +- What I changed / why: + +## 3-to-pounds.js +- What the program does: +- Inputs: +- Output: +- Unfamiliar syntax (with link): +- What I changed / why: + + +## 1-percentage-change.js + +### a) How many function calls are there? +There are 5 function calls: +- Number(carPrice.replaceAll(",", "")) +- carPrice.replaceAll(",", "") +- Number(priceAfterOneYear.replaceAll(",", "")) +- priceAfterOneYear.replaceAll(",", "") +- console.log(...) + +--- + +### b) Where did the error occur and why? +The error occurred on the line: +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + +The replaceAll function was missing a comma between its arguments. The correct syntax requires two arguments: replaceAll(search, replacement). + +Fix: +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); + +--- + +### c) Variable reassignment statements +- carPrice = Number(carPrice.replaceAll(",", "")) +- priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")) + +--- + +### d) Variable declarations +- let carPrice = "10,000" +- let priceAfterOneYear = "8,543" +- const priceDifference = carPrice - priceAfterOneYear +- const percentageChange = (priceDifference / carPrice) * 100 + +--- + +### e) What does Number(carPrice.replaceAll(",", "")) do? +First, replaceAll(",", "") removes commas from the string "10,000", producing "10000". +Then, Number(...) converts the string "10000" into the numeric value 10000. +This ensures arithmetic operations can be performed correctly. + + + +## 2-time-format.js + +### a) How many variable declarations? +There are 6 variable declarations: +- movieLength +- remainingSeconds +- totalMinutes +- remainingMinutes +- totalHours +- result + +--- + +### b) How many function calls? +There is 1 function call: +- console.log(result) + +--- + +### c) What does movieLength % 60 represent? +The % operator returns the remainder after division. +movieLength % 60 gives the number of seconds left after converting full minutes. +In this example, it returns 24 seconds. + +--- + +### d) Interpret line 4 (totalMinutes) +const totalMinutes = (movieLength - remainingSeconds) / 60; + +This subtracts the leftover seconds from the total seconds and divides by 60. +The result is the total number of full minutes in the movie. + +--- + +### e) What does result represent? Better name? +The variable result represents the formatted movie duration in hours:minutes:seconds. +A better name could be: +- formattedTime +- movieDuration +- durationString + +--- + +### f) Will this work for all values of movieLength? +Yes, it works for any positive number of seconds. +However, if movieLength is negative, a decimal, or not a number, the result would be incorrect. +Also, minutes and seconds are not padded with leading zeros, so values like 2:3:5 may appear instead of 02:03:05. + + + + +## 3-to-pounds.js + +1) `const penceString = "399p";` +- Declares a string containing a price in pence, with a trailing `p` character. + +2) `const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);` +- Takes a substring from index 0 up to (but not including) the last character. +- Purpose: remove the trailing `"p"`. +- For `"399p"`, this becomes `"399"`. + +3) `const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");` +- Pads the string on the left until it is at least length 3, using `"0"`. +- Purpose: make sure we always have enough digits to represent pounds + pence. +- Example: `"5"` becomes `"005"`, `"50"` becomes `"050"`, `"399"` stays `"399"`. + +4) `const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);` +- Takes everything except the last 2 digits. +- Purpose: the part before the last 2 digits is the pounds. +- For `"399"`, length is 3, so substring(0, 1) → `"3"`. + +5) `const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");` +- First, `substring(length - 2)` takes the last 2 digits. +- Then `padEnd(2, "0")` ensures it’s at least 2 characters (adds zeros on the right if needed). +- Purpose: get exactly two pence digits. +- For `"399"`, last two digits are `"99"` → stays `"99"`. + +6) `console.log(\`£${pounds}.${pence}\`);` +- Uses a template literal to format the final currency string as pounds and pence. +- For pounds `"3"` and pence `"99"` → outputs `£3.99`. + +### Unfamiliar syntax (docs) +- substring: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring +- padStart: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart +- padEnd: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd +- template literals: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals \ No newline at end of file diff --git a/Sprint-1/4-stretch-explore/answers.md b/Sprint-1/4-stretch-explore/answers.md new file mode 100644 index 000000000..ca6d6cfdd --- /dev/null +++ b/Sprint-1/4-stretch-explore/answers.md @@ -0,0 +1,72 @@ +## chrome.md + +### What the task asked me to do +Explore the Chrome DevTools console and investigate what the console object stores and how dot notation works. + +--- + +### What I did (steps) +1. Opened Chrome DevTools and navigated to the Console tab. +2. Entered `console.log` and observed that it returned a function. +3. Entered `console` and observed that it returned an object with multiple methods such as log, warn, error, and assert. +4. Entered `typeof console` and confirmed that console is an object. + +--- + +### What I learned +- `console` stores an object containing many methods used for debugging. +- `console.log`, `console.assert`, etc. are functions stored inside the console object. +- The `.` operator is called dot notation and is used to access properties or methods of an object. + +--- + +### Useful links +- https://developer.mozilla.org/en-US/docs/Web/API/Console +- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_objects + + + +## objects.md + +### What output did I get? + +- `console.log` → returned a function definition (ƒ log() { [native code] }) +- `console` → returned an object with many methods such as log, warn, error, assert +- `typeof console` → returned "object" + +--- + +### What does `console` store? +The console variable stores an object that contains multiple debugging methods. These methods allow developers to print messages, warnings, errors, and other diagnostic information in the browser console. + +--- + +### What does `console.log` or `console.assert` mean? +These expressions use dot notation to access methods inside the console object. +For example: +- `console.log` accesses the log function +- `console.assert` accesses the assert function + +--- + +### What does the `.` mean? +The dot operator is called dot notation. +It is used to access properties or methods of an object. + +So: +- `console` is the object +- `log` is a method inside that object +- `console.log` means "access the log method from the console object" + +--- + +### What I learned +- Objects store related data and functions together +- The browser console itself is an object +- Dot notation is how we interact with object properties and methods + +--- + +### Useful links +- https://developer.mozilla.org/en-US/docs/Web/API/Console +- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_objects \ No newline at end of file diff --git a/Sprint-1/notes/learning-log.md b/Sprint-1/notes/learning-log.md new file mode 100644 index 000000000..61caf57c2 --- /dev/null +++ b/Sprint-1/notes/learning-log.md @@ -0,0 +1,16 @@ +# Sprint 1 — Learning Log + +## What I learned +- Started Sprint 1 exercises and set up coursework branch +- Learned how to extract directory and extension from a file path using lastIndexOf and slice. +- How Math.random(), Math.floor(), and arithmetic combine to generate a random integer in a range + + +## Errors I hit and what they mean## What I learned + + +## Docs I used (links) +- + +## Questions for class +- \ No newline at end of file