diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..f93ffbcfc 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 increases the value of count by 1. +// the "=" operator assigns the result of "count + 1" back to the variable count. +// Can you find out what one-word programming term describes the operation on line 3? +// The one word term is "increment". +// increment means increasing the value of a variable by 1. diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..1e2a39ae4 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,12 @@ 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[0] + middleName[0] + lastName[0]; +console.log(initials) +// fistName[0] gets the first character of the string "Creola" C +// middleName[0] gets the second character of the name "Katherine" K +// lastName[0] gets the third character of the name "Johnson" J + // 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..bebd818db 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 ext = base.slice(base.lastIndexOf(".")); +// dir uses slice(lastSlashIndex) to get everything before the last slash +// ext used base.slice(base.lastIndexOf(".")) to get .txt // 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..6d92e7e06 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,11 @@ 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 + +// The variable "num" stores a random whole number between 1 and 100. +// The expression works as follows: +// 1: Math.random() generates a decimal 0 to 1 (not including 1) +// This decimal is multiplied by (maximum - minimum + 1) which gives a number between 0 and 99.999. +// Math.floor() then rounds this down to a whole number between 0 to 99. +// Adding "minimum" shifts the result so the final number is between 1 to 100. +// for this reason num changes every time when you run the program always gives the number between 1 to 100. diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..8b10c8e50 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; + +// Error: The variable const for the "age" value can not be reassigned +// const means "constant" once assigned, it can not be changed. +// to fix this error, we use "let" instead of "const" to declare the age variable. \ 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..b4a8768d1 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"; + +// Reference error:can not access "cityOfBirth" before initialization +// in this code the variable "cityOfBirth" is used before it is been declared" +// solution: Move the const variable above the console.log diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..3b9fe839e 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.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 +// TypeError: cardNumber.slice is not a function. it was not the error i predicted, and the difference is that i predicted that the code is correct just add console.log variable. +// How to fix this error: first convert number "toString", then use .slice(-4);. +// The "toString()" method converts number to string. +// java script has trouble with large number so in this case we add the number in string(""). diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..252de2704 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,8 @@ const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const 24hourClockTime = "08:53"; + +// syntax error: java script does not allow variable names to start with a number as in above examples: "12HourClockTime", "24hoursClockTime". +// The value does not match in both 24hour and 12hour formats. +// Solution: move the numbers after the words or spell the numbers out. +// the variables should be named like this: const hourClockTime12 = "08:53";, const hourClockTime24 = "20:53";. +// Or like spell the number out: const twelveHourClockTime = "08:53"; , const twentyFourHourClockTime = "20:53";. diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..e8a090497 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; @@ -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? + +// Answer a: 1: carPrice.replaceAll(",", "")- line-4, 2:Number(...)- line-4, 3: priceAfterOneYear.replaceAll(",","")__line-5, 4: Number(...)__line-5, 5: console.log(...)__line-10. +// Answer b: the line 5 has an error, the second replaceAll is missing a comma between the two arguments. to fix this problem add a comma between the two arguments. +// Answer c: line 4 and 5 are variable reassignments. +// Answer d: line 1,2,7 and 8 are variable declarations. +// Answer e: 1: carPrice.replaceAll(",", "")) replaces all commas in "10,000" in an empty string "10000". 2: Number("10000") converts the string "10000" into a number 10000. +// and the purpose of this expression is to clean the price string by removing commas and convert it into a number. \ 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..29d520509 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -23,3 +23,11 @@ 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: There are 6 variable declarations in this program: movieLength, remainingSeconds,totalMinutes,totalHours,remainingMinutes, and result. +// Answer b: There is only one function call "console,log(result);". +// Answer c: The expression movieLength % 60 uses the modules operator (%) which returns the remainder after dividing movieLength by 60. +// This tells us how many remainder seconds are left after converting the total time into full minutes. +// Answer d: This line calculates the total number of complete minutes in the movie after removing the leftover seconds. in first subtract the remaining seconds then divide the rest by 60 to convert seconds by minutes. +// Answer e: result stores the formatted time in hours,minutes,seconds. A better name for this variable formattedTime,durationString,timeInHMS. +// Answer f: yes, it works for all non negative integer values of 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..43e7315e4 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -24,4 +24,15 @@ 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 = penceString.substring(0,penceString.length - 1)"__ Removes the trailing "p" character. +// substring(0,penceString.length - 1) this code gets all characters except the last one. so "399p" becomes just"399". +// 3: const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0") Ensures the pence string that has at least 3 digits, by padding it in left with "0" if needed for example: 399 stays 399 or 7 would become 007. +// 4: const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2) Extracts the pound portion (except the last two one) __ "399"-"3"(pounds) +// 5: const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0") Gets the last two digits as the pence part. for example: "399"__"99"(pence) +// padEnd(2, "0") ensures at least two digits for pence.__eg: "3"_"30". +// 6: console.log(`£${pounds}.${pence}`) logs the full formatted price in pounds and pence.__eg:for "399P" pounds"3" pence"99" and the result is __ "3.99" +// Could we expect this program to work as intended for any valid penceString if we deleted .padEnd(2, "0") from the code? +// In other words, do we really need .padEnd(2, "0") in this script? +// Yes, we need `.padEnd(2, "0")` to ensure the penceString is always at least two digits long. diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..a6af848f3 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -11,8 +11,18 @@ In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +A popup appears with the message ("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`. What effect does calling the `prompt` function have? +It opens a popup asking for the user’s name, and lets the user type something. What is the return value of `prompt`? +A string that contains what the user typed (e.g. “Ahmad”). + +If we were writing a program that uses prompt() to ask for an input value, how could +the program check if the user clicked "OK" or "Cancel"? + +If the user clicks **OK**, it returns the string they typed, even an empty string when there is nothing written. + +If the user clicks **Cancel**, it returns null. diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..5e28f58d8 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,17 @@ 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] } this output i got when i entered the 'console.log' this function is named 'log'. Now enter just `console` in the Console, what output do you get back? +console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} it contains many functions, we can use it for logging and debugging. Try also entering `typeof console` Answer the following questions: What does `console` store? +The "console" stores the collection of functions used for logging and debugging your java code. What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +these are using dot notation, console is an object and log and assert are methods(functions). +dot means access a property or method that belongs to an object.