diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..72398bc71 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -1,6 +1,11 @@ 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 + +//Answer +// The third line is a statement that reassigns the value of count to count + 1. +// The assignment operator (=) updates the variable count with a new value. diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..1f5c553b3 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,8 @@ 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.charAt(0) + middleName.charAt(0) + lastName.charAt(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..82ee01445 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -11,13 +11,18 @@ const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; const lastSlashIndex = filePath.lastIndexOf("/"); +console.log(lastSlashIndex); const base = filePath.slice(lastSlashIndex + 1); 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) ; +console.log(dir); +const lastDotIndex = base.lastIndexOf("."); +console.log(lastDotIndex); +const ext = base.slice(lastDotIndex) ; +console.log(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..ea35ba324 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -2,8 +2,32 @@ const minimum = 1; const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; - +console.log(num); // 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 + +//Answers +// num is a constant variable that stores a random integer between the minimum and maximum values (inclusive), generated using Math.random() and Math.floor() + + + +// Math.random() +// This expression gives you a random decimal number between 0 and 1. +// It could be something like 0.25 or 0.879, but it will never reach 1. + +// (maximum - minimum + 1) +// This expression calculates how many numbers are in the range you want. +// For example, if your range is 1 to 100, the result is 100 because there are 100 possible values. + +// Math.random() * (maximum - minimum + 1) +// This expression multiplies the random decimal by your range. + + +// Math.floor(...) +// This expression rounds the number down to the nearest integer. +// For example, 37.6 becomes 37, and 99.9 becomes 99. + +// + minimum +// Variable minimum is added to the expression "Math.floor(Math.random() * (maximum - minimum + 1))" and the the value is stored inside the variable 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 cf6c5039f..fe5e0f358 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,11 @@ -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? + +// Interpretation: +// The error "SyntaxError: Unexpected identifier 'is'" means that JavaScript found the word "is", but it didn’t make sense in that position. +// It wasn’t valid code that JavaScript could understand. + +// Why the error occurred: +// The error occurred because the sentence was written as plain text instead of a comment. +// To fix it, the sentence should be written as a comment (by adding // at the beginning) so that JavaScript ignores it. + diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..eb835305b 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; + + +// The error message is "Assignment to constant variable", which means that a variable declared with the 'const' keyword +// cannot be reassigned to a new value after its initial declaration. diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..7bf2655c2 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,5 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +// The error message 'Cannot access 'cityOfBirth' before initialization' occured because the 'const' variable was used before it was declared. \ 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..41d7c4210 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 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 +8,15 @@ 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 + + +// Answer +// The code will not work because the slice method in javascript only works on a string or an array. +// The data type of the value stored in the variable cardNumber is a number and for this reason, the code will return an error. +// After running the code, it gave an error " TypeError: cardNumber.slice is not a function" +// My prediction and the error align. +// To update the code, I will convert the variable cardNumber to a string first then use slice() method after + +const cardNumber = 4533787178994213; +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..d1aff20b0 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,12 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +// const 12HourClockTime = "20:53"; +// const 24hourClockTime = "08:53"; + +// console.log(12HourClockTime , 24hourClockTime); + +// Answer +// After running the code, it gave an error message "SyntaxError: Invalid or unexpected token". +// This error means that Javascript was unable to read the code because a variable name cannot start with a number +// I can fix this code by removing the numbers from the start of the variable name + +const time24HourFormat = "20:53"; +const time12HourFormat = "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..2a8e70b64 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -1,8 +1,8 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; -carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +carPrice = Number(carPrice.replaceAll("," ,"")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ,"")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -12,11 +12,17 @@ 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 +// Answer: There are five function calls in this file. function was called on line 4 (twice), line 5 (twice) and line 10. // 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? +// Answer: +// After running the code, the error occurred because a comma was missing between the quotation marks on line 5. I fixed it by adding the missing comma. // c) Identify all the lines that are variable reassignment statements +// Answer: Variable reassignment statements occurred on lines 4 and 5 // d) Identify all the lines that are variable declarations +// Answer: Variable declarations occurred on lines 1, 2, 7, and 8. // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// Answer: The .replaceAll() method runs first to find all the comma in the variable carPrice and replace them with nothing then the Number() functions runs to convert the value to a number data type \ 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 47d239558..5fe2e16a5 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 = 8974; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,14 +12,21 @@ 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? +//Answer: There are six variable declarations in this program // b) How many function calls are there? +// Answer: There is only one function call in the program // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// Answer: The expression movieLength % 60 represents the remainder when movieLength is divided by 60 // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// Answer: The expression assigned to totalMinutes subtracts the leftover seconds from the movieLength and divide it by 60 to get the totalMinutes. +// This gives the total whole minutes of the movie without the leftover seconds. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// Answer: The variable result represents the time in hours, minutes and seconds. A better name for the variable could be totalTime // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// Answer: I tried different positive values of movieLength and the code worked for all but result variable returned a negative value when I tried a negative number as the variable movieLength diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..5e6d57138 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,168 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +// Answer +const penceString = "399p" +// declares a variable penceString and assigns a value "399p" to it. + +const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 +); +// Step-by-step breakdown +// 1. const + +// What it means: +// const is short for constant. +// It’s a keyword in JavaScript used to declare a variable — a named place in memory where we store a value. + +// What makes it special: +// Once you assign a value to a const variable, you cannot reassign it later. + +// 2. penceStringWithoutTrailingP + +// What it is: +// This is the name of the variable we’re creating. + + +// 3. = + +// What it means: +// The equals sign here means assignment, not comparison. +// It tells JavaScript: “Store the value on the right-hand side into the variable on the left-hand side.” + +// So, penceStringWithoutTrailingP will hold whatever value comes from the code on the right side. + +// 4. penceString + +// What it is: +// This is a variable that was created earlier in the code: + +// 4. .substring(...) + +// What it is: +// .substring() is a built-in method (a function that belongs to strings) in JavaScript. + +// What it does: +// It extracts a section (or slice) of a string, starting and ending at specific positions (index numbers). + +// The first number is where to start (inclusive). + +// The second number is where to stop (exclusive, it doesn’t include that index). + +// ( 0, penceString.length - 1 ) + +// These are the arguments (inputs) given to the substring() function. + +// Let’s understand each one: + +// 0 + +// This means: “Start from the very first character of the string.” +// In JavaScript, string positions start counting at 0, not 1. + +// penceString.length + +// .length is a property (not a function). +// It gives the number of characters in the string. + +// For "399p", penceString.length is 4. + +// penceString.length - 1 + +// This means “one less than the total length.” + +// 4 - 1 = 3. + +// So we’re telling JavaScript: “Stop right before the last character.” + +// Putting it all together: +// penceString.substring(0, penceString.length - 1) means “Take the text in penceString, start at position 0, and stop right before the last character.” +// So for "399p", it returns "399" — it removes the final p. +// The variable penceStringWithoutTrailingP will now contain: "399" + +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +// Step-by-step breakdown +// 1. const + +// Keyword meaning: +// const is short for constant. +// It tells JavaScript that we’re creating a new variable whose value cannot be changed later. + +// Why use it here? +// We only need to store the processed result; we’re not planning to change it again. + +// 2. paddedPenceNumberString + +// This is the variable’s name. + +// 3. = + +// equals sign here is the assignment operator. + +// It means “store the result on the right-hand side into the variable on the left-hand side.” + +// 4. penceStringWithoutTrailingP + +// This is another variable that was defined earlier in the program. + +// It holds a string like "399", "50", or "5", after removing the “p” at the end of the original price (like "399p" → "399"). + +// 5. .padStart(3, "0") + +// This is a string method — something you can call on a string to make it do something. + +// Let’s break it down further: + +// a) The dot (.) + +// The dot connects the variable (penceStringWithoutTrailingP) with a method (a built-in action it can perform). + +// In English: “Hey JavaScript, take this string and do the padStart operation on it.” + +// b) padStart(...) + +// This is the method name. + +// It “pads” the start (the beginning) of a string with extra characters until it reaches a certain length. + +// c) The parentheses (3, "0") + +// These are the arguments — pieces of information you pass to the method. + +// The first argument (3) means: +// “Make the string at least 3 characters long.” + +// The second argument ("0") means: +// “If it’s shorter than that, add zeros ("0") at the start.” + +// Putting it all together +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +// When JavaScript checks the length, it sees that it’s already 3 characters long, so it doesn’t add any zeros. +// the result "399" is stored in the variable "paddedPenceNumberString" + +const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 +); +// paddedPenceNumberString.length means the length of the character "399" is 3. +// paddedPenceNumberString.length - 2 means 3 - 2 = 1 +// paddedPenceNumberString.substring(0 , 1) means take everything from position 0 up till position 1 exclusive. This will return 3 +// const pounds means store the value 3 inside the variable pounds. + +const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); +// paddedPenceNumberString.length means the length of the character "399" is 3. +// paddedPenceNumberString.length - 2 means 3 - 2 = 1 +// .substring(paddedPenceNumberString.length - 2) means .substring(1) which means take everything from positon 1 to the end. This returns 99 +// .padEnd(2, "0") means make sure the string has at least two characters. If not, add zeros ("0") at the end. for this exercise, 99..padEnd(2, "0") will return 99 +// so, const pence means variable pence will store the value 99 + +console.log(`£${pounds}.${pence}`); +// console meaans terminal and log means print or show this message. +// `£${pounds}.${pence}` is a literal template which means £3.99 +// so, print £3.99 to the terminal + +