diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..2238bfd9c 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,4 @@ 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 take the count and add 1 to that so as the count was 0 line 3 make it 1. \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..c989b43d9 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -4,8 +4,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 = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); -let initials = ``; + 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..89898afe8 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,11 @@ 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 + 1); +const lastSlDotIndex = filePath.lastIndexOf("."); +const ext = filePath.slice(lastSlDotIndex); + +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 292f83aab..c7eec5b7f 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,12 @@ 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 + +console.log(num); + +// this will make a random number between 1-100. Math.random will give us a number between 0-1 (both ends included). +// (maximum - minimum + 1): this is equal to 100-1+1=100 in this example. +// (Math.random() * (maximum - minimum + 1)): this will multiply the random number which is between 0-1 by 100 so we have a number. +// Math.floor: this will round the number to the nearest lower whole number. +// + minimum: will add 1 (the defined minimum) to the number so it never be below 1. +// so we end up with a random number between 1-100 \ 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..7a1b95df3 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 need to add double "//" at the start of the line so computer will ignore this line. \ 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..0b227e7ab 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,6 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +// const age = 33; age should be defined as let so it can get different values. +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..f172f4a91 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,7 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? +// any variable must be defined first and then can be used. -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); +// const cityOfBirth = "Bolton"; => Wrong place! diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..3ab022d80 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 last4Digits = cardNumber.toString().slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +8,10 @@ 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() is for strings but here we have 1 number here. so we need to make that number a string by putting it in a "". or using .toString() to change any input to string type. + + +console.log(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 21dad8c5d..a1ee24b93 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,7 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +//const 12HourClockTime = "20:53"; +//const 24hourClockTime = "08:53"; + +// An identifier or keyword cannot immediately follow a numeric literal. + +const ClockTime24Hour = "20:53"; +const ClockTime12hour = "08:53"; \ 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..d9bd870d3 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -11,12 +11,34 @@ 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 +// a) How many function calls are there in this file? Write down all the lines where a function call is made + +// lines 4,5,7,8 +// the following 4 lines are functions as they are manipulating the data. +//carPrice = Number(carPrice.replaceAll(",", "")); +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ,"")); +//const priceDifference = carPrice - priceAfterOneYear; +//const percentageChange = (priceDifference / carPrice) * 100; // 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("," "")); the error is a missing comma between the two arguments in the .replaceAll() function call. Without the comma, +// the two string arguments "," and "" are not separated, so the computer reads them together as one argument. + // c) Identify all the lines that are variable reassignment statements +// lines 4,5 +//carPrice = Number(carPrice.replaceAll(",", "")); +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + +//carPrice = Number(carPrice.replaceAll(",", "")); +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + + // d) Identify all the lines that are variable declarations +// lines 1,2,7,8 + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// It replace the "," with "" which means omitting the "," and add nothing so we have whole number form that computer can work with it. diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..9fee4fda1 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -13,13 +13,26 @@ console.log(result); // a) How many variable declarations are there in this program? +6 + // b) How many function calls are there? +// 1 : 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 +// % is the remainder. that line of code divides the total seconds number by 60 and gives us the remaining seconds. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// in this line, totalMinutes of the move calculated. the remainder will be deducted and the rest of the second divided by 60 gives us the total minutes number. + + // e) What do you think the variable result represents? Can you think of a better name for this variable? +// it represents the move length in hour, minutes and seconds. and it would be better understood as duration. + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +// yes as long as it receives the total seconds as a whole number it works fine. \ 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..3d79aefbd 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,20 +1,9 @@ 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( - 0, - paddedPenceNumberString.length - 2 -); - -const pence = paddedPenceNumberString - .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); - +const pounds = paddedPenceNumberString.substring( 0 , paddedPenceNumberString.length - 2); +const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) +.padEnd(2, "0"); console.log(`£${pounds}.${pence}`); // This program takes a string representing a price in pence @@ -25,3 +14,13 @@ 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): creates a new string with the last character removed by taking a substring from index 0 up to +// penceString.length - 1, it removes the trailing "p" so we are left with the numeric pence portion as a string. +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): ensures the numeric string has at least 3 characters by adding leading zeros if required. +// padStart(3, "0") will left-pad with "0" until the length is at least 3, so the code expects to split the string into ...pounds and last two digits = pence. +// By guaranteeing at least 3 characters, the substring operations that follow always have at least one digit for pounds and two for pence. +// 4. const pounds = paddedPenceNumberString.substring( 0 , paddedPenceNumberString.length - 2): takes the substring from the start up to the character two from the end. +// That is, everything except the last two characters. as the last two characters represent pence; everything before that is pounds. Splitting this way converts a string of +// pure pence into pounds and pence parts. +// 5. substring(paddedPenceNumberString.length - 2): takes the last two characters (the pence portion). +// 6. .padEnd(2, "0"): would add trailing zeros to make sure the pence string is at least 2 characters long. \ 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 e7dd5feaf..1a7239f5d 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -11,8 +11,10 @@ In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +"Hello world!" will be printed in a pop up box. 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? +A pop up window with the question and a place to input answer is opening. What is the return value of `prompt`? +The input value will be returned by prompt() and stored in the variable myName. \ 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 0216dee56..f86e81584 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -1,16 +1,22 @@ ## Objects -In this activity, we'll explore some additional concepts that you'll encounter in more depth later on in the course. +//In this activity, we'll explore some additional concepts that you'll encounter in more depth later on in the course. -Open the Chrome devtools Console, type in `console.log` and then hit enter +//Open the Chrome devtools Console, type in `console.log` and then hit enter -What output do you get? +// What output do you get? +// ƒ (){for(var o=arguments.length,u=new Array(o),l=0;lt.toString())).splice(1),c=("assert"===… -Now enter just `console` in the Console, what output do you get back? +//Now enter just `console` in the Console, what output do you get back? +// console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} -Try also entering `typeof console` +//Try also entering `typeof console` +//Answer the following questions: +//What does `console` store? +//The console in JavaScript doesn’t store the program’s variables or data — instead, it’s an object provided by the browser (or Node.js) that contains a set of methods for +//logging information, debugging, and interacting with the JavaScript environment. -Answer the following questions: - -What does `console` store? -What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +//What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +//In both cases, console is an object, and log or assert are properties (specifically, methods) of that object. The dot (.) is called the dot notation operator or member +//access operator. It is used in JavaScript (and many other languages) to access a property or method that belongs to an object.