diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..01ba66b16 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 + +// We are updating the previous value of count to a new value. The = operator assigns a new value +// to the variable, meaning the data stored in memory is being changed. \ 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..db7d556f7 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,8 @@ 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); \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..c2906650c 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -9,15 +9,13 @@ // (All spaces in the "" line should be ignored. They are purely for formatting.) -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}`); +const lastDotIndex = filePath.lastIndexOf("."); -// Create a variable to store the dir part of the filePath variable -// Create a variable to store the ext part of the variable +const base = filePath.slice(lastSlashIndex + 1); // file.txt +const dir = filePath.slice(0, lastSlashIndex); // /Users/mitch/cyf/Module-JS1/week-1/interpret +const ext = filePath.slice(lastDotIndex); // .txt -const dir = ; -const ext = ; - -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +console.log(`Base part of path: ${base}`); +console.log(`Dir part of path: ${dir}`); +console.log(`Ext part of path: ${ext}`); \ 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..ebed977d1 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -3,7 +3,25 @@ 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 + + +// order of math calculation - () > ** > * / % > + - (Parentheses first, then Exponentiation +// (right to left), then Multiplication/Division/Modulo (left to right), then Addition/Subtraction +// (left to right)). + +//Math.floor - returns the value of x rounded down to its nearest integer +//Math.random - returns a random number + +// First, the innermost parentheses are evaluated: (maximum - minimum + 1). +// Then, the result is multiplied by the random number generated by the Math object. +// Math.floor rounds the result down to the nearest integer. +// Finally, the value is added to the minimum. + +// the purpose - To generate a random whole number between minimum and maximum, inclusive. +// output - It will be a random integer between 1 and 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..3e6a16ca1 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 sole this by witting these two as 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..953c99674 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; +let age = 33; age = age + 1; + +// we have to use let as it allows the value to be reassign \ 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..97f2edd8d 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,15 @@ // 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}`); +// the system reads the code top to bottom and because the variable has been create/define before +// the consol.log it can not recognize the variable + +//Wrong +//console.log(`I was born in ${cityOfBirth}`); +//const cityOfBirth = "Bolton"; + +//correct 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..8094a71e1 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,22 @@ -const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); - -// 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 -// 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 +// result as a string +let cardNumber = 4533787178994213; +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); // Output: "4213" + + +// result as a number +const last4DigitsNum = Number(cardNumber.toString().slice(-4)); +console.log(last4DigitsNum); // Output: 4213 + + +// the function your asked return string +function last4DigitsStr(cardNumber) { + const last4Digs= cardNumber.toString().slice(-4); + return last4Digs; +} + +// the function your asked return number +function last4DigitsNum(number) { + const last4Digs= Number(number.toString().slice(-4)); + return last4Digs; +} \ 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..e5bddfb48 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,15 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const HourClockTime24 = "20:53"; +const hourClockTime12 = "08:53"; + +// Go Variable Naming Rules +// A variable can have a short name (like x and y) or a more descriptive name (age, price, carname, etc.). + +// Go variable naming rules: + +// A variable name must start with a letter or an underscore character (_) +// A variable name cannot start with a digit +// A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ) +// Variable names are case-sensitive (age, Age and AGE are three different variables) +// There is no limit on the length of the variable name +// A variable name cannot contain spaces +// The variable name cannot be any Go keywords \ 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..321d928a6 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,42 @@ 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 +// Function calls: +// Line 4 - .replaceAll, Number +// Line 5 - .replaceAll, Number +// Line 10 - 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? +// from line :5 +// fix: add , - priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); +// Why is the original line causing an error? +// We were trying to subtract two string values like "30000" - "24000" but +// In JavaScript, strings with commas (like "24,000") can’t be parsed directly to numbers +// So this fails + + // c) Identify all the lines that are variable reassignment statements +// line 4 , 5 + + // d) Identify all the lines that are variable declarations +// lines : 1 , 2 , 7 , 8 +// Line 1: let carPrice +// Line 2: let priceAfterOneYear +// Line 6: const priceDifference +// Line 7: const percentageChange + + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// method replaceAll is changing a specific character and replace it with the one we asked for. +// function number which changes a string number value to integers. +// purpose : is to have a integer instead of a string + +// explain why we want to convert it into an integer? +// We use Number(carPrice.replaceAll(",", "")) to convert a formatted string into a real number, +// so we can do mathematical calculations on it. \ 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..b738070b5 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -6,20 +6,32 @@ const totalMinutes = (movieLength - remainingSeconds) / 60; const remainingMinutes = totalMinutes % 60; const totalHours = (totalMinutes - remainingMinutes) / 60; -const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; -console.log(result); +const formattedTime = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; +console.log(totalMinutes); // 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? +// 6 // b) How many function calls are there? +// 1 // 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 (modulo) returns the remainder of a division. +// In this case, it returns how many seconds are left after dividing the total seconds by 60. +// For example, 8784 % 60 = 24 — so 24 seconds remain. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// we are calculating the total minutes. +// we subtract movieLength by remainingSeconds because remaining seconds don't add up to a full min. +// convert to minute by dividing by 60. // e) What do you think the variable result represents? Can you think of a better name for this variable? - +// The variable `result` represents the movie's length in a time format: `HH:MM:SS`. +// string format +// better name can be `formattedTime` or `HHMMSS` // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// Yes, the code works for all non-negative values of `movieLength`, including 0. +// However, for values less than 60, it will return 0 hours and 0 minutes \ 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..f5067030b 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -24,4 +24,23 @@ 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": initializes a string variable with the value "399p" + +// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); +// .substring - selects a section between the given indexes in a string -> "399", by doing this we remove the "p" + +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +// The padStart() method pads penceStringWithoutTrailingP with another string (multiple times) until it reaches a given length 3. +// id we have a one digit number it adds two "004" but if the number is 3 digits will keep it as it is "399" + +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); +// Extracts the pound portion, which is the first part of the 3-digit string -> "3" + +// 5. const pence = paddedPenceNumberString +// .substring(paddedPenceNumberString.length - 2) +// .padEnd(2, "0"); +// it is two separate parts +// first - with substring method we select the two last digits +// second - ensures that if somehow only one digit is returned it’s padded to two characters by adding a "0" to the end. + +// 6. Displays the formatted price in British pounds -> £3.99 or £0.05 \ 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..3c3e7c509 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -11,8 +11,18 @@ In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? + -It causes the browser to display a modal popup box with the message "Hello world!" + -It blocks interaction with the page until the user clicks "OK" + -It returns undefined — the purpose is only to notify, not to receive input. 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`. +const myName = prompt("What is your name?"); + What effect does calling the `prompt` function have? +browser displays a popup dialog box + What is the return value of `prompt`? + A string - if the user types something and clicks OK + An empty string ("") - if they click OK but type nothing + null - if the user clicks Cancel diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..de828f992 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,30 @@ 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: ƒ, …} Try also entering `typeof console` +typeof(console); +'object' Answer the following questions: What does `console` store? +console is a built-in JavaScript object provided by browsers (like Chrome, Firefox, etc.). It stores a collection of functions that help developers log, debug, and inspect values during runtime. +It doesn’t store data like a variable; instead, it holds methods (a.k.a. functions) like: +console.log() – print to the console +console.error() – print errors +console.warn() – print warnings +console.assert() – test if an expression is true + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +- `console.log` or `console.assert` +These are method calls on the console object. +You're telling JavaScript: "Use the log or assert method that belongs to the console object." +- the `.` mean +The dot (.) is the member access operator in JavaScript. +It accesses a property or method of an object \ No newline at end of file