diff --git a/.gitignore b/.gitignore index bde36e530..e7509451d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,39 @@ -node_modules +# Generated .gitignore file for: Node, JavaScript +# Created with FOSSA's DevOps Tools + +# Node +node_modules/ +npm-debug.log +yarn-error.log +.env .DS_Store -.vscode -**/.DS_Store \ No newline at end of file +dist/ +.cache/ +coverage/ + +# JavaScript +# Add JavaScript-specific ignores here +package-lock.json + +# Notebooks and documentation +docs/ + +# Common +.DS_Store +Thumbs.db +*.log +*.bak +*.tmp +*.swp +*~ + +# IDE and editor generated files +.idea/ +*.sublime-* +.vscode/ +.devcontainer/ + +# Local environment files +.env +.env.local +.env.*.local diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..d076c89ce 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,6 @@ 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 reassigns the count variable by evaluating the expression on the right hand side of the assignment. +// The right hand side (count + 1) is evaluated first, then the = operator assigns that value to count. diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..4f397871f 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,5 @@ 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 = ``; - -// https://www.google.com/search?q=get+first+character+of+string+mdn - +let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`; +console.log(initials); diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..2692f8315 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,7 @@ 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 + 1); +const ext = base.slice(base.lastIndexOf(".")); -// 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-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..573abfcc5 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -3,7 +3,37 @@ 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 +// Breakdown of the 'num' declaration +// The expression of `num` can be broken down into several discrete steps: + +// 1. `Math.random()`, this generates a random floating point number +// This number is between 0 (inclusive) and 1 (exclusive) +console.log("Math.random():", Math.random()); + +// 2. `(maximum - minimum + 1)` this part simple resolve to 100 +console.log("maximum - minimum + 1:", maximum - minimum + 1); + +// 3. By multiplying steps 1 and 2, the floating point number is scaled from 0-1 to 0-100 +// This adjusted scale is defined by the `minimum` and `maximum` variables declared earlier +console.log( + "Math.random() * (maximum - minimum + 1):", + Math.random() * (maximum - minimum + 1) +); + +// 4. `Math.floor()` then rounds down to the nearest whole number +// Turning the floating point number into a whole number and adjusting the range from 0-99 +console.log( + "Math.floor(Math.random() * (maximum - minimum + 1)):", + Math.floor(Math.random() * (maximum - minimum + 1)) +); + +// 5. Lastly, `+ minimum` shifts the result up by the value of 1 +// In effect, this adjusts the range from 0-99 to 1-100 +console.log( + "Math.floor(Math.random() * (maximum - minimum + 1)) + minimum:", + Math.floor(Math.random() * (maximum - minimum + 1)) + minimum +); + +// The final result is a random whole number between minimum (1) and maximum (100) +// Where 100 is an inclusive valid value in the range +console.log("num:", num); diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..11a7126ce 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,6 @@ -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? + +// Answer: Use comments (//) to prevent execution +// SyntaxError: Unexpected identifier - plain text is not valid JavaScript +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#comments diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..15582e771 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,11 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +// const age = 33; +// age = age + 1; + +// TypeError: Assignment to constant variable - `const` cannot be reassigned +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const + +// Solution: +let age = 33; age = age + 1; diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..df246c547 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,12 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... -// what's the error ? +// What's the error? -console.log(`I was born in ${cityOfBirth}`); +// console.log(`I was born in ${cityOfBirth}`); +// const cityOfBirth = "Bolton"; + +// ReferenceError: Cannot access 'cityOfBirth' before initialization - temporal dead zone +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const#temporal_dead_zone_tdz + +// Solution: const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..fb205fc11 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,5 @@ -const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +// const cardNumber = 4533787178994213; +// const last4Digits = cardNumber.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +7,10 @@ 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 + +// TypeError: cardNumber.slice is not a function - .slice() exists on strings, not numbers +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice + +// Solution: +const cardNumber = 4533787178994213; +const last4Digits = String(cardNumber).slice(-4); diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..d281d2133 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,9 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +// const 12HourClockTime = "20:53"; +// const 24hourClockTime = "08:53"; + +// SyntaxError: Invalid or unexpected token - variable names cannot start with a digit +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#variables + +// Solution: +const twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "08:53"; diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..f455296f4 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -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 +// 6 function calls: Line 4 (replaceAll, Number), Line 5 (replaceAll, 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? +// SyntaxError on line 5: missing comma between arguments "," and "" +// Fix: priceAfterOneYear.replaceAll(",", "") // c) Identify all the lines that are variable reassignment statements +// Line 4: carPrice = Number(carPrice.replaceAll(",", "")); +// Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); // d) Identify all the lines that are variable declarations +// Line 1: let carPrice, Line 2: let priceAfterOneYear, Line 7: const priceDifference, Line 8: const percentageChange // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// Converts string with commas ("10,000") to number (10000) for mathematical calculations +// replaceAll(",", "") removes commas, Number() converts string to number +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..7bf286a8b 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,26 @@ 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? +// 6 declarations: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, result // b) How many function calls are there? +// 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 +// % is the remainder operator - calculates remainder when dividing movieLength by 60 +// Example: 8784 % 60 = 24 (seconds that don't fit into complete minutes) +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// (movieLength - remainingSeconds) / 60 calculates complete minutes +// Subtracts leftover seconds (24) from total (8784), then divides by 60 to convert to minutes // e) What do you think the variable result represents? Can you think of a better name for this variable? +// Represents movie length formatted as "hours:minutes:seconds" +// Better names: formattedTime, timeString, movieDuration, formattedDuration // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// Works mathematically for positive integers but has formatting issues: +// - Single digits aren't zero-padded (shows "1:6:5" instead of "01:06:05") +// - Negative values produce incorrect results +// - Would need padStart() for proper time formatting diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..959f45104 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,26 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); +// Removes trailing "p" from "399p" to get "399" +// substring(0, 3) extracts characters from index 0 to 2 +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring + +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +// Ensures string has at least 3 characters by adding leading zeros +// "399" stays "399", but "9" would become "009" (for 9p = £0.09) +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart + +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); +// Extracts all except last 2 characters for pounds +// "399" → substring(0, 1) → "3" (£3) + +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); +// Extracts last 2 characters for pence and pads to 2 digits +// "399" → substring(1) → "99", then padEnd(2, "0") → "99" +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd + +// 6. console.log(`£${pounds}.${pence}`); +// Formats and displays result using template literal: "£3.99" +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals