diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..7519f8829 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 +//In line 3 we are taking the current value of count (which is 0) and adding 1 to it, and store the new value back into count" so now count is 1.the = is an assignment operator that Assigns values to variables so it takes the value on the right side (count + 1) and assigns it to the variable on the left side (count). \ 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..23a96ab68 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]}`; // https://www.google.com/search?q=get+first+character+of+string+mdn +console.log(initials); //that should print "CKJ" +//so the code is going to check the first character of each variable that means “the character at position 0 diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..e0e2b5b0e 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,12 @@ 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);// the slice is a method that cuts out part of a string and returns it as a new string. we use it here to get the dir part of the filePath +const ext = base.slice(base.lastIndexOf("."));//the ext is any thing after the last dot +//const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; +//const dir = "/Users/mitch/cyf/Module-JS1/week-1/interpret"; + +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..ee82575d1 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -2,8 +2,53 @@ 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 is a random number between minimum and maximum (inclusive) +/*Math.random() + +This gives you a random decimal number between 0 (inclusive) and 1 (exclusive) — meaning it can be 0, but never exactly 1. + +Example outputs: +0.23 + +/*(maximum - minimum + 1) + +This calculates how many whole numbers are in the range, including both ends. + +Example: +100 - 1 + 1 = 100 + +So, there are 100 possible integers from 1 to 100. + +/*Math.random() * (maximum - minimum + 1) + +This scales the random decimal to the desired range size. + +Example if Math.random() gave 0.57: +0.57 * 100 = 57 +/*Math.floor(...) +The Math.floor() method in JavaScript is used to round a number down to the nearest integer, regardless of whether the number is positive or negative or has a decimal part. +Example: +Math.floor(57.8) → 57 + +/*+ minimum + +Finally, we shift the range up so it starts at 1 instead of 0. +0–99 becomes 1–100*/ + +//The order for evaluation is: +/*maximum - minimum + 1 + +Math.random() * (that result) + +Math.floor(...) + ++ minimum*/ + +//I run the code several times and got different numbers one was 60 the other one was 88 diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..bb4556c21 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,6 @@ -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? + + +//if we have any instructions or explanations in our code without the computer executing them,we can use comments +//comments In javascript are created by using (//) for single line comments \ 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..8d492ab86 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,7 @@ const age = 33; age = age + 1; +// This will give an error because we only use const for Fixed values so we can change const to let +let age = 33; +age = age + 1; +console.log(age); // that should print 34 \ 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..023100053 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 ? -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + //we have to declare the variable cityOfBirth with const or let before using it in the console.log statement diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..ec4223fd1 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,18 @@ 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 // Before running the code, make and explain a prediction about why the code won't work +//the slice method is used for strings not numbers. // Then run the code and see what error it gives. +//Uncaught TypeError: cardNumber.slice is not a function // Consider: Why does it give this error? Is this what I predicted? If not, what's different? +//javascript can not slice a number // Then try updating the expression last4Digits is assigned to, in order to get the correct value +//I have added the console.log to see the output. +//there is another way to get the last 4 digits by using slice method is to convert the number to a string first by using toString() method +const last4Digits = cardNumber.toString().slice(-4); //it wil print "4213" +//If you want it to be a number, you can convert it back: +const last4Digits = Number(cardNumber.toString().slice(-4)); +Now last4Digits will be 4213 as a 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..07b8dea17 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,10 @@ const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const 24hourClockTime = "08:53"; + +//because variables names cannot start with a number +//I have changed the variable names to start with a letter +const twelveHourClockTime = "08:53 PM"; +const twentyFourHourClockTime = "20:53"; +console.log(twelveHourClockTime); +console.log(twentyFourHourClockTime); +// that should print "08:53 PM" and "20: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..3ec5c3051 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,26 @@ 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 - +//after running the code I found 5 function calls +//line 1: replaceAll +//line 2: replaceAll +//line 4: Number +//line 5: Number +//line 8: console.log // 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? +// after running the code i found an error in line 5 missing a comma in the replaceAll method to fix it I have added the comma // c) Identify all the lines that are variable reassignment statements +carPrice = Number(carPrice.replaceAll(",", "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); + // d) Identify all the lines that are variable declarations +let carPrice = "10,000"; +let priceAfterOneYear = "8,543"; +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? +// Fist the car price is a string so we can't do any calculation on it yet +//so first we remove all the comma by using replaceAll method then we convert it to a number by using Number method \ 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..405bfc4fd 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -8,18 +8,29 @@ const totalHours = (totalMinutes - remainingMinutes) / 60; const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; console.log(result); - +let formattedDuration = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; +console.log(formattedDuration); // 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 is 6 variable declarations // b) How many function calls are there? +// there is only 1 function call which is the last onw in line 10 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 +//modulus operator % gives the remainder after dividing one number by another.which is in this case8784 % 60 = 24 second + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +//we are converting the seconds into minutes by dividing by 60 // e) What do you think the variable result represents? Can you think of a better name for this variable? - +// we can change the name of the variable result into a clearer variable name like formattedDuration +//and if i want to change or update the value later i can use let instead of const +// formattedDuration = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; +//the result will be the duration of the movie in hours, minutes, and seconds(hh:mm:ss) format // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +//yes it will work for all values of movieLength because the code will always convert seconds into hours, minutes, and seconds format. \ 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..4cb4ce2c7 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -24,4 +24,9 @@ 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": a string variable with the value "399p" +//2.in line 3 because the value is a string we can't do any calculation on it yet we need to convert it to a number so the first step is to get rid of the letter P at the end so no matter how long the number is we get rid of the last character. +//3. in line 8 we make sure the number has 3 digits so if needed we add zeros at the start at the number +//4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);: by taking away the last 2 digit we will get the pounds in this example the amount is 399 so by taking away the 99 we will get 3 pounds +//6. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");: the same thing again now extracts the pence part by taking the last two digits if it is one digit we add 0 at the end to make it two digits +// in line 18 to print the result in the right format £ sign then pounds then a dot and then pence diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..cbc84b3be 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -11,8 +11,14 @@ In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +the browser shows a popup message box with your 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`. +let myName = prompt("What is your name?"); +The browser shows a popup with a text input box and OK / Cancel buttons. + What effect does calling the `prompt` function have? +It display a dialog box that prompts the user for input What is the return value of `prompt`? +The text the user entered (string) if clicked OK, or null if Cancel diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..e2510a537 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,17 @@ 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? +nothing Now enter just `console` in the Console, what output do you get back? +error message Try also entering `typeof console` Answer the following questions: What does `console` store? +console store Functions What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +console.log will print the message to the console +console.assert will print a message if the condition is false