diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..47877f59e 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -3,4 +3,7 @@ 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 is adding 1 to count variable and then saving this new value to count variable +// = sign is being used to assign the new value of count to the count variable diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..20b2c255e 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -6,6 +6,6 @@ let lastName = "Johnson"; // 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 = ``; +initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); // 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..b10fe94ec 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -12,12 +12,14 @@ 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}`); +console.log(`The base part of ${filePath} is: \n ${base}` + "\n"); // 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); +console.log(`The dir part of ${filePath} is: \n ${dir}` + "\n"); +const ext = filePath.slice(filePath.lastIndexOf(".")); +console.log(`The ext part of ${filePath} is: \n ${ext}`); -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +// https://www.google.com/search?q=slice+mdn diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..5186455b1 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -2,8 +2,20 @@ 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 in the above code represents integer numbers from 1 to 100 +// random() method in Math class is used in the above code returns floating numbers between 0(inclusive) and 1(exclusive) +/*and (maximum - minimum + 1) will give use the number that when multiplied by values between +0(inclusive) and 1(exclusive) we will get results from 0 to 99.99999999..... including any decimal values in between +and the Math.floor is used to get rid of the decimal values and return only numbers; so our +values will become 0 to 99 only numbers. +And at the end part of the equation we are adding minimum value i.e. 1 which will result in +assigning values from 1 to 100 in num; that's why we are getting only values from 1 to 100 in our num +and every time we print the num in console.log() we can get any values between 1 to 100 depending on the radom value returned +by the Math.random() method.*/ diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..50f3ce8c2 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? + +//we can solve this by making the above statements as comments using // diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..d4e367e31 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,12 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +//const age = 33; +//age = age + 1; + +/* the mistake in the above code is the use of const which we use only to save the constant values +which we don't want to change in the future; therefore, in the above example we would declare age +as a variable rather than constant to solve the issue.*/ + +let age = 33; age = age + 1; +console.log(age); diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..5b482e529 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,10 @@ // 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}`); +//console.log(`I was born in ${cityOfBirth}`); +//const cityOfBirth = "Bolton"; + +//the above code is not working because you are printing or using the cityOfBirth constant before initializing it +// it can be solved by declaring and initializing the constant before using it as shown below: 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..3eb5ef73d 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,12 @@ 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 + +//I predicted that the code is not working because the cardNumber is a constant value and cannot be changed +// but actually we are not changing the value of cardNumber we are just slicing it which will be stored in a different variable +//and the cardNumber value will remain the same +//After running the code, I have realized that the code is not working because cardNumber is a number and the slice is a method in String class +//so, in order to make it work, we need to change cardNumber to String before calling the slice method + +const last4Digits = (cardNumber + "").slice(-4); +console.log("Last four digits of card are : " + last4Digits); diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..5395578b5 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,9 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +//const 12HourClockTime = "20:53"; +//const 24hourClockTime = "08:53"; + +const twelveHourClockTime = "20:53"; +const twentyFourhourClockTime = "08:53"; + +/*in javascript and also many other programming languages, we can't start the name of an identifier +// with a number, therefore, I have changed that to text form*/ + diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..b7eff0b39 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -2,7 +2,9 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); //error line of code +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," , "")); //fixed + const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -12,11 +14,33 @@ 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 three function calls in this file as shown by the following lines from the code: +carPrice = Number(carPrice.replaceAll(",", "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); +console.log(`The percentage change is ${percentageChange}`); + +replaceAll and log are the functions in the above lines */ // 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 is coming from the following line inside the replaceAll method that accepts two parameters separated by comma +but in our code there is no comma for the separation and that is the problem +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +we can fix this by simply adding the comma after first parameter value as follows: +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," , "")); */ // c) Identify all the lines that are variable reassignment statements +/* The following two lines are the variable reassignment statements: +carPrice = Number(carPrice.replaceAll(",", "")); +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); */ // d) Identify all the lines that are variable declarations +/* All the lines that are variable declarations are as follows: +carPrice = Number(carPrice.replaceAll(",", "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); //error line of code +const priceDifference = carPrice - priceAfterOneYear; +const percentageChange = (priceDifference / carPrice) * 100; */ // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +/* This code is replacing all the comma characters with an empty character inside the carPrice String and then converting that String to a Number +The purpose of doing this is because in the later part of code we are doing some Math calculations on this number +and, therefore, we can't use String, we need to remove the commas in the String and convert that String 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..60f3a8cfa 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,24 @@ 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? +// Ans: 6 // b) How many function calls are there? +// Ans: 1 // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +/* Ans: In this expression the operator % is used which will return the remainder when we divide movieLength by 60 + Specifically, in the above code it is used to get the remaining seconds which will be left over after + converting the movieLength i.e given in seconds into minutes. */ + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +/* Ans: Here, we are taking out all the remainder or left over seconds from the movieLength seconds + and then dividing that value by 60 to get the length of movie in minutes. */ // e) What do you think the variable result represents? Can you think of a better name for this variable? - +/* Ans: result represents the total length of movie in hours, minutes and seconds. The better name + could be "lengthOfMovieInHMS" */ // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// Ans: Yes, this code will work with all number 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..133e399ba 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,20 +1,47 @@ +//initialises a string variable with the value "399p" const penceString = "399p"; +//creates a substring of penceString to remove the trailing or the last character p value and +//assign the new value to "penceStringWithoutTrailingP" const penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1 -); +); //it will save 399 in penceStringWithoutTrailingP + +/* The method padStart is used when we want our string to have at least some length; + in this case if the length of our string is less than 3, we are going to put "0" + in extra spaces + The reason for padding the pence number string to 3 size is because in the next step + we will create a substring by removing the last two values + and that will give us the pounds number value and the remaining characters will be the + pence value. */ +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); //it will not change our string because 399 already has three characters -const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +/* Here we are trying to get the pounds value out of the total pence value + Therefore we are creating a substring of paddedPenceNumberString to + create a string that removes the last two numbers from paddedPenceNumberString + which are pence numbers. */ const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2 ); + +/* Here, we are doing two things: + 1. we are creating a substring of paddedPenceNumberString + to get the pence numbers only by returning the last two numbers. + 2. We are using padEnd method to pad our result to two character values + and adding 0 at the end if the length of our substring is less than 2 + + Note: However, our paddedPenceNumberString length is always going to be at least 3 + and the below substring is going to return us the last two characters which is pence value + So, I don't understand the use padEnd and in fact, I tried commenting it and tested the code + with different penceString values and the results are same. */ const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); - + +// in the below code we are printing the value starting with £ symbol of pounds and pence separated by "." console.log(`£${pounds}.${pence}`); // This program takes a string representing a price in pence diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..85aa97e93 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? +Ans: It creates an alert message on my tab with the text "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? +Ans: It creates a message box on my tab with the input `"What is your name?"` and there is an input field beneath it for answering this question. + What is the return value of `prompt`? +Mehroz diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..870458ce1 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,102 @@ 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: ƒ, …} +assert +: +ƒ assert() +clear +: +ƒ clear() +context +: +ƒ context() +count +: +ƒ count() +countReset +: +ƒ countReset() +createTask +: +ƒ createTask() +debug +: +ƒ debug() +dir +: +ƒ dir() +dirxml +: +ƒ dirxml() +error +: +ƒ error() +group +: +ƒ group() +groupCollapsed +: +ƒ groupCollapsed() +groupEnd +: +ƒ groupEnd() +info +: +ƒ info() +log +: +ƒ log() +memory +: +MemoryInfo {totalJSHeapSize: 11200000, usedJSHeapSize: 10000000, jsHeapSizeLimit: 3760000000} +profile +: +ƒ profile() +profileEnd +: +ƒ profileEnd() +table +: +ƒ table() +time +: +ƒ time() +timeEnd +: +ƒ timeEnd() +timeLog +: +ƒ timeLog() +timeStamp +: +ƒ timeStamp() +trace +: +ƒ trace() +warn +: +ƒ warn() +Symbol(Symbol.toStringTag) +: +"console" +[[Prototype]] +: +Object + Try also entering `typeof console` +It is object Answer the following questions: What does `console` store? +console is an object that does not store anything but it is used to call debugging +methods such as log or assert or trace + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +it means that the console object is calling its methods log or assert using the dot "." operator +