diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..10c1e2ff2 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/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 is updating the count variable by reassigning new value that is count + 1 or (0 + 1 which is 1) +// The = operator is assigning the count variable we declared in line 1 with new value diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..18ab057fa 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -5,6 +5,8 @@ const 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. -const initials = ``; +const 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-2/1-key-exercises/3-paths.js b/Sprint-2/1-key-exercises/3-paths.js index ab90ebb28..e17b9b632 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -12,12 +12,14 @@ const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; const lastSlashIndex = filePath.lastIndexOf("/"); const base = filePath.slice(lastSlashIndex + 1); -console.log(`The base part of ${filePath} is ${base}`); +//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 ext = base.slice(-4); +//console.log(dir); +//console.log(ext); -// 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-2/1-key-exercises/4-random.js b/Sprint-2/1-key-exercises/4-random.js index 292f83aab..011620ddd 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -3,7 +3,17 @@ 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 + +// num is a variable declared to hold (being assigned) the value of the calculated result of the var minimum, var maximum and the Math.random() method +// This expression can be broken down in to main steps +// 1. the inner parentheses (maximum - minimum + 1) is calculated because that is the inner most expression +// 2. then the Math.random() generates random number between 0 & 1 and multiply with the result of (maximum - minimum + 1) +// 3. then the Math.floor() rounds down the result of (Math.random() * (maximum - minimum + 1)) to the nearest whole number +// 4. finally the minimum is being added to the down rounded whole number and the variable num holds the value +// after running the program the value of num looks like this 51,8,85,8,99,21 random numbers between 1 & 100 diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..c9163b12e 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,9 @@ -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? + +//OR + +/* 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? */ + +// We can put the 2 lines in comment by adding // before each single line or we can enclose them in /* */ if we want to comment multi-line diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..0f1253bca 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -1,4 +1,12 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +//const age = 33; +let age = 33; age = age + 1; +console.log(age); + +/* When we try to run the code it is giving us an error message saying TypeError: Assignment to constant variable, +this error is telling us that we are trying to perform an operation on a value that is not the correct type, +in our case reassigning a cons variable. The cons keyword prevents us from reassigning a variable +We can overcome the error by changing the declaration keyword to "let" +*/ diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..ab8687fe4 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -1,5 +1,11 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); +//const cityOfBirth = "Bolton"; + +/* When we try to run our code it is giving us an error saying ReferenceError: Cannot access 'cityOfBirth' before initialization +and this is because we tried to access the var cityOfBirth before we declared and initialized it*/ + +// We can overcome this error by declaring and initializing the var cityOfBirth before accessing it diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..296286bd6 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -1,5 +1,7 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +//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 +9,8 @@ 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 + +// I am predicting the code wont't work because the slice method does not work for numbers I guess +// when i run the program it is giving an error saying "TypeError: cardNumber.slice is not a function" +// It is giving this error because our var cardNumber is number and numbers don't have slice() method & it is what I predicted +// In order our code to be able to run we need first to change the number type into String so that the slice() method can function diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..76b74be8b 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,13 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +//const 12HourClockTime = "8:53pm"; +//const 24hourClockTime = "20:53"; + +const HourClockTime12 = "8:53pm"; +const hourClockTime24 = "20:53"; + +console.log(HourClockTime12); +console.log(hourClockTime24); + +// when we tried to run the code it threw an error saying "SyntaxError: Invalid or unexpected token" +// this happened because when we were declaring a variable we gave an invalid name to the identifier which started with number +// variable name must start with letters or _ or $ +// we can fix it buy removing the front number of the identifiers and may be put them last diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..f3120dbc3 100644 --- a/Sprint-2/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-2/3-mandatory-interpret/1-percentage-change.js @@ -2,7 +2,8 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -20,3 +21,21 @@ console.log(`The percentage change is ${percentageChange}`); // d) Identify all the lines that are variable declarations // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +// Answer + +// a) In this file we can say we have 5 function calls (2 function calls & 3 method calls to be specific) and they are in line 4, 5 and 10 + +// b) When we run the code the error is generating from line 5 and the reason is that when we pass more than 1 argument +// in a method we need to separate them with comma, but in line 5 there is no comma to separate the arguments +// we can fix it by adding a comma to separate the arguments + +// c) Lines 4 and 5 are reassignment statements + +// d) Lines 1, 2, 7 and 8 are variable declarations + +// e) The expression Number(carPrice.replaceAll(",","")) +// 1. carPrice.replaceAll(",","") this method is removing the "," from the String variable carPrice by replacing the "," with empty String which is "" +// all left then is just the String digits without "," +// 2. Number("String digit without comma") and this function converts the String digit to number digit +// and finally Number(carPrice.replaceAll(",","")) expression gives us number diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..75221ede1 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -23,3 +23,19 @@ 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 + +// Answer + +// a) In the above code there are 6 variable declarations + +// b) There is only 1 function call in the code + +// c) the expression movieLength % 60 is trying to divide the movieLength by 60 then get the remainder + +// d) totalMinutes = (movieLength - remainingSeconds) / 60 in this expression +// 1. (movieLength - remainingSeconds) the values inside the parenthesis is calculated first then it is divided by 60 +// then the value of expression is assigned to the var totalMinutes + +// e) the variable result represents the formatted time in hours:minutes:seconds. I would rename it time + +// f) it will not work with negative numbers because we can not have negative time diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..965bd131f 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -2,13 +2,13 @@ const penceString = "399p"; const penceStringWithoutTrailingP = penceString.substring( 0, - penceString.length - 1 + penceString.length - 1, ); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); const pounds = paddedPenceNumberString.substring( 0, - paddedPenceNumberString.length - 2 + paddedPenceNumberString.length - 2, ); const pence = paddedPenceNumberString @@ -25,3 +25,43 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +// 2. From line 3 to line 6 const penceStringWithoutTrailingP is declared and it will hold the value of +// penceString.substring(0, penceString.length - 1) +// which is "399". We got this value by the method substring(0, penceString.length - 1) called by penceString +// Breaking down the expression penceString.substring(0, penceString.length - 1) +// a) penceString.length - 1 this counts the number of characters in the penceString String and then subtracts 1 from it (4-1=3) +// then it will look like this penceString.substring(0, 3) +// b) penceString.substring(0, penceString.length - 1) then the substring method called by penceString +// is used produce a slice of small string from a big string +// it takes two arguments the first one tells where to start slicing and the second one tells where to stop +// (but the last character is not included) +// Therefore const penceStringWithoutTrailingP will be assigned "399" + +// 3. In line 8 const paddedPenceNumberString is declared and it will hold the value of penceStringWithoutTrailingP.padStart(3, "0") +// which is still "399". In this expression we used the method padStart called by penceStringWithoutTrailingP and this method is basically +// adds characters at the start of our String. It takes two arguments the first one is telling how many characters we want in our String +// and second argument tells the character that needs to be added so that we get out desired number of characters in our String. +// since our String length is 3 the method will not do anything + +// 4. From line 9 to line 12 const pounds is declared and it will hold the value of paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2,) +// which is "3". We got this value by the method substring(0, paddedPenceNumberString.length - 2,) called by paddedPenceNumberString +// Breaking down the expression paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2,) +// a) paddedPenceNumberString.length - 2 this counts the number of characters in the paddedPenceNumberString then subtracts 2 from it (3-2=1) +// then it will look like this paddedPenceNumberString.substring(0, 1) +// b) paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2,) then the substring method called by paddedPenceNumberString is used to +// produce a slice of small string from a big string +// it takes two arguments the first one tells where to start slicing and the second one tells where to stop +// (but the last character is not included) +// Therefore const pounds will assigned "3" + +// 5. From line 14 to line 16 const pence is declared and it will hold the value of +// paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0") which is "99" +// Breaking down the expression +// a) paddedPenceNumberString.length - 2 this will count the number of characters in paddedPenceNumberString and then subtracts 2 from it (3-2=1) +// paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) then this substring method will produce small string from it starting from index 1 which will be "99" +//then finally paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0") the padEnd method will add characters at the end of the String +// and will take two arguments first will tell how many characters we need in our string and the second will tell the character that needs to be added +// since our String length is 2 the method will do nothing + +// 6. Finally line 18 will print out the desired format we have added what to be printed inside the back-ticks if they are variables we will put them in ${} diff --git a/Sprint-2/4-stretch-explore/chrome.md b/Sprint-2/4-stretch-explore/chrome.md index 962580b82..198f9c67a 100644 --- a/Sprint-2/4-stretch-explore/chrome.md +++ b/Sprint-2/4-stretch-explore/chrome.md @@ -1,15 +1,20 @@ -Open a new window in Chrome, right click an empty space on the page, select **Inspect** from the dropdown, then locate the **Console** tab. +Open a new window in Chrome, right click page, select **Inspect** from the dropdown, then locate the **Console** tab. Voila! You now have access to the [Chrome V8 Engine](https://www.cloudflare.com/en-gb/learning/serverless/glossary/what-is-chrome-v8/). Just like the Node REPL, you can input JavaScript code into the Console tab and the V8 engine will execute it. Let's try an example. -In the Chrome console, invoke the function `alert` with one argument, the string `"Hello world!"`; +Click an empty space on tn the Chrome console, invoke the function `alert` with one argument, the string `"Hello world!"`; What effect does calling the `alert` function have? +// The `alert` function displays a message to the user and it stops js execution until the user dismisses the alert + 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? What is the return value of `prompt`? + +// The `prompt` function opens a popup dialog and waits an input from a user. it stops js execution until the user inputs or cancel the popup dialog +// The return value of `prompt` is a string diff --git a/Sprint-2/4-stretch-explore/objects.md b/Sprint-2/4-stretch-explore/objects.md index 0216dee56..4ec83cf1e 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -13,4 +13,9 @@ Try also entering `typeof console` Answer the following questions: What does `console` store? + +// `console` stores methods + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? + +// The `console.log` or `console.assert` means access log or assert method from console object and the `.` is called the dot operator, and it is used to access the property or method of an object or value