diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..19c3f8bf4 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/1-key-exercises/1-count.js @@ -2,5 +2,7 @@ let count = 0; count = count + 1; +// = is an assign operator so if count = 0 at line 1 then at line 3 count = 0 + 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 diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..5055ac681 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -5,6 +5,10 @@ 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..9ecae1709 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -17,7 +17,10 @@ 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 = filePath.slice(-4); -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +console.log(dir); +console.log(ext); + +// 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..8da536bc2 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -2,6 +2,10 @@ const minimum = 1; const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +//Math.floor is rounding down number from float number to integer +//Math.random create a random decimal number(float) from 0 up to 1 we then multiply it by (100) and add 1(minimum) +// variable num represents a random integer ranging from 1 to 100 +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 diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..98b123dff 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,5 @@ 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 have to use the // to comment a single line +/* or use /* to comment multiple lines */ \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..34bbfe5fd 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -2,3 +2,5 @@ const age = 33; age = age + 1; +/* const means that the variable cannot be reassigned we should use let keyword instead. +TypeError displayed due to that. */ diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..f3d08f744 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -3,3 +3,8 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +//The error we see it's a ReferenceError which tells us that we cannot access variable before initialization. + +/*JavaScript reads the code from top to bottom, order is wrong. +Task cannot be executed as variable cannot be access before being created. */ diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..f50e98f42 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/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,7 @@ 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 + +/*The code would not work because cardNumber variable stores number data type and .slice() method cannot be used on a number, +if we run the code without changing data type to string we will get TypeError displayed +I used .toString() method to convert cardNumber variable to a string and then used .slice(-4) to get the last 4 digits. */ diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..acc443b1f 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,4 @@ const 12HourClockTime = "8:53pm"; const 24hourClockTime = "20:53"; + +//A variable cannot start with a number, SyntaxError is displayed. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..52d456001 100644 --- a/Sprint-2/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-2/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; @@ -20,3 +20,10 @@ 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? + +//a)There are 5 function calls on line 4, 5 and 10. +//b) error is coming from line 5, there is missing , between arguments in replaceAll method +//c) line 4 and 5 +//d) line 1,2 and 7,8 +/*e) replaceAll method inside of () replace comma with nothing, removing it from the string, +then Number converts string to number data type */ diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..c15cb1162 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = -8784.08; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -23,3 +23,10 @@ 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 + +//a) There are 6 variable declarations. +//b)There is 1 function call on line 10 +//c) movieLength use remainder operator % . movieLength % 60 returns the remaining seconds after dividing the movie length into full minutes. +//d) expression assigned to totalMinutes calculate movie time in whole minutes. +//e) This variable represent total movie time in hh/mm/ss format, we could name it totalTime or totalMovieTime. +//f) Yes, the code works correctly when movieLength is non-negative whole number that represents seconds. diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..adc11eee4 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 @@ -24,4 +24,18 @@ 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" +// 3. penceStringWithoutTrailingP is created using substring() on penceString. The purpose is to remove the trailing "p".. +// 4. value for substring method to start from which is beginning of the string. +// 5. penceString variable uses argument of .length to -1 (p) from the value, 399p becomes 399. +// 6. substring method closure +// 8. paddedPenceNumberString uses padStart() to make the string at least 3 characters long, adding "0" at the beginning when needed. +// 9. pounds variable assigns paddedPenceNumberString that uses substring method which returns part of the string from the start of index. +//10. value for substring method which is beginning of the string. +//11. paddedPenceNumberString variable is using length argument and deduct 2 out of length of the string 399 become 3 as pounds. +//12. Closes the substring method. +//14. pence variable created from paddedPenceNumberString +//15. substring method called which value of is in the parenthesis (variable length -2 characters) +//16. method padEnd is called that contains at least 2 character and adds 0 at the end if needed. +//18. we log £3.99 in the console. diff --git a/Sprint-2/4-stretch-explore/chrome.md b/Sprint-2/4-stretch-explore/chrome.md index 962580b82..5c8000d49 100644 --- a/Sprint-2/4-stretch-explore/chrome.md +++ b/Sprint-2/4-stretch-explore/chrome.md @@ -9,7 +9,15 @@ In the Chrome console, invoke the function `alert` with one argument, the string What effect does calling the `alert` function have? +### Alert would display a pop up window which can be used to display for example error messages. + 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`? + +### Prompt display text input option that we can store in a variable. + +### prompt returns text that user entered in the input field diff --git a/Sprint-2/4-stretch-explore/objects.md b/Sprint-2/4-stretch-explore/objects.md index 0216dee56..d702ef9d6 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -6,11 +6,22 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter What output do you get? +### ƒ log() { [native code] } + Now enter just `console` in the Console, what output do you get back? +### the output is console object with it's available properties and methods. + Try also entering `typeof console` +### console is an object + Answer the following questions: What does `console` store? + +### Console store functions that can interact with the console object + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? + +### "." is used to access a property or method inside an object eg. console.log access the log function where .assert access the assert function diff --git a/Sprint-2/README.md b/Sprint-2/README.md index a47afc540..d77309976 100644 --- a/Sprint-2/README.md +++ b/Sprint-2/README.md @@ -12,7 +12,7 @@ This README will guide you through the different sections for this week. ## 1 Exercises -In this section, you'll have a short program and task. Some of the syntax may be unfamiliar - in this case, you'll need to look things up in documentation. +In this section, you'll have a short program and task. Some of the syntax may be unfamiliar - in this case, you'll need to look things up in documentation. https://developer.mozilla.org/en-US/docs/Web/JavaScript @@ -28,7 +28,7 @@ You must use documentation to make sense of anything unfamiliar - learning how t You can also use `console.log` to check the value of different variables in the code. -https://developer.mozilla.org/en-US/docs/Web/JavaScript +https://developer.mozilla.org/en-US/docs/Web/JavaScript ## 4 Explore - Stretch 💪