diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..4da442b0d 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -1,6 +1,12 @@ let count = 0; -count = count + 1; +// count = count + 1; +// count += 1; + +count++; + +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 +// In line 3 they are reassign the value for count by adding count+1 and = here called assigning operator diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..0a1e1535c 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,7 @@ 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); // 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..02b939925 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(1, lastSlashIndex); +const lastDotIndex = filePath.lastIndexOf(".") +const ext = filePath.slice(lastDotIndex + 1) +console.log(dir) +console.log(ext) // 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..1765075e0 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -2,8 +2,19 @@ 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? +// num represents random whole number between 1 to 100 +//It is generated by: +//Creating a random decimal between 0 and 1 using Math.random() +//Scaling it to the range 1 to 100 +//Rounding it down using Math.floor() so it becomes an integer + // 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 +// Math.random() produces a random decimal number between 0 (inclusive) and 1 (exclusive). example : .10,.23 etc +//it will never return 1 +// scale the random decimal number using multiplication with 100(maximum-minimum+1) +//Math.floor() this method will remove the decimal part and return the nearest integer from 0 to 99 +// +minimum will shift the range from 0 to 99 to 1 to 100. \ 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..003117be0 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,5 @@ // 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) \ 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..9c1d31a46 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/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 ? +// what's the error ? Ans:They i try to prin the cityOfBirth before it was defined -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..e8e715135 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.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 + +// Slice() works on String(characters) not number +//Numbers Don't have Indexes and strings do +// So converting to String allowed me to cut the last 4 Digit of the card number. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..f35a00dd5 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,5 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const $24HourClockTime = "20:53"; +const $12hourClockTime = "08:53"; +console.log($24HourClockTime) +console.log($12hourClockTime) +// \ 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..1dc090298 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; @@ -12,11 +12,22 @@ 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 +// 5 // 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? - +//The error came from line 5 +//Because arguments inside replaceAll() were not separated properly +//After fixing the comma, the program worked +//Output became correct (14.57) // c) Identify all the lines that are variable reassignment statements - +// line 4 and 5 // d) Identify all the lines that are variable declarations + // line 1,2,7 and 8 // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + // Number(carPrice.replaceAll(",", "")) does two jobs: + +//replaceAll(",", "") → removes the commas from the string +//"10,000" becomes "10000" +//Number(...) → converts the cleaned string into a real number +//"10000" becomes 10000 diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..fed805518 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,26 @@ 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? - +// 6 // b) How many function calls are there? - +// 1 // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +//movieLength % 60 returns the remaining seconds after converting total seconds into full minutes. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +//It subtracts the leftover seconds (remainingSeconds) from the total movie length (movieLength) to get the number of seconds that make up complete minutes, and then divides by 60 to convert those seconds into total minutes. + // e) What do you think the variable result represents? Can you think of a better name for this variable? +//result is too general — it does not tell what the value means. +//The variable result represents the movie running time in hours, minutes, and seconds (HH:MM:SS) +//A better variable name would be movieRunTime or formattedRunTime. + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +//Yes, this code works for all positive integer values of movieLength. +//It converts seconds into hours, minutes, and seconds using division and remainder. +//For example, when movieLength = 5550, the result is 1:32:30. \ 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..7e526642a 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,9 +1,6 @@ const penceString = "399p"; -const penceStringWithoutTrailingP = penceString.substring( - 0, - penceString.length - 1 -); +const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); const pounds = paddedPenceNumberString.substring( @@ -25,3 +22,26 @@ 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); Here we use subString() to extracts everything except the last character. + +// 3.const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + +// padStart(3, "0") ensures the string has at least 3 characters. +// If the string is shorter than 3, zeros ("0") are added at the beginning. +// If the string is already 3 or more characters, nothing changes. +// padStart() ensures the code works for all pence values, including 1-digit and 2-digit numbers. +// Without padStart(), the logic would only work correctly for numbers that already have 3 or more digits. + +// 4.const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); + +// This line used extract the pound value 3 from the padded pence string 399 . + +// 5.const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); + +//This line extracts the last two digits from paddedPenceNumberString by starting at length - 2 and going to the end. +// Then padEnd(2, "0") ensures the pence value always has two digits. + +// 6.console.log(`£${pounds}.${pence}`); + +// Here we use console.log() to print our output and merge the pound value and pence value and add pound symbol using template literals. diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..e58cc08dc 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -11,8 +11,12 @@ In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +It will open alert and say 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 Will open the prompt and asking for the input. What is the return value of `prompt`? +prompt() returns a string, or empty string, or null (if Cancel is pressed). + diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..5034910b7 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,25 @@ 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? +I get ƒ log() { [native code] } as my output when i type console.log and then hit enter in console. Now enter just `console` in the Console, what output do you get back? +The output i get is console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} Try also entering `typeof console` +The output i get 'object' Answer the following questions: What does `console` store? +console does NOT store values. +It is used to output. + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +console.log is use print the output. +console.assert if the condition is true nothing happen +if the condition failed it will print assertion failed. +"." the member access operator in JavaScript. +it is used to connects the console object with log method. + +