diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..8717bea0c 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -2,5 +2,7 @@ let count = 0; count = count + 1; +console.log(count); // Output: 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 reassigning the count variable to a new value which is the current value increment by 1, "=" is assigning operator that assigning value of right side which mens count increment 1 to the left side value of count variable diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..1fbf1385e 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -1,11 +1,17 @@ let firstName = "Creola"; let middleName = "Katherine"; let lastName = "Johnson"; +// The code above declares these three variables: firstName, middleName, and lastName. +// Each variable is assigned a string value representing each element in the name. +// The task is to create a new variable take initials that combines the first character from each of these strings. +// The initials variable should contain the first letter of each name, resulting in the string "CKJ". // 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 = ``; - +initials = firstName[0] + middleName[0] + lastName[0]; +console.log(initials); // Output: CKJ +// The code above uses the bracket notation to access the first character of each string [0]. +// This is a common way to get the first character of a string in JavaScript. // 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..12c062494 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -12,12 +12,16 @@ 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}`); -// 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 = ; - -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +const ext=base.slice(base.lastIndexOf(".") + 1); // The code uses the lastIndexOf method to find the position of the last dot in the base variable. +// Create a variable to store the dir part of the filePath variable +const dir = filePath.slice(0, lastSlashIndex);// The code uses the lastIndexOf method to find the position of the last slash in; +filePath.slice(0, lastSlashIndex); +filePath + .slice(lastSlashIndex + 1) + .split(".") + .pop(); +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The base part of ${filePath} is ${base}`); +console.log(`The ext part of ${filePath} is ${ext}`); diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..bace6513e 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,10 @@ 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); +// Output: A random number between 1 and 100 (inclusive both 1 and 100) +//num is a random number generated between the minimum 1 and maximum values 100. +// The expression Math.random() generates a random floating-point number between [0,1). +// The expression (maximum - minimum + 1) calculates the range of numbers we want to generate, which is 100 - 1 + 1 = 100. +// The expression Math.floor() rounds down the result to the nearest whole number. +// Finally, we add the minimum value to ensure that the random number falls within the desired range. diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..044add7ac 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,2 @@ -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? diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..a1391a233 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,6 @@ const age = 33; age = age + 1; +console.log(age); // Output: 34 +// The code above declares a variable age with an initial value of 33. +// It then reassigns the value of age by adding 1 to its current value, so the declaration must be using 'let' keyword. diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..b7e608bee 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 ? - -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + +//The error is that the variable cityOfBirth is being used before it has been declared.JavaScript executes code line by line from top to bottom, so you must declare and assign a value to the variable before using it. +// In JavaScript, variables declared with let or const are not hoisted, meaning they cannot be accessed before their declaration. diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..8c4a85cd5 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,10 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +// Prediction: The code will not work because the `slice` method is being called on a number type (`cardNumber`), +// and the `slice` method is a string method. In JavaScript, numbers do not have a `slice` method. +// Running the code will likely result in a TypeError. + +const last4Digits = cardNumber.toString().slice(-4); // Convert cardNumber to a string first +console.log(`The last 4 digits of the card number are ${last4Digits}`); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..1392058b1 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,37 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +// const 12HourClockTime = "20:53"; it is in 24 hour format +// const 24hourClockTime = "08:53"; it is in 12 hour format then we will write correctly +// const twelveHourClockTime = "08:53 PM"; // Example 12-hour format time +// const twentyFourHourClockTime = "20:53"; // Example 24-hour format time + +const twelveHourClockTime = "08:53 PM"; // Example 12-hour format time +const twentyFourHourClockTime = "20:53"; // Example 24-hour format time +function convertTo12HourClockTime(time24clock) { + let [hours, minutes] = time24clock.split(":").map(Number); + const modifier = hours >= 12 ? "PM" : "AM"; + + if (hours === 0) { + hours = 12; // Midnight case + } else if (hours > 12) { + hours -= 12; // Convert to 12-hour format + } + + return `${hours}:${minutes.toString().padStart(2, "0")} ${modifier}`; +} +function convertTo24HourClockTime(time12clock) { + let [time, modifier] = time12clock.split(" "); + let [hours, minutes] = time.split(":").map(Number); + + if (modifier === "PM" && hours !== 12) { + hours += 12; + } else if (modifier === "AM" && hours === 12) { + hours = 0; + } + + return `${hours.toString().padStart(2, "0")}:${minutes + .toString() + .padStart(2, "0")}`; +} +console.log(convertTo12HourClockTime(twentyFourHourClockTime)); // Output: "08:53 PM" +console.log(convertTo24HourClockTime(twelveHourClockTime)); // Output: "20:53" +// Output: "20:53" +// Output: "08:53 PM" diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..b7a33b59e 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,23 @@ 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 this file. The code consists of variable assignments and calculations without any function definitions or calls. +// replaceAll(",", "") (twice) +// Number(...) (twice) +// console.log(...) (once) // 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? +// There are no errors in the code. The code runs successfully and calculates the percentage change in car price without any issues. // c) Identify all the lines that are variable reassignment statements +// The variable reassignment statements are: +// 1. carPrice = Number(carPrice.replaceAll(",", "")); +// 2. priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); // d) Identify all the lines that are variable declarations - +// The variable declarations are: +// 1. let carPrice = "10,000"; +// 2. let priceAfterOneYear = "8,543"; +// 3. const priceDifference = carPrice - priceAfterOneYear; +// 4. const percentageChange = (priceDifference / carPrice) * 100; // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// The expression `Number(carPrice.replaceAll(",", ""))` is converting the string representation of the car price, which includes commas (e.g., "10,000"), into a number. The `replaceAll(",", "")` part removes all commas from the string, resulting in "10000", and then the `Number()` function converts this string into a numeric value (10000). This is necessary for performing arithmetic operations on the price. diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..87ef5c9f5 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = 9000; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,14 +12,27 @@ 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 6 variable declarations +// 1. movieLength +// 2. remainingSeconds +// 3. totalMinutes +// 4. remainingMinutes +// 5. totalHours +// 6. result // b) How many function calls are there? +// their is 1 function call +// console.log(result); -// c) Using documentation, explain what the expression movieLength % 60 represents +// c) Using documentation, explain what the expression movieLength % 60 represents The is expressing remaining number of seconds after converted to 60 seconds +// The expression `movieLength % 60` calculates the remainder when `movieLength` (the length of the movie in seconds) +// This gives the number of seconds that do not complete a full minute, effectively representing the remaining seconds after converting the total movie length into minutes. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators // d) Interpret line 4, what does the expression assigned to totalMinutes mean? - +// The expression (movieLength - remainingSeconds) / 60 determines the total number of full minutes in the movie by removing the leftover seconds and then dividing the result by 60. // e) What do you think the variable result represents? Can you think of a better name for this variable? - +//time +// The variable `result` represents the formatted time of the movie in the format "hours:minutes:seconds". +// A better name for this variable could be "movieDuration" to make it clearer that it holds the duration of the movie in a specific format. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..f5a553523 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": initialize a string variable with the value "399p" +//2. Removes the trailing "p" from the string: and result will be 399 +//3. Ensures the number has at least 3 digits by padding from the left with zeros: like 000 +//4. Extracts the pound portion: "3" +//5.Extracts the pound portion: "99" +//6. Extracts the pound portion: in the standard monetary format of pounds and pence: "£3.99" diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..6a4bab498 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -11,8 +11,11 @@ In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +This displays a popup box (called an "alert dialog") in the browser with the message: '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? +A popup input box appears asking: "What is your name?" What is the return value of `prompt`? +surafel diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..c4f6927a1 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,15 @@ 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? - -Try also entering `typeof console` +console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} +Try also entering `typeof console` // object 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 `console` store? different functions (methods) like s log messages, show errors, warnings + +What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?`console.log` means accessing 'log' function inside 'console' object +`console.assert` mean accessing 'assert' function inside 'console' object +`.` is the dot notation and It’s used to access properties or methods of an object.