diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..75f70a99c 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,7 @@ 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 updating the value of count by adding 1 to its current value, so count now equals 1 and every time we run the code it will increment by 1. diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..ed47479dc 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.charAt(0)}${middleName.charAt(0)}${lastName.charAt(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..d91354a67 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); +const ext = filePath.slice(filePath.lastIndexOf(".")); -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +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 diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..818e19a65 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -5,5 +5,31 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // 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 + +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor +// + +//Mach.random() - generates a random number between 0 (inclusive) and 1 (exclusive): +const num1=Math.random(); +console.log(num1); + +const num3=(maximum - minimum + 1); +//(maximum - minimum + 1) - calculates the range of numbers we want (in this case, 100 - 1 + 1 = 100) +//so by adding this to our random number we are scaling it to be between 0 and 100 +console.log(num3); +const num2=Math.random()*(maximum - minimum + 1); +console.log(num2); +//until this point we have a random number between 0 (inclusive) and 100 (exclusive)but is can be a decimal number +// Math.floor() - rounds down to the nearest whole number, so we now have a random integer between 0 and 99 + +const num5 = Math.floor(Math.random() * (maximum - minimum + 1)) ; +console.log(`it suppose to be a rounded number from 0-99 ${num5}`); + +//finally By adding 1, the number will be between 1 and 100, including both. + +console.log(`it suppose to be a rounded number from 1-100 ${num}`); + // 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 + diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..b585d56a6 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 use comments to ignore these lines of code, in javascript we use // for single line comments and /* */ for multi 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..c7193acfc 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,22 @@ // trying to create an age variable and then reassign the value by 1 + + const age = 33; age = age + 1; +console.log(age); +/*the constant variable cannot be reassigned a new value +so in lin 6 we will get an error because it can not reassign the age variable with a new value of age + 1 which in this case is 34 +here is the error message we will get:/home/cyf/CYF/ITP/Module-Structuring-and-Testing-Data/Sprint-1/2-mandatory-errors/1.js:6 +age = age + 1; + ^ + +TypeError: Assignment to constant variable. + + to fix this error we can use let instead of const because let variable can be reassigned a new value*/ + +let age2 = 33; +age2 = age2 + 1; +console.log(age2); + +// now it will work fine and the output will be 34 and every time we run the code it will increase by 1 \ 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..b186e8890 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,17 @@ // 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"; + +/*/home/cyf/CYF/ITP/Module-Structuring-and-Testing-Data/Sprint-1/2-mandatory-errors/2.js:4 +console.log(`I was born in ${cityOfBirth}`); + ^ + +ReferenceError: Cannot access 'cityOfBirth' before initialization + +this error is because we are trying to access the variable cityOfBirth before it is initialized and assigned a value. + the console.log should be after the variable declaration and assignment + +*/ +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..9192fcd9a 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); +const last4Digits = cardNumber.toString().slice(-4); +console.log(`The last 4 digits of my card number are ${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 + + + +//it doesn't work because the slice method is used for strings and arrays but cardNumber is a number so it doesn't have the slice method. + + + // 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 +// to fix this error we can convert the number to a string using the toString() method before applying the slice method \ 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..6666d4699 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 twelveHourClockTime = "08:53"; +const twentyFourHourClockTime = "20:53"; + +//tha variable names are cannot start with a number +// so the variable name 12HourClockTime is not valid because it starts with a number +// to fix this error we can change the variable name to twelveHourClockTime and as is not relevant to the time format +// so we can swap the variable names. \ 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..adcd0c99f 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -2,21 +2,63 @@ 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; -console.log(`The percentage change is ${percentageChange}`); +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 5 function calls in total: two in the line, and another on in console log + +carPrice = Number(carPrice.replaceAll(",", "")); + +and two in the line + +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + +In each line, one function is nested inside the other, so both lines contain two function calls each.*/ + + + // 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? + +/*priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + ^^^ + +SyntaxError: missing ) after argument list +the error is because a missing comma in the replaceAll method argument list +*/ + + // c) Identify all the lines that are variable reassignment statements + + +/*carPrice = Number(carPrice.replaceAll(",", "")); + priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + these two lines are variable reassignment statements because we are reassigning a new value to the existing variables carPrice and priceAfterOneYear.*/ + // 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; + +these four lines are variable declaration statements because we are declaring new variables: +carPrice, priceAfterOneYear, priceDifference and percentageChange using let and const keywords.*/ + + + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +/*first removes all commas from the string carPrice using replaceAll(",", ""), and then converts the resulting string into a number using 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..95c58a344 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,59 @@ 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 six variable declarations in this program: +1. movieLength +2. remainingSeconds +3. totalMinutes +4. remainingMinutes +5. totalHours +6. result +*/ // b) How many function calls are there? + /*There are 1 function calls in this code. in 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 + /*The % operator in JavaScript is the remainder operator. It divides the left-hand number by the right-hand number and returns the remainder. + + So in this case: + + movieLength % 60 + + divides movieLength (8784) by 60 and gives the remaining seconds, which is 24.*/ + + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? + /*The expression + + totalMinutes = (movieLength - remainingSeconds) / 60; + + subtracts the leftover seconds from the total seconds so that we get a whole number of minutes. + Then it divides by 60 to convert seconds into minutes without any decimal part.*/ + + // e) What do you think the variable result represents? Can you think of a better name for this variable? + /*The variable result represents the length of the movie in the format hours:minutes:seconds. + + A better name could be something like: + + movieDurationFormatted + + formattedDuration + + movieLengthHMS (HMS = hours, minutes, seconds)*/ + +// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer, + /*Yes, it works for all non-negative values: + + If the movie is less than an hour, totalHours is 0, so we get 0:MM:SS. + + If the movie is less than a minute, both totalHours and remainingMinutes are 0, so we get 0:0:SS. + + For larger movies, it correctly calculates hours, minutes, and seconds. -// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + The only exception would be negative values, which aren’t realistic for a movie length.*/ \ 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..aa8399470 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,27 +1,69 @@ -const penceString = "399p"; +const penceString = "3679p"; -const penceStringWithoutTrailingP = penceString.substring( - 0, - penceString.length - 1 -); +const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); + +console.log(penceStringWithoutTrailingP); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); -const pounds = paddedPenceNumberString.substring( - 0, - paddedPenceNumberString.length - 2 -); -const pence = paddedPenceNumberString - .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); +console.log(paddedPenceNumberString); + +const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2); + +console.log(pounds); + +const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); + +console.log(pence); console.log(`£${pounds}.${pence}`); + + // This program takes a string representing a price in pence // The program then builds up a string representing the price in pounds // You need to do a step-by-step breakdown of each line in this program + + + // 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"; + +We create a variable called penceString and give it the value "399p". + +This is the price in pence, written as text. + +2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); + +We remove the last letter "p" from the string. + +Now we only have the numbers, for example "399". + +**3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + +We make sure the string has at least 3 characters by adding zeros at the start if needed. + +Example: "50" becomes "050" and "5" becomes "005". + +4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); + +We take all characters except the last two. + +These characters are the pounds. Example: "399" → "3" pounds. + +5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); + +We take the last two characters. These are the pence. + +We add a zero at the end if needed to always have two digits. Example: "5" → "05". + +6. console.log(£${pounds}.${pence}); + +We show the price in pounds and pence. + +Example: "399p" → £3.99, "50p" → £0.50. */ diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..c79472101 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -6,11 +6,21 @@ 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: ƒ, …} + Try also entering `typeof console` +'object' Answer the following questions: What does `console` store? + +console stores different functions (like log, assert, warn, error) that you can use to interact with the console. + + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +console.log("Hello") means: Look in the console object, find the log function, and run it with "Hello" as the argument. diff --git a/Sprint-1/prep/example.js b/Sprint-1/prep/example.js new file mode 100644 index 000000000..0cbaf5e5d --- /dev/null +++ b/Sprint-1/prep/example.js @@ -0,0 +1,16 @@ +function to24hourtime(hour, minute, period) { + //if the period is pm then add 12 to hours. + + if ((hour = "12") && (minute = "00")) { + return "0000"; + } +} + +//for hours and minute make sure they are in 3 digit. +//return the hours and minute as a 4 char string. + +// hour will always range from 1 to 12 (inclusive) +// minute will always range from 0 to 59 (inclusive) +// period will always be either "am" or "pm" + +console.log(to24hourtime()); diff --git a/Sprint-1/prep/myAge.html b/Sprint-1/prep/myAge.html new file mode 100644 index 000000000..888baa26e --- /dev/null +++ b/Sprint-1/prep/myAge.html @@ -0,0 +1,101 @@ + + + +
+ + + +Please enter your year of birth:
+ +enter a decimal number and click the button to see the percentage:
+ + + +enter the width and height to calculate the area and perimeter
+ + + + +This is a test page.
+Testing 123...
+Another paragraph for testing.
+More content goes here.
+ + +