diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..3dc06a012 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,8 @@ 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 re-assigning the variable "count" with a new value that increases the previous value by 1 + +*/ diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..d55da2f24 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -6,6 +6,11 @@ let lastName = "Johnson"; // 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 firstNameInitials = firstName.charAt(0); +let middleNameInitials = middleName.charAt(0); +let lastNameInitials = lastName.charAt(0); +initials= `${firstNameInitials}${middleNameInitials}${lastNameInitials}` +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..c322f8e9a 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 = filePath.lastIndexOf("."); +const ext = filePath.slice(lastDotIndex); +console.log(`The dir filePath is ${dir} and the ext filePath is ${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..06d03014e 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,15 @@ 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 + + +/* +num stores a random integer between minimum (1) and maximum (100), inclusive. + +Breakdown: +1. Math.random() generates a decimal number between 0 (inclusive) and 1 (exclusive). +2. Multiplying by (maximum - minimum + 1) scales the range to 0–100. +3. Math.floor() removes the decimal part, producing integers from 0–99. +4. Adding minimum shifts the range to 1–100. +*/ +console.log(num); diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..612eb3934 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,21 @@ -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? + +//The error found when running the code-> SyntaxError: Unexpected identifier 'is'. +// syntax errors means the grammar rules of the language has not been applied. The instruction is written in plain english in a js file. It is not written in js syntax so js cannot parse it. + +// An identifier just means: +//A variable name +//A function name +// Or any word that isn’t a keyword +// So "is" is being treated like a variable name — and the parser wasn’t expecting one at that position. + +// //Solution: +/*In this case, comment out the code by adding two forward slashes to a single line comment or +a single slash and a multiplication sign to both ends of a double line comment like the one used for this comment. +Other solutions could be checking: +Missing quotes +Missing operator +Missing comma +Or misplaced word +*/ \ 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 7a43cbea7..2dd5e4339 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; + +//TypeError: Assignment to constant variable. +//what went wrong +//The program is trying to reassign a constant variable. In javascript a constant once assigned cannot be reassigned another value \ 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 e09b89831..7fb878394 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,7 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +//ReferenceError: Cannot access 'cityOfBirth' before initialization +/* A lexical variable was accessed before it was initialized. This happens within any scope (global, module, function, or block) when variables declared with let or const are accessed before the place where they are declared has been executed +*/ \ 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 ec101884d..83625e797 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,6 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +8,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 +//The Number type value stored in cardNumber does not a slice method +//slice methods only works with arrays, strings or objects that has a callable property named slice + +//Solution: Convert the number value to string +//Options: use a String Wrapper or toString method diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..f0ab94e12 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,2 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "08:53"; \ 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..816e41325 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -2,7 +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; @@ -12,11 +12,22 @@ 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 +// Line 4 and 4 has two function calls Number() and replaceAll() +// - line 10 has a function 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? +// Line 5- +// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +// ^^^ +// SyntaxError: missing ) after argument list // c) Identify all the lines that are variable reassignment statements +// carPrice an priceAfterOneYear - lines 4 and 5 // d) Identify all the lines that are variable declarations +//Lines - 1, 2, 7, 8 // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// replaceAll() removes all occurrence of commas from the string e.g "10,000" becomes "10000" and the Number() wrapper converts +// the string to number type; "10000" becomes 10000 + diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..8f52fb2b2 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = 9; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -6,20 +6,32 @@ const totalMinutes = (movieLength - remainingSeconds) / 60; const remainingMinutes = totalMinutes % 60; const totalHours = (totalMinutes - remainingMinutes) / 60; -const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; +const result = `${totalHours}:${String(remainingMinutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; 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 // b) How many function calls are there? +// 1 // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +//Remainder operator. +//The remainder (%) operator returns the remainder left over when one operand is divided by a second operand. +// It always takes the sign of the dividend. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// The value stored in totalMinute is calculating the number of minutes that have elapsed in the movie by subtracting the remaining time from the total duration and converting seconds to minutes. // e) What do you think the variable result represents? Can you think of a better name for this variable? +//The variable result represents a formatted time string in the format HH:MM:SS. +// A better name would be "movieDurationDisplay", as these names more clearly describe the purpose and content of the variable. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// No. Not for all possible values of movieLength, some EdgeCases are: +//1. when movieLength is of negative value, the maths produces negative time and its illogical for a movie duration +//2. if movieLength is not a number, its produces "NAN" and this breaks mathematically +//3. what if movieLength is less than 10? if movieLength is let say 9 , without proper formatting it will look like this 0:0:9 but with 0 padding it looks better like this 0:00:09 \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..ece49e720 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -6,6 +6,7 @@ const penceStringWithoutTrailingP = penceString.substring( ); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2 @@ -24,4 +25,10 @@ console.log(`£${pounds}.${pence}`); // 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" +/* 1. const penceString = "399p": initializes a string variable with the value "399p" + 2. const penceStringWithoutTrailingP = This stores the substring of penceString by extracting all characters except the final p. It takes the substring from index 0 up to (but not including) the last character p, so "399p" becomes "399". This removes the unit symbol so only the numeric portion remains. + 3. const paddedPenceNumberString= This stores the value of penceStringWithoutTrailingP, padded to a total length of 3 characters by adding leading zeros where necessary. This guarantees there are always enough digits to separate pounds and pence correctly. + 4. const pounds= Stores the substring of paddedPenceNumberString from index 0 to the last 2 index. These leading digits represent the pound portion of the amount. + 5. const pence= Extracts the final two digits of the padded string to represent the pence portion. padEnd(2, "0") ensures that the pence value always contains exactly two digits. + 6. console.log(`£${pounds}.${pence}`); logs the value of pounds and pence into a formatted currency string using template literals +*/ diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..f92906a81 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -9,10 +9,14 @@ Let's try an example. In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; +alert("Hello world!"); -What effect does calling the `alert` function have? +What effect does calling the `alert` function have? +A popup modal appears with the String -> "Hello world!" 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`. +let myName = prompt("What is your name?"); -What effect does calling the `prompt` function have? -What is the return value of `prompt`? + +What effect does calling the `prompt` function have?A modal appears with the "What is your name?" with an input field to enter your name. The input is then stored in the variable myName +What is the return value of `prompt`? My name stored in the variable myName