diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6e..aec2d06fa0 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,6 @@ 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 +// it updates the value of Count variable by addiing 1 to its current value. +// count value hold 1 + diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f6175..26d6c6a212 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,9 @@ 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]}`; +initials = initials.toUpperCase(); +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 ab90ebb28e..5ac3899d5e 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 =filePath.slice(filePath.lastIndexOf(".")); +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The ext part of ${filePath} is ${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 292f83aabb..c105a1a9a1 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -1,7 +1,19 @@ const minimum = 1; const maximum = 100; -const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +// const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +let range = maximum - minimum + 1; +let rand = Math.random() ; // generates a random number between 0 and 1 (not including 1) +let rand100 = rand * range; // generates a random number between 0 and 100 (not including 100) +let rand99 = Math.floor(rand100); // generates a random number between 0 and 99 (not including 100) +const num = rand99 + minimum; + + + +console.log(`The random number between 0 and 1 is ${rand}`); +console.log(`The random number between 0 and 100 is ${rand100}`); +console.log(`The random number mutipled with range is ${rand99}`); +console.log(`The random number is ${num} between ${minimum} and ${maximum}`); // 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-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f7..65ad3030d6 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,2 @@ -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? \ 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 7a43cbea76..4d7964e4bd 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; +//const age = 33; we should change to let age = 33; to allow reassignment +let age = 33; age = age + 1; diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831d..ff4ad5252f 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,6 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... -// what's the error ? +// what's the error ? in line 4 the variable cityOfBirth is being used before it is declared. We should declare the variable before using it in the console.log statement. -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 ec101884db..2b1473d638 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,5 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +//const last4Digits = cardNumber.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +7,9 @@ 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 is not working becuase slide method is for strings where as cardNumber is a number. We should convert the cardNumber to string before using slice method. +//Synax error in line 2, but the result is not clear from the output. + +const last4Digits = cardNumber.toString().slice(-4); +console.log(`The last 4 digits of ${cardNumber} are ${last4Digits}`); \ 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 5f86c730bc..f4e42195fc 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/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"; +// valid identifier don't start with a number \ 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 e24ecb8e18..56d750e671 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,21 @@ 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 - +// There are 4 fucntion calls + //Number(carPrice.replaceAll(",", "")); 2 calls Function Number and Function replaceAll + //Number(priceAfterOneYear.replaceAll(",""")); 2 calls Function Number and Function replaceAll // 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 priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +// ^^^SyntaxError: missing ) after argument list +// add semicolon after te the first argument in the replaceAll method to fix the error // c) Identify all the lines that are variable reassignment statements +//line 4 to varaible carprice //carPrice = Number(carPrice.replaceAll(",", "")); +//line 5 to variable priceAfterOneYear //priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); // d) Identify all the lines that are variable declarations - +//line 1 to variable carPrice //let carPrice = "10,000"; +//line 2 to variable priceAfterOneYear //let priceAfterOneYear = "8,543"; +//line 7 to variable priceDifference //const priceDifference = carPrice - priceAfterOneYear; +//line 8 to variable percentageChange //const percentageChange = (priceDifference / carPrice) * 100; // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// it replace all the commas with empty string and convert the string to 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 47d2395587..3bb8c3a7b6 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = 60; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,14 +12,22 @@ 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 declarations. // b) How many function calls are there? +// one function call in the program, which is console.log(result); // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators - +//modulo operator returns left over after division. the expession movieLength % 60 returns the +// remaining seconds after dividing the movieLength by 60. In this case, it returns 24, which is the remaining seconds after dividing 8784 by 60. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? - +// the expression covert the movieLength from seconds to minutes. +// it subtracts the remaining seconds from the movieLength and divides the result by 60 to get the total minutes. In this case, it returns 146, which is the total minutes of the movie length. // e) What do you think the variable result represents? Can you think of a better name for this variable? - +// the total length of movie in hours,minutes and seconds. +// a better name movieLengthHMS would be more clear. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// No, it will not work for all values of movieLength. +// if movieLength is above 24 hours, the code will create an incorrect format of hours. +// example 52:26:24 which don't make sense. \ 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 60c9ace69a..57d690b21b 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,19 +1,25 @@ -const penceString = "399p"; +const penceString = "399p";// initialises a string variable with the value "399p" const penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1 ); +// The substring method is used to extract a portion of the string, starting from index 0 and ending at the second-to-last index (length - 1). const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +//padStart method is used to add leading zeros to the penceStringWithoutTrailingP until it reaches a length of 3 characters +// The result is stored in a new variable called paddedPenceNumberString. +//esure that to represent like 0.09 or 3.99 const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2 ); - +//slice the paddedPenceNumberString until the last two characters, which represent the pounds. const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); + //slice the last two characters of the paddedPenceNumberString, which represent the pence. + // by using penEnd method esure the it is two characters long. if it is not add zeros to end. console.log(`£${pounds}.${pence}`); @@ -25,3 +31,14 @@ 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): +// removes the last character "p" from the penceString and stores the result in a new variable called penceStringWithoutTrailingP. +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): +// adds leading zeros to the penceStringWithoutTrailingP until it reaches a length of 3 characters. +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): +// it like slicing , until the last two of the paddedPenceNumberString that represents the pounds. +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): +// takes the last two characters +// the panEnd method esure the it is two characters long. if it is not add zeros to end. +// 6. console.log(`£${pounds}.${pence}`): +// Final result in console in the format of pounds and pence. \ No newline at end of file diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feafe..35f5d9d5fd 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 displays message box Hello world! with ok option 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`. +myName=prompt("Enter your name") What effect does calling the `prompt` function have? +it diplay text field to input name with options to for ok and cancel. What is the return value of `prompt`? +it will return the value inserted/inputed in the texfield, which is your name. \ No newline at end of file diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56a..27105d0129 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/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] } it does not call the function the name of function and [native code] means browser can't show the code. Now enter just `console` in the Console, what output do you get back? +console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} list of functions with their names Try also entering `typeof console` +'object' + Answer the following questions: What does `console` store? +it holds collection methods like log and error. What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +dot variable(.) used to access the methods of console object. \ No newline at end of file