diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..2711fd150 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -1,6 +1,9 @@ let count = 0; count = count + 1; +/* in line three, the variable count is reassign new value by increment it current value by 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 diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..10cc52f0a 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..4c6b41eed 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -16,8 +16,17 @@ 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 slashIndex = filePath.indexOf("/"); +const dir =filePath.slice(slashIndex + 0, 45) ; +const ext =filePath.slice(slashIndex + 49, 53); +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The ext part of ${filePath} is ${ext}`); +*/ + +let dirDirectory = filePath.slice(filePath.indexOf("/"),filePath.lastIndexOf("/")); +let extDirectory = filePath.slice(filePath.lastIndexOf("/")); +console.log(`The dir part of the file is ${dirDirectory}.`); +console.log(`The ext part of the file is ${extDirectory}.`); // 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..5133cbed3 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -3,7 +3,16 @@ 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 + +/*Number data type represents any type of number such as integer, float number(decimal number), infinity number, etc... +In the code at line 4 the num variable are doing math function that generate a integer between variable minimum and maximum value, +the floor method behind the Math are is main purpose if to help turn value of a float number and round it up to the nearest integer. +inside the Math.floor parameter the math random method is choosing a random float number between 0 and 1 and have it multiply with the second expression +to set the range of possible value range from 0 to under 100. The last plus one to make sure the that number stay above 0(0+1) and to 100(99+1); +*/ \ 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..eb485e19f 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,4 @@ -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? +By comment both the line 1 and 2 out and the system should ignore these 2 line and see them as comment. +*/ \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..3c88f39b3 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,8 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; -age = age + 1; +let age = 33; +age += 1 ; + +console.log(age); + +/*By changing the variable from const to let since const variable value can't be change. I used prefix to increase the value and reassign it right away.*/ \ 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..45eda0213 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"; + +/*the main the error here is that the console.log try to print the expression string template with the variable cityOfBirth. But since in the variable +cityOfBirth declare after the console.log, so the console.log when try to involve the cityOfBirth will return error since the it doesn't exist. +*/ diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..3872d807a 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,7 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); +console.log(typeof last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..0a22b02ff 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,6 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "08:53"; +console.log($12HourClockTime); +console.log($24HourClockTime); + +/*For this error I I fix by changing the number 12 and 24 to word variable name because in JS number can't be used to begin writing a variable name */ \ 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..76c80706b 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,30 @@ 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 section of the code, we have function calls appear 5 time through out the code. Here is each of them on the line of code that +they are call: + - Number(carPrice.replaceAll(",","")); in line 4 is a function calls because the .replaceALL is being executed + - Number(priceAfterOneYear.replaceAll(",", "")); in line 5 is also a function call because .replaceAll is being executed. + - Inside the Number() function the .replaceAll in both line 4 and 5 is also a function call because the the parent function is also + being executed. + - the console.log is a function calls +So the total amount of function call in this code is 5 + +b) The main error for the when the running this line of code if for the missing right parameter between the arguments, so fix this +just add the coma right after the double quote. + +c) There are 2 reassign variable statement at line 4 and line 5. + +d)There are 4 total variable declarations in this code: + - let carPrice = "10,00"; line 1 + - let priceAfterOneYear ="8,543"; line 2 + - const priceDifference = carPrice - priceAfterOneYear; line 7 + - const percentageChange = (priceDifference / carPrice) * 100; line 8 + +e) The expression Number(carPrice.replaceAll(",","")) are doing 2 main purpose, + - The first purpose is to remove the coma from the string number "10000" and "8543". + - Is the changing the string number to 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..d0c275d6f 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -23,3 +23,31 @@ 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 this program there are 5 variable declarations: + -const movieLength = 8784; at line 1. + -const remainingSeconds = movieLength % 60; at line 3. + -const totalMinutes = (movieLength - remainingSeconds) / 60; at line 4. + -const remainingMinutes = totalMinutes % 60; at line 6. + -const totalHours = (totalMinutes - remainingMinutes) / 60; at line 7. + +b) There are only 1 function call in this program. + - console.log(result); at line 10 + +c)At the line 3 we can see the the expression movieLength % 60, +what is represent is the reminder value after the division movieLength(in second) value by 60%; +this is to remind how much left over second left in the movie. + +d)In line 4, the the expression that is assigned to totalMinutes +represent the value after the math operation if movieLength value and remainingSeconds then divine by 60; + +e)For the variable result it represent the combine total time length of the movie in hour/minutes/second template, for better name for this variable +I would to rename it to formattedMovieDuration. + +f) After the experimenting with different values of movieLength. As Long as the value in movieLength is a positive integer of second. It will +correctly produce the correct value in hours/minutes/second format. However for to the other value such as float number, string, negative integer +and non numeric value. They might still print out in the format hours/minutes/second but some type will break code such as if string not a number or coma in a string +number, negative value that don't reflect what this code try to do or decimal number that give incorrect result. +*/ \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..fbf4af92b 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -24,4 +24,31 @@ 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: this variable is now store a new value of penceString "399". +By using the .substring method(.slice method predecessor)it remove the p from the penceString value and +reassign the new value to variable penceStringWithoutTrailingP. It start from index position 0 in this case "3" and stop at the index 2 "9". + +3. const paddedPenceNumberString: this variable main role is to check if the string value total length +at least 3, if the length short then padStart will add 0 the left from the first index(0) in this case "3" until the string length total is 3. In this case +it select the string number "3". + +4. const pounds: this variable is role is to extract the pound value from the paddedPenceNumberString. +Inside the the paddedPenceNumberString subString method parameter it have 2 value 0 and paddedPenceNumberString total string length minus 1, +the 0 is the index of the string number that we would like to start while .length - 2 is to tell the SubString method where to stop in this case the first "9". + +5) const pence: as the name suggest this variable main role is to extract he pence value from the paddedPenceNumberString variable. +How ever this variable there are 2 method is used. + -subString method is used to slice the string value "99" off the "399" value, how ever is now only have 1 value + is the length of the "399" is 3 now - 2 equal to 1 and no end value. The main reason is to select where the slice will begin stop at the end + of the string. + -padEnd method work exactly the same as padStart but in reverse. The padEnd method is checking to see is the paddedPenceNumberString string length at least total of 2, + if not they will add 0 from the selected index position from the string value "99" from the index 0 in this case is "9" to the right. + +6)console.log(`£${pounds}.${pence}`): is to log and print out the value inside the terminal by using the string template, the back tick is to +define a template literal. Then inside the template the pound sign(£) is show the pound currency, the money sign before the curly bracket is to allow +embed variables in this case pounds and pence that separate by dot that print out a float string value. +*/ + \ No newline at end of file