diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6e..3ce680a323 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,5 @@ 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 is performing assignment. It uses the assignment operator = to update the value of count. It evaluates the expression count + 1, then stores the new value back into count. diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f6175..0238a71c60 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,7 @@ 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 = firstName[0] + middleName[0] + lastName[0]; // 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 ab90ebb28e..cbd9398a76 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/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); + +const lastDotIndex = base.lastIndexOf("."); +const ext = base.slice(lastDotIndex); // 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 292f83aabb..05387f4682 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -4,6 +4,16 @@ 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? +//num represents a random number between minimum(1) and maximum(100), inclusive. + // Try breaking down the expression and using documentation to explain what it means +//Math.random() gives a random decimal number between 0 (inclusive) and 1 (exclusive).Example: 0.005, 0.15, 0.999 etc. +//Math.floor() removes the decimal part and rounds the number down to the nearest whole number. So the value becomes a whole number from 0 and 99. +//maximum - minimum + 1 gives the range of numbers we want to generate, which is 100 - 1 + 1 = 100. This means we want to generate a random number between 1 and 100, inclusive. +//Math.random() * (maximum - minimum + 1) gives a random decimal number between 0 (inclusive) and 100 (exclusive).Example: 0.15 * 100 = 15, 0.88 * 100 = 88, 0.999 * 100 = 99.9 etc. +//Math.floor(Math.random() * (maximum - minimum + 1)) gives a random whole number between 0 (inclusive) and 99 (exclusive). Adding the minimum value shifts this range to between 1 (inclusive) and 100 (inclusive). +//+ minimum shifts the range of random numbers generated by Math.floor(Math.random() * (maximum - minimum + 1)) from 0-99 to 1-100, inclusive. This makes the final result a whole number between 1 and 100, inclusive. + // 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); \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f7..295e424a79 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/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? + +//We can comment it out to stop the computer from executing it by using either // for single-line comment or /* */ for multi-line comment. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea76..72d08dcf5a 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,7 @@ const age = 33; age = age + 1; + +/*This threw the error TypeError: Assignment to constant variable because the line + age = age + 1 attempts to change the value of age after a constant variable is created. + Constant variables cannot be reassigned after initial value is set.*/ \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831d..d149f7fd2e 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,12 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +/* ReferenceError: Cannot access 'cityOfBirth' before initialisation occured because cityOfBirth is used before it is declared. +JavaScript executes code from top to bottom, and the console.log tries to access cityOfBirth before the const cityOfBirth = "Bolton"; line has run. +Variables declared with const cannot be accessed before their declaration, so JavaScript throws a ReferenceError. +To fix this error, move the declaration above the console.log +Like this: +const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); +*/ \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884db..7ff3111849 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,19 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +//const last4Digits = cardNumber.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working + // Before running the code, make and explain a prediction about why the code won't work +/* I predict that the code will fail because cardNumber is a number, and numbers do not have a .slice() method. +.slice() only workon strings and arrays. Since JavaScript cannot call .slice() on a number, it will throw a TypeError. */ + // Then run the code and see what error it gives. +// It gives TypeError: cardNumber.slice is not a function. + // Consider: Why does it give this error? Is this what I predicted? If not, what's different? +//JavaScript throws the TypeError because numbers do not have the.slice() method and therefore cannot be sliced and cardNumber is a number. + // Then try updating the expression last4Digits is assigned to, in order to get the correct value +const last4Digits = String(cardNumber).slice(-4); +console.log(last4Digits); diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 5f86c730bc..c743fd5153 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,5 @@ const 12HourClockTime = "8:53pm"; const 24hourClockTime = "20:53"; + +//JavaScript throws a SyntaxError: Invalid or unexpected token because it does not allow variables to start with a number. +// The variable 12HourClockTime starts with a number 12 making the entire identifier invalid therefore JavaScript could not interpret the variable as valid syntax. diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e18..ca97a6956f 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -12,11 +12,40 @@ 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 in the file. Function call is when parentheses is added to its name: name(). +/* The Lines where function call is made: + replaceAll(",","") which appear twice in both line 4 carPrice.replaceAll(",", "") and + line 5 priceAfterOneYear.replaceAll("," ""). + + Number(...) which appears twice aswell: + Also in line 4 Number(carPrice.replaceAll(",", "")) and line 5 Number(priceAfterOneYear.replaceAll("," "")) + + console.log(...) in line 10 + console.log(`The percentage change is ${percentageChange}`) + */ + // 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 is in line 5.It throws the error SyntaxError: missing ) after argument list because it +is expecting a comma between the arguments in replaceAll(",", "") but sees this replaceAll("," ""). +To fix it you just add the missing comma.*/ // c) Identify all the lines that are variable reassignment statements +/* Line 4 and 5 are the variable reassignment statements. +carPrice = Number(carPrice.replaceAll(",", "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +*/ // d) Identify all the lines that are variable declarations +/* Lines 1,2,7 and 8 are variable declarations. +let carPrice = "10,000"; +let priceAfterOneYear = "8,543"; + +const priceDifference = carPrice - priceAfterOneYear; +const percentageChange = (priceDifference / carPrice) * 100; +*/ // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +/* The expression first uses replaceAll(",","") to remove all commas from the string "10,000" turning it into "10000". Then the Number(...) converts that string into the actual number. +The purpose is to convert a price written as string with commas into a real number so mathematical calculations can be performed. +*/ \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d2395587..e36c0cfa60 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,33 @@ 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 declarations. +const movieLength +const remainingSeconds +const totalMinutes +const remainingMinutes +const totalHours +const result */ // b) How many function calls are there? +// one 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 +/* movieLenth % 60 means: Divide movieLength by 60 and return the leftover seconds. +Example: 8784 / 60 = 146 minutes, remainder 24 seconds. Which makes remainingSeconds = 24.*/ // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +/* line 4 is calculating the total number of whole minutes in the movie. It removes the leftover seconds (remainingSeconds=24) from movieLength (8784), + leaving only the seconds that form complete minutes. +Dividing that value by 60 converts those full seconds into whole minutes. So tottalMinutes = 146. // 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 +// result represents formatted time string so the better name will be formattedTime. + +// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer. +/* It works correctly with positive values, but breaks or becomes weird when: +negative number is used because time cannot be negative. +numbers less than 60 is used results in zero hour and minutes. +non-integer values are used, remainder logic becomes messy. +extremely large numbers are used, it works but formatting becomes unrealistic. */ diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69a..09569ab0a6 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,23 @@ 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 the trailing "p" from the original String, leaving only the numeric part. It takes a substring starting at index 0 and ending just before the last Character, so "399p" becomes "399". + +3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): Ensures the pence value is at least three characters long by padding with leading zero if necessary. Eg, "3" would become "003", "99" would become "099" while "399" remains "399". + +4. const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 +): Extracts the pounds portion from the padded string by taking all characters except the last two, making "399" be "3" representing the pounds component. + +5.const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"): Extracts the last two characters as the pence portion and ensures it is exactly two digits by padding with trailing zeros if needed. "399" becomes "99", representing the pence component. + + 6. console.log(`£${pounds}.${pence}`): Outputs the final formatted price in pounds and pence. Eg, £3.99. + +*/ diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feafe..35d9847853 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -11,8 +11,12 @@ In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +Calling alert("Hello World!") displays a pop-up message box containing the text "Hello World!". The user will now have to click OK to dismiss 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? +Calling prompt("What is your name?") displays a pop-up box with a text input feild. The user can type a response, and the gunction returns whatever text the user entered. If the user clicks Cancel, the return value is null. + What is the return value of `prompt`? +The return value is the text the user typed, or null if the cancelled. diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56a..0302fa10e5 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,21 @@ In this activity, we'll explore some additional concepts that you'll encounter i Open the Chrome devtools Console, type in `console.log` and then hit enter What output do you get? +It outputs: f log() {[native code]} Now enter just `console` in the Console, what output do you get back? +It outputs: console {debug: f, error: f, info: f, log: f, warn: f, ...} -Try also entering `typeof console` +Try also entering `typeof console`: This outputs: 'object' Answer the following questions: What does `console` store? +console stores a collection of functions that allows outputting of information, display warnings, show errors, and interact with the browser's debugging tools. Eg: console.log, console.debug, console.assert,etc + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +The . is the dot operator, and it is used to access a property or method inside an object. +So: +console.log means "access the log function inside the console object". + +console.assert means "access the assert function inside the console object". \ No newline at end of file