diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..aa773717f 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/1-key-exercises/1-count.js @@ -3,4 +3,5 @@ let count = 0; 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 add 1 to value of count and store result back in count, +// in this line = is not mean (is equal to ), It is a assignment diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..9c4e8312e 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -1,10 +1,8 @@ const firstName = "Creola"; const middleName = "Katherine"; 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 = ``; - -// https://www.google.com/search?q=get+first+character+of+string+mdn +const initials = + `${firstName.charAt(0)}` + + `${middleName.charAt(0)}` + + `${lastName.charAt(0)}`; +console.log(`${initials}`); diff --git a/Sprint-2/1-key-exercises/3-paths.js b/Sprint-2/1-key-exercises/3-paths.js index ab90ebb28..669b88cff 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -13,11 +13,7 @@ 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}`); - -// 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 = ; - -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +const dir = filePath.slice(0, lastSlashIndex); +const ext = filePath.slice(filePath.lastIndexOf(".")); +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The ext part of this URL is ${ext}`); diff --git a/Sprint-2/1-key-exercises/4-random.js b/Sprint-2/1-key-exercises/4-random.js index 292f83aab..fb4f79bbc 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -1,9 +1,12 @@ 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 +//num represents a random whole number between 1 and 100, +//first we calculate value inside parentheses(maximum - minimum +1) = 100 -1 + 1 =100 +// math.random return number between 0 and 1 +// math.random() * 100 give us a decimal number +// math.floor round down the number +// add 1 to the result +//for example math.floor((0.783 *100))=78 + 1 = 79 diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..ccf28a0fe 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,8 @@ -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? + +/*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 "//" in the beginning of the line so computer ignore them +// or put them between /* and */ diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..26ce53c60 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -1,4 +1,7 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; +console.log(`${age}`); +//We should use let if we want to change the value. +//Before I changed const to let node show this error message: (Assignment to constant variable.) diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..c728ae6fe 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -1,5 +1,5 @@ // 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}`); +// This error (Cannot access 'cityOfBirth' before initialization) is because we need to declare the variable before using it. diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..f4482226c 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -1,9 +1,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 -// Before running the code, make and explain a prediction about why the code won't work -// 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 +const last4Digits = String(cardNumber).slice(-4); +console.log(`${last4Digits}`); +// we get error (cardNumber.slice is not a function) because slice is a String method diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..56d5f93bc 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,3 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +const HourClockTime12 = "8:53pm"; +const hourClockTime24 = "20:53"; +//JavaScript variable names cannot start with a number. diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..e1f7ae341 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; @@ -12,11 +12,23 @@ 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 +/* Number() - line 4 and Line 5 + replaceAll() - Line 4 and Line 5 + console.log() - 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? +/* line 5 , There is a comma missing between "," and ""*/ // c) Identify all the lines that are variable reassignment statements +/* Line 4 :carPrice = Number(carPrice.replaceAll(",", "")); + Line5 : priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); + carPrice and priceAfterOneYear were already declared on Line 1 and 2 using let */ // d) Identify all the lines that are variable declarations +/* Line 1 : let carPrice = "10,000"; + Line 2 : let priceAfterOneYear = "8,543"; + Line 7 : const priceDifference = carPrice - priceAfterOneYear; + Line 8 : const percentageChange = (priceDifference / carPrice) * 100;*/ // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +/*replaceAll remove all the commas from the string and Number convert string to the 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..8ad9a113a 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 = -98; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,14 +12,25 @@ 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 variable declaration in this program. + 1- movieLength , 2- remainingSeconds , 3- totalMinutes + 4- remainingMinutes , 5- totalHours , 6- result */ // b) How many function calls are there? +/* one function + Line 10 : console.log(); */ // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// Remainder(%) : Return the remainder left over when one operand is divided by a second operand +// console.log(13 % 5 ); 3 // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// This expression converts movie length from seconds into minutes // e) What do you think the variable result represents? Can you think of a better name for this variable? +// Variable result represent the movie length formatted as hours, minutes, seconds , +// another name could be movieTime // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +//So the code works properly for whole numbers, but not for negative numbers and decimal numbers because the result can contain decimal values. diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..9c814cf4f 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,19 @@ 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); +// create a new variable and put the string without p + +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +// padStart() adds characters to the beginning of a string to make it 3 characters long. +// because we need to convert it to pound and pence + +// 4. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2); +// We remove the last 2 characters and store the remaining part of the string as pounds. + +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); +// Gets the last two characters as pence.” + +// 6. console.log(`£${pounds}.${pence}`); +//. Finally combine pound and pence diff --git a/Sprint-2/4-stretch-explore/chrome.md b/Sprint-2/4-stretch-explore/chrome.md index 962580b82..fa95e5b37 100644 --- a/Sprint-2/4-stretch-explore/chrome.md +++ b/Sprint-2/4-stretch-explore/chrome.md @@ -8,8 +8,11 @@ Let's try an example. In the Chrome console, invoke the function `alert` with one argument, the string `"Hello world!"`; What effect does calling the `alert` function have? +alert(): shows the message to the users. in this case show "Hello world!" in the box and we can close it or click on the ok. 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? +prompt(): returns whatever the user enters. It is provide a textbox that we can type on it. What is the return value of `prompt`? +The Return value of prompt is the text enter by user. in this example return name that we write in the box. diff --git a/Sprint-2/4-stretch-explore/objects.md b/Sprint-2/4-stretch-explore/objects.md index 0216dee56..91c0660c9 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -5,12 +5,18 @@ 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? +ƒ log() { [native code] } Now enter just `console` in the Console, what output do you get back? +console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} Try also entering `typeof console` +'object' Answer the following questions: What does `console` store? -What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +console stores object. It stores a collection of methods that allow you to interact with the browser's developer console. +what does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +access the log property/method of the console object. +access the assert method of the console object.