From 7ef556f8836668a8b735bc6bf437eeacbf99e20d Mon Sep 17 00:00:00 2001 From: zeliha pala Date: Tue, 7 Nov 2023 22:16:15 +0000 Subject: [PATCH 1/8] 0.js done --- week-1/errors/0.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/week-1/errors/0.js b/week-1/errors/0.js index cf6c5039..537e0733 100644 --- a/week-1/errors/0.js +++ b/week-1/errors/0.js @@ -1,2 +1,3 @@ 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 +We don't want the computer to run these 2 lines - how can we solve this problem? +// we use comments for them \ No newline at end of file From e6a23a9c7dc8ca895bf52d11d34c567202eafdf8 Mon Sep 17 00:00:00 2001 From: zeliha pala Date: Tue, 7 Nov 2023 22:47:35 +0000 Subject: [PATCH 2/8] started errors --- week-1/errors/0.js | 6 +++--- week-1/errors/1.js | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/week-1/errors/0.js b/week-1/errors/0.js index 537e0733..d698eab0 100644 --- a/week-1/errors/0.js +++ b/week-1/errors/0.js @@ -1,3 +1,3 @@ -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 use comments for them \ 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 use comments for them diff --git a/week-1/errors/1.js b/week-1/errors/1.js index 7a43cbea..5f606dd9 100644 --- a/week-1/errors/1.js +++ b/week-1/errors/1.js @@ -1,4 +1,9 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +//const age = 33; +//age = age + 1; + +//we can not reassign a new value to a constant. if we reassign a value to a const variable, we will get a "TypeError." but we can do this; +let age = 33; age = age + 1; +console.log(age); From d8034b53ce9db788dd8518395a4087538e3c7e4b Mon Sep 17 00:00:00 2001 From: zeliha pala Date: Wed, 8 Nov 2023 00:14:30 +0000 Subject: [PATCH 3/8] errors done --- week-1/errors/2.js | 4 ++-- week-1/errors/3.js | 7 ++++++- week-1/errors/4.js | 4 +++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/week-1/errors/2.js b/week-1/errors/2.js index e09b8983..fe7fc81c 100644 --- a/week-1/errors/2.js +++ b/week-1/errors/2.js @@ -1,5 +1,5 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... -// what's the error ? +// what's the error ? cityOfBirth variable needs to be declared before console.log. -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); diff --git a/week-1/errors/3.js b/week-1/errors/3.js index ffa72ca4..de3559bf 100644 --- a/week-1/errors/3.js +++ b/week-1/errors/3.js @@ -1,6 +1,11 @@ -const cardNumber = 4533787178994213; +const cardNumber = "4533787178994213"; const last4Digits = cardNumber.slice(-4); +console.log("Last 4 digits : ${last4Digits}"); + +/*1.The slice method is typically used with strings to extract a portion of the string. +to extract the last 4 digits of the card number, we must first convert it to a string and then use the slice method.So we put the number in string */ + // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working // Make and explain a prediction about why the code won't work diff --git a/week-1/errors/4.js b/week-1/errors/4.js index 21dad8c5..bcbb2873 100644 --- a/week-1/errors/4.js +++ b/week-1/errors/4.js @@ -1,2 +1,4 @@ const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const 24hourClockTime = "08:53"; +//variable names in JavaScript cannot start with a digit. They must start with a letter, underscore (_), or dollar sign ($). + From 94abab1a53a9e02a158fd445f45f96261971d574 Mon Sep 17 00:00:00 2001 From: zeliha pala Date: Fri, 10 Nov 2023 16:21:40 +0000 Subject: [PATCH 4/8] All done --- week-1/exercises/count.js | 3 +++ week-1/exercises/decimal.js | 9 ++++++++- week-1/exercises/initials.js | 9 ++++++++- week-1/exercises/paths.js | 8 ++++++-- week-1/exercises/random.js | 2 ++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/week-1/exercises/count.js b/week-1/exercises/count.js index 117bcb2b..9e05d2b4 100644 --- a/week-1/exercises/count.js +++ b/week-1/exercises/count.js @@ -1,6 +1,9 @@ let count = 0; count = count + 1; +console.log(count); // 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 + +// The assignment sign is used to assign a new value to a variable. Here the count variable will have a new value of 1, which means it has been incremented by 1. diff --git a/week-1/exercises/decimal.js b/week-1/exercises/decimal.js index bd4a4740..1be65152 100644 --- a/week-1/exercises/decimal.js +++ b/week-1/exercises/decimal.js @@ -1,4 +1,3 @@ - const num = 56.5467; // You should look up Math functions for this exercise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math @@ -8,3 +7,11 @@ const num = 56.5467; // Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number ) // Log your variables to the console to check your answers + +const wholeNumberPart = Math.floor(num); // Extract the whole number part 56 +const decimalPart = num - wholeNumberPart; // Calculate the decimal part; num is 56.5467 - wholeNum is 56 so 0,5467 is decimal +const roundedNum = Math.round(num); // Round to the nearest whole number is 56,5 and will be 57 + +console.log("Whole Number Part:", wholeNumberPart); +console.log("Decimal Part:", decimalPart); +console.log("Rounded Number:", roundedNum); diff --git a/week-1/exercises/initials.js b/week-1/exercises/initials.js index 50b62103..37d12f81 100644 --- a/week-1/exercises/initials.js +++ b/week-1/exercises/initials.js @@ -3,4 +3,11 @@ let middleName = "Katherine"; let lastName = "Johnson"; // Declare a variable called initials that stores the first character of each string in upper case to form the user's initials -// Log the variable in each case +// Log the variable in each case. +//Here we use the charAt(0) method with the toUpperCase() method. (the first character of a string in JavaScript starts with 0) +let initials = + firstName.charAt(0).toUpperCase() + + middleName.charAt(0).toUpperCase() + + lastName.charAt(0).toUpperCase(); + +console.log("User:", initials); diff --git a/week-1/exercises/paths.js b/week-1/exercises/paths.js index c91cd2ab..153ec0ca 100644 --- a/week-1/exercises/paths.js +++ b/week-1/exercises/paths.js @@ -12,7 +12,11 @@ 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 = filePath.slice(filePath.indexOf("/"), filePath.lastIndexOf("/")); +console.log(dir); + +// Create a variable to store the ext part of the variable .txt file.txt +const ext = filePath.slice(filePath.lastIndexOf(".")); +console.log(ext); diff --git a/week-1/exercises/random.js b/week-1/exercises/random.js index 79a4a4d5..eb1ba31b 100644 --- a/week-1/exercises/random.js +++ b/week-1/exercises/random.js @@ -3,6 +3,8 @@ 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 From 609ca15132b93c5e47c1caf6cacd6d7c7b578ce1 Mon Sep 17 00:00:00 2001 From: zeliha pala Date: Fri, 10 Nov 2023 23:42:10 +0000 Subject: [PATCH 5/8] week1 all done --- week-1/explore/objects.md | 15 +++++++++++++-- week-1/interpret/percentage-change.js | 8 +++++++- week-1/interpret/time-format.js | 27 +++++++++++++++++++++++++-- week-1/interpret/to-pounds.js | 7 ++++--- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/week-1/explore/objects.md b/week-1/explore/objects.md index 0216dee5..5a35dc06 100644 --- a/week-1/explore/objects.md +++ b/week-1/explore/objects.md @@ -2,15 +2,26 @@ 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 + ƒ log() { [native code] } What output do you get? 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? -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 JavaScript, the console is like a toolbox for developers. It helps them see messages, warnings, and errors while building and fixing things on a website. It's a way to peek behind the scenes and understand what's happening in their code. console.log is like a sticky note to jot down messages for themselves, and console.assert is a tool to shout out if something isn't right. + +In particular, what does the `.` mean? + +The . (dot) in JavaScript is called the dot notation and is used to access properties and methods of objects. diff --git a/week-1/interpret/percentage-change.js b/week-1/interpret/percentage-change.js index 49b0ac15..2d6e801f 100644 --- a/week-1/interpret/percentage-change.js +++ b/week-1/interpret/percentage-change.js @@ -1,7 +1,7 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; -carPrice = Number(carPrice.replaceAll(",", "")); +carPrice = Number(carPrice.replaceAll(",", "")); // converts to number takes the string quato priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); const priceDifference = carPrice - priceAfterOneYear; @@ -12,9 +12,15 @@ 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 +//Number is a function. replaceAll is a method // b) Identify all the lines that are variable reassignment statements +//carPrice = Number(carPrice.replaceAll(",", "")); // converts to number takes the string quato +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); +//the values of the variables carPrice and priceAfterOneYear has bee changed with removing commas and converting them to numbers // c) Identify all the lines that are variable declarations +//let keyword is used to declare two variables, carPrice and priceAfterOneYear, and initialize them with string values. // d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +//replaceAll turns into an empty character with .replaceAll, and it turns into a number with number diff --git a/week-1/interpret/time-format.js b/week-1/interpret/time-format.js index 7961fe0d..9d4b0158 100644 --- a/week-1/interpret/time-format.js +++ b/week-1/interpret/time-format.js @@ -13,17 +13,40 @@ 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? +// a) How many variable declarations are there in this program? 6 +/* +const movieLength = 8784; +const remainingSeconds = movieLength % 60; +const totalMinutes = (movieLength - remainingSeconds) / 60; +const remainingMinutes = totalMinutes % 60; +const totalHours = (totalMinutes - remainingMinutes) / 60; +const remainingHours = totalHours % 24; +*/ -// b) How many function calls are there? +// b) How many function calls are there? 1 +//console.log(result); // c) Using documentation on MDN, explain what the expression movieLength % 60 represents +//This is modulo operation, and it returns the remainder of the division. Here it says the variable movieLength, is divided by 60 and The % modulo operator, returns the remainder of the division operation. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +/*to find the total minutes of the movie; + we Subtract the remaining seconds (remainingSeconds) from the total seconds (movieLength) + and we Take the result and divide it by 60. + The final result is the total number of minutes in the movie, stored in the variable totalMinutes."*/ + // e) What do you think the variable result represents? Can you think of a better name for this variable? +/* It expresses the length of the movie in terms of hours, minutes, and seconds. +The format is HH:MM:SS, where HH is the remaining hours, MM is the remaining minutes, and SS is the remaining seconds. +My suggestion for result variable is : +const movieDurationDisplay */ // f) Think about whether this program will work for all values of movieLength. +//The program correctly converts the total seconds into a formatted string representing hours, minutes, and seconds.It should work correctly for any positive integer value of movieLength. + // Think of what values may cause problems for the code. + // Decide the result should be for those values, then test it out. + // Can you find any values of movieLength which don't give you what you'd expect? diff --git a/week-1/interpret/to-pounds.js b/week-1/interpret/to-pounds.js index 196be3b2..5214b4cb 100644 --- a/week-1/interpret/to-pounds.js +++ b/week-1/interpret/to-pounds.js @@ -2,18 +2,19 @@ const penceString = "399p"; const penceStringWithoutTrailingP = penceString.substring( 0, - penceString.length - 1 + penceString.length - 1 // takes out the p with -1 ); -const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // padStarts add a zero infront of the number const pounds = paddedPenceNumberString.substring( 0, - paddedPenceNumberString.length - 2 + paddedPenceNumberString.length - 2 // last two numbers ignored we have 03 ); const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); +//This method is used to pad a string with a specified character at the beginning until it reaches a certain length. While doing this, the original string remains unchanged; a new string is returned.The first parameter of the padStart method specifies the target length, and the second parameter specifies the character to be added during the padding. console.log(`£${pounds}.${pence}`); From a3530e9c694803ffbf30f6a1a7faf4a6d27c2101 Mon Sep 17 00:00:00 2001 From: zeliha pala Date: Sun, 12 Nov 2023 15:51:26 +0000 Subject: [PATCH 6/8] corrected the codes after rewiev --- week-1/errors/3.js | 12 +++++++-- week-1/errors/4.js | 5 +++- week-1/exercises/decimal.js | 5 ++++ week-1/exercises/initials.js | 5 +--- week-1/exercises/paths.js | 2 +- week-1/exercises/random.js | 7 ++++- week-1/explore/chrome.md | 4 +-- week-1/explore/objects.md | 11 ++++++++ week-1/interpret/percentage-change.js | 28 ++++++++++++------- week-1/interpret/time-format.js | 17 +++++++----- week-1/interpret/to-pounds.js | 39 ++++++++++++++++++++------- 11 files changed, 98 insertions(+), 37 deletions(-) diff --git a/week-1/errors/3.js b/week-1/errors/3.js index de3559bf..a9121e6d 100644 --- a/week-1/errors/3.js +++ b/week-1/errors/3.js @@ -1,12 +1,20 @@ -const cardNumber = "4533787178994213"; +/*const cardNumber = "4533787178994213"; const last4Digits = cardNumber.slice(-4); -console.log("Last 4 digits : ${last4Digits}"); +console.log(" 4 digits of cardNumber: " + last4Digits); */ + +const cardNumber = "4533787178994213"; +const last4Digits = cardNumber.slice(-4); // I wanted to test if it works without concatenating and it works + +console.log("4 digits cardNumber:", last4Digits); // here i want to see if without concatenate /*1.The slice method is typically used with strings to extract a portion of the string. to extract the last 4 digits of the card number, we must first convert it to a string and then use the slice method.So we put the number in string */ // The last4Digits variable should store the last 4 digits of cardNumber + // However, the code isn't working + // Make and explain a prediction about why the code won't work + // Then try updating the expression last4Digits is assigned to, in order to get the correct value diff --git a/week-1/errors/4.js b/week-1/errors/4.js index bcbb2873..08e844aa 100644 --- a/week-1/errors/4.js +++ b/week-1/errors/4.js @@ -1,4 +1,7 @@ const 12HourClockTime = "20:53"; const 24hourClockTime = "08:53"; -//variable names in JavaScript cannot start with a digit. They must start with a letter, underscore (_), or dollar sign ($). +//variable names in JavaScript cannot start with a digit. They must start with a letter, underscore (_), or dollar sign ($). +//I do not know what to do here +const twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "08:53"; \ No newline at end of file diff --git a/week-1/exercises/decimal.js b/week-1/exercises/decimal.js index 1be65152..3a746d66 100644 --- a/week-1/exercises/decimal.js +++ b/week-1/exercises/decimal.js @@ -15,3 +15,8 @@ const roundedNum = Math.round(num); // Round to the nearest whole number is 56,5 console.log("Whole Number Part:", wholeNumberPart); console.log("Decimal Part:", decimalPart); console.log("Rounded Number:", roundedNum); + +/* a) The Math.floor function is used to round down a decimal number to the nearest integer that is less than or equal to the original number. +If Math.floor(-8.4), the result will be -9. This is because -9 is the closest integer that is less than or equal to -8.4. The function essentially rounds down to the nearest whole number.*/ + +/* b) If Math.floor(8.4), the result will be 8. The Math.floor function rounds down the given decimal number to the nearest integer that is less than or equal to the original number. In this case, 8 is the closest integer that satisfies this condition for the decimal 8.4. */ diff --git a/week-1/exercises/initials.js b/week-1/exercises/initials.js index 37d12f81..28e8b325 100644 --- a/week-1/exercises/initials.js +++ b/week-1/exercises/initials.js @@ -5,9 +5,6 @@ let lastName = "Johnson"; // Declare a variable called initials that stores the first character of each string in upper case to form the user's initials // Log the variable in each case. //Here we use the charAt(0) method with the toUpperCase() method. (the first character of a string in JavaScript starts with 0) -let initials = - firstName.charAt(0).toUpperCase() + - middleName.charAt(0).toUpperCase() + - lastName.charAt(0).toUpperCase(); +let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); console.log("User:", initials); diff --git a/week-1/exercises/paths.js b/week-1/exercises/paths.js index 153ec0ca..7759a0ef 100644 --- a/week-1/exercises/paths.js +++ b/week-1/exercises/paths.js @@ -14,7 +14,7 @@ const lastSlashIndex = filePath.lastIndexOf("/"); const base = filePath.slice(lastSlashIndex + 1); // Create a variable to store the dir part of the filePath variable -const dir = filePath.slice(filePath.indexOf("/"), filePath.lastIndexOf("/")); +const dir = filePath.slice(filePath.indexOf("/"), lastSlashIndex); console.log(dir); // Create a variable to store the ext part of the variable .txt file.txt diff --git a/week-1/exercises/random.js b/week-1/exercises/random.js index eb1ba31b..80bfb0aa 100644 --- a/week-1/exercises/random.js +++ b/week-1/exercises/random.js @@ -2,8 +2,13 @@ const minimum = 1; const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +//Math.random() function returns a floating-point number between 0 (inclusive) and 1 (exclusive). It's essentially a random decimal number. like spinning a wheel with numbers between 0 and 1. It gives us a random decimal, like 0.567 or 0.874. -console.log(num); +//maximum - minimum + 1: This part helps decide how big your range on the number line should be. If you want numbers from 1 to 100, the range is 100 - 1 + 1, which is 100. +// Math.floor(...): This is like rounding down to the nearest whole number. So, if you got 56.78, you round down to 56. +//+ minimum: Finally, you add the starting point of your range. If the minimum is 1, you add 1 to the rounded-down number. So, if you got 56 earlier, you add 1 and get 57. + +console.log(num); // Here we get random numbers each time // 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 diff --git a/week-1/explore/chrome.md b/week-1/explore/chrome.md index e7dd5fea..11b4057f 100644 --- a/week-1/explore/chrome.md +++ b/week-1/explore/chrome.md @@ -8,11 +8,11 @@ Just like the Node REPL, you can input JavaScript code into the Console tab and Let's try an example. In the Chrome console, -invoke the function `alert` with an input string of `"Hello world!"`; +invoke the function `alert` with an input string of `"Hello world!"`; alert("Hello world!"`;) What effect does calling the `alert` function have? -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`. +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?");*/ What effect does calling the `prompt` function have? What is the return value of `prompt`? diff --git a/week-1/explore/objects.md b/week-1/explore/objects.md index 5a35dc06..a11beae8 100644 --- a/week-1/explore/objects.md +++ b/week-1/explore/objects.md @@ -22,6 +22,17 @@ What does `console` store? What does the syntax `console.log` or `console.assert` mean? In JavaScript, the console is like a toolbox for developers. It helps them see messages, warnings, and errors while building and fixing things on a website. It's a way to peek behind the scenes and understand what's happening in their code. console.log is like a sticky note to jot down messages for themselves, and console.assert is a tool to shout out if something isn't right. +both console.log and console.assert provide ways to output information to the console, but they serve different purposes. +console.log is for general logging and debugging, +while console.assert is used to check conditions and log an error if the condition is not met. + +examples +let myAge = 42; +console.log("I am :", myAge); + +let myAge = 42; +console.assert(myAge >= 18, "Eligible"); + In particular, what does the `.` mean? The . (dot) in JavaScript is called the dot notation and is used to access properties and methods of objects. diff --git a/week-1/interpret/percentage-change.js b/week-1/interpret/percentage-change.js index 2d6e801f..d0f2b128 100644 --- a/week-1/interpret/percentage-change.js +++ b/week-1/interpret/percentage-change.js @@ -11,16 +11,24 @@ 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 -//Number is a function. replaceAll is a method +/* a) How many function calls are there in this file? Write down all the lines where a function call is made + Number is a function. replaceAll is a method -// b) Identify all the lines that are variable reassignment statements -//carPrice = Number(carPrice.replaceAll(",", "")); // converts to number takes the string quato -//priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); -//the values of the variables carPrice and priceAfterOneYear has bee changed with removing commas and converting them to numbers + how many function calls would you say are in this file? + carPrice.replaceAll(",", ""): This function call is used to replace commas in the string representation of carPrice with an empty string. -// c) Identify all the lines that are variable declarations -//let keyword is used to declare two variables, carPrice and priceAfterOneYear, and initialize them with string values. + priceAfterOneYear.replaceAll(",", ""): This function call is used to replace commas in the string representation of priceAfterOneYear with an empty string.*/ -// d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? -//replaceAll turns into an empty character with .replaceAll, and it turns into a number with number +/* b) Identify all the lines that are variable reassignment statements + carPrice = Number(carPrice.replaceAll(",", "")); // converts to number takes the string quato + priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); + the values of the variables carPrice and priceAfterOneYear has bee changed with removing commas and converting them to numbers */ + +/* c) Identify all the lines that are variable declarations + let keyword is used to declare two variables, carPrice and priceAfterOneYear, and initialize them with string values. + priceDifference and percentageChange, using the const keyword. The const keyword is used to create constants in JavaScript, meaning that the value of these variables cannot be reassigned once they are initially assigned. If we attempt to reassign a value to a variable declared with const, it will result in a syntax error.*/ + +/* d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + ReplaceAll turns into an empty character with .replaceAll, and it turns into a number with number. */ + +// e) const keyword is used to declare constants. A constant is a variable whose value cannot be changed or reassigned after it has been initially assigned. If you attempt to reassign a value to a variable declared with const, it will result in a syntax error. diff --git a/week-1/interpret/time-format.js b/week-1/interpret/time-format.js index 9d4b0158..24ac8a5e 100644 --- a/week-1/interpret/time-format.js +++ b/week-1/interpret/time-format.js @@ -1,14 +1,14 @@ const movieLength = 8784; // length of movie in seconds -const remainingSeconds = movieLength % 60; -const totalMinutes = (movieLength - remainingSeconds) / 60; +const remainingSeconds = movieLength % 60; // This line calculates the remaining seconds by taking the modulus (%) of movieLength divided by 60. +const totalMinutes = (movieLength - remainingSeconds) / 60; //This line calculates the total minutes by subtracting remainingSeconds from movieLength and then dividing the result by 60. -const remainingMinutes = totalMinutes % 60; -const totalHours = (totalMinutes - remainingMinutes) / 60; +const remainingMinutes = totalMinutes % 60; //This line calculates the remaining minutes by taking the modulus of totalMinutes divided by 60. +const totalHours = (totalMinutes - remainingMinutes) / 60; //This line calculates the total hours by subtracting remainingMinutes from totalMinutes and then dividing the result by 60. -const remainingHours = totalHours % 24; +const remainingHours = totalHours % 24; //This line calculates the remaining hours by taking the modulus of totalHours divided by 24. -const result = `${remainingHours}:${remainingMinutes}:${remainingSeconds}`; +const result = `${remainingHours}:${remainingMinutes}:${remainingSeconds}`; // time format "HH:MM:SS". console.log(result); // For the piece of code above, read the code and then answer the following questions @@ -46,7 +46,10 @@ const movieDurationDisplay */ //The program correctly converts the total seconds into a formatted string representing hours, minutes, and seconds.It should work correctly for any positive integer value of movieLength. // Think of what values may cause problems for the code. +// If the movieLength is negative or if movieLength is a floating-point number, or If movieLength is 0, we may have problems -// Decide the result should be for those values, then test it out. +// Decide the result should be for those values, then test it out. ...... // Can you find any values of movieLength which don't give you what you'd expect? +const movieLength = "not a number"; +const movieLength = 3661.5; diff --git a/week-1/interpret/to-pounds.js b/week-1/interpret/to-pounds.js index 5214b4cb..469e881e 100644 --- a/week-1/interpret/to-pounds.js +++ b/week-1/interpret/to-pounds.js @@ -1,21 +1,16 @@ const penceString = "399p"; - const penceStringWithoutTrailingP = penceString.substring( 0, - penceString.length - 1 // takes out the p with -1 + penceString.length - 1 ); - -const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // padStarts add a zero infront of the number +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); const pounds = paddedPenceNumberString.substring( 0, - paddedPenceNumberString.length - 2 // last two numbers ignored we have 03 + paddedPenceNumberString.length - 2 ); - const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); -//This method is used to pad a string with a specified character at the beginning until it reaches a certain length. While doing this, the original string remains unchanged; a new string is returned.The first parameter of the padStart method specifies the target length, and the second parameter specifies the character to be added during the padding. - console.log(`£${pounds}.${pence}`); // This program takes a string representing a price in pence @@ -27,4 +22,30 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with -// 1. const penceString = "399p": initialises a string variable with the value "399p" +/* 1. const penceString = "399p": initialises a string variable with the value "399p" + + 2.const penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1 ); penceStringWithoutTrailingP takes a substring of penceString starting from index 0 and ending at the length of penceString minus 1. This effectively removes the last character "p" from penceString. + if penceString is "399p", then penceStringWithoutTrailingP would be "399" because it removes the trailing "p" from the original string. The purpose of this operation seems to be to get the numeric part of the string without the currency symbol ("p" in this case). + + +3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + +The padStart method is used to ensure that paddedPenceNumberString is always three characters long. If the length is less than 3, "0" is padded to the start of the string. + + +4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2 ); +pounds is assigned the substring of paddedPenceNumberString starting from index 0 and ending two characters before the length of paddedPenceNumberString. This extracts the part of the string representing pounds. + + +5. const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); + +pence is assigned the last two characters of paddedPenceNumberString, and if the length is less than 2, "0" is padded to the end of the string. + + + + +console.log(`£${pounds}.${pence}`); £3.99 */ + +//The padStart method is used to pad the start of a string with a specified character (or a specified string) until the resulting string reaches a given length. Here, we are using padStart to ensure that paddedPenceNumberString is at least 3 characters long. If the length of penceStringWithoutTrailingP is less than 3 characters, it will pad the start of the string with zeros until it reaches a length of 3.This method is used to pad a string with a specified character at the beginning until it reaches a certain length. While doing this, the original string remains unchanged; a new string is returned.The first parameter of the padStart method specifies the target length, and the second parameter specifies the character to be added during the padding. + +// Since the length of penceStringWithoutTrailingP is already 3, padStart does not add any zeros, and paddedPenceNumberString remains "399".So, in this case, no zeros are added in front of the number because the length requirement is already met. If the length was less than 3, it would pad with zeros accordingly. From a736fb1e16259f403ffefd5abc788431b0fb1248 Mon Sep 17 00:00:00 2001 From: zeliha pala Date: Sun, 3 Dec 2023 01:40:01 +0000 Subject: [PATCH 7/8] Revision --- week-1/exercises/count.js | 4 ++-- week-1/exercises/paths.js | 23 ++++++++++++++++++++++- week-1/explore/chrome.md | 6 ++++-- week-1/explore/objects.md | 11 ++++++++--- week-1/interpret/to-pounds.js | 11 ++++++----- week-4/implement/card-validator.md | 1 + 6 files changed, 43 insertions(+), 13 deletions(-) diff --git a/week-1/exercises/count.js b/week-1/exercises/count.js index 9e05d2b4..b2e39838 100644 --- a/week-1/exercises/count.js +++ b/week-1/exercises/count.js @@ -1,9 +1,9 @@ let count = 0; -count = count + 1; +count = count + 3; console.log(count); // 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 -// The assignment sign is used to assign a new value to a variable. Here the count variable will have a new value of 1, which means it has been incremented by 1. +// The assignment sign is used to assign a new value to a variable. Here the count variable will have a new value of 1, which means it has been incremented by diff --git a/week-1/exercises/paths.js b/week-1/exercises/paths.js index 7759a0ef..9cd00459 100644 --- a/week-1/exercises/paths.js +++ b/week-1/exercises/paths.js @@ -1,5 +1,7 @@ // The diagram below shows the different names for parts of a file path on a Unix operating system +const { example } = require("yargs"); + // ┌─────────────────────┬────────────┐ // │ dir │ base │ // ├──────┬ ├──────┬─────┤ @@ -9,7 +11,7 @@ // (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 filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; const lastSlashIndex = filePath.lastIndexOf("/"); const base = filePath.slice(lastSlashIndex + 1); @@ -20,3 +22,22 @@ console.log(dir); // Create a variable to store the ext part of the variable .txt file.txt const ext = filePath.slice(filePath.lastIndexOf(".")); console.log(ext); +console.log(base); +console.log(lastSlashIndex); */ + +//my example +const filePath = "/user/zeliha/cyf/Module-JS-1/week-1"; +// Extracting the directory +const directoryIndex = filePath.lastIndexOf("/"); +const directory = filePath.slice(0, directoryIndex); +console.log("Directory:", directory); + +// Extracting the file name +const fileNameIndex = filePath.lastIndexOf("/") + 1; +const fileName = filePath.slice(fileNameIndex); +console.log("File Name:", fileName); + +// Extracting the file extension +const extensionIndex = fileName.lastIndexOf(".") + 1; +const extension = fileName.slice(extensionIndex); +console.log("Extension:", extension); diff --git a/week-1/explore/chrome.md b/week-1/explore/chrome.md index 11b4057f..4ce60e0d 100644 --- a/week-1/explore/chrome.md +++ b/week-1/explore/chrome.md @@ -8,11 +8,13 @@ Just like the Node REPL, you can input JavaScript code into the Console tab and Let's try an example. In the Chrome console, -invoke the function `alert` with an input string of `"Hello world!"`; alert("Hello world!"`;) +invoke the function `alert` with an input string of `"Hello world!"`; alert("Hello world!"`;) What effect does calling the `alert` function have? -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?");*/ +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?");_/ What effect does calling the `prompt` function have? What is the return value of `prompt`? + +// The return value of the prompt function is the text entered by the user. If the user clicks the 'OK' button after entering text, the text they entered is returned as a string. If the user clicks 'Cancel' or closes the dialog, the function returns null. diff --git a/week-1/explore/objects.md b/week-1/explore/objects.md index a11beae8..6b241b42 100644 --- a/week-1/explore/objects.md +++ b/week-1/explore/objects.md @@ -20,17 +20,22 @@ Answer the following questions: What does `console` store? What does the syntax `console.log` or `console.assert` mean? -In JavaScript, the console is like a toolbox for developers. It helps them see messages, warnings, and errors while building and fixing things on a website. It's a way to peek behind the scenes and understand what's happening in their code. console.log is like a sticky note to jot down messages for themselves, and console.assert is a tool to shout out if something isn't right. +In JavaScript, the console is like a toolbox for developers. +It helps them see messages, warnings, and errors while building and fixing things on a website. +It's a way to peek behind the scenes and understand what's happening in their code. + +console.log is like a sticky note to jot down messages for themselves, +and console.assert is a tool to shout out if something isn't right. both console.log and console.assert provide ways to output information to the console, but they serve different purposes. console.log is for general logging and debugging, while console.assert is used to check conditions and log an error if the condition is not met. examples -let myAge = 42; +let myAge = 17; console.log("I am :", myAge); -let myAge = 42; +let myAge = 17; console.assert(myAge >= 18, "Eligible"); In particular, what does the `.` mean? diff --git a/week-1/interpret/to-pounds.js b/week-1/interpret/to-pounds.js index 469e881e..2b5cf06c 100644 --- a/week-1/interpret/to-pounds.js +++ b/week-1/interpret/to-pounds.js @@ -2,16 +2,17 @@ const penceString = "399p"; const penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1 -); -const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +); // clears p +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // 339 no need to 0 const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2 -); +); // it takes out last 2 characters so we only have 3 const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); -console.log(`£${pounds}.${pence}`); + .padEnd(2, "0"); // make it 2 characters long and add 0 but no need it's already 2 character and still 99 + +console.log(`£${pounds}.${pence}`); // 3,99 // This program takes a string representing a price in pence // The program then builds up a string representing the price in pounds diff --git a/week-4/implement/card-validator.md b/week-4/implement/card-validator.md index e39c6ace..7faff16f 100644 --- a/week-4/implement/card-validator.md +++ b/week-4/implement/card-validator.md @@ -5,6 +5,7 @@ In this project you'll write a script that validates whether or not a credit car Here are the rules for a valid number: - Number must be 16 digits, all of them must be numbers. + - You must have at least two different digits represented (all of the digits cannot be the same). - The final digit must be even. - The sum of all the digits must be greater than 16. From 16fe0b3e32b172e764a951e27bf367848a97acbe Mon Sep 17 00:00:00 2001 From: zeliha pala Date: Sun, 3 Dec 2023 01:41:42 +0000 Subject: [PATCH 8/8] updated --- week-1/explore/objects.md | 2 ++ week-1/interpret/percentage-change.js | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/week-1/explore/objects.md b/week-1/explore/objects.md index 6b241b42..d9d0624f 100644 --- a/week-1/explore/objects.md +++ b/week-1/explore/objects.md @@ -38,6 +38,8 @@ console.log("I am :", myAge); let myAge = 17; console.assert(myAge >= 18, "Eligible"); +// Assertion failed: Eligible + In particular, what does the `.` mean? The . (dot) in JavaScript is called the dot notation and is used to access properties and methods of objects. diff --git a/week-1/interpret/percentage-change.js b/week-1/interpret/percentage-change.js index d0f2b128..d7798bfd 100644 --- a/week-1/interpret/percentage-change.js +++ b/week-1/interpret/percentage-change.js @@ -1,7 +1,7 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; -carPrice = Number(carPrice.replaceAll(",", "")); // converts to number takes the string quato +carPrice = Number(carPrice.replaceAll(",", "")); // converts to number takes the string quota priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); const priceDifference = carPrice - priceAfterOneYear; @@ -12,7 +12,10 @@ 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 - Number is a function. replaceAll is a method + There are two distinct functions being used: replaceAll() and Number(). +The replaceAll() function is called twice, once for carPrice and once for priceAfterOneYear. +The Number() function is also called twice, once for converting carPrice to a number and once for priceAfterOneYear. +Therefore, there are two different functions used, but a total of four function calls (two calls to replaceAll and two calls to Number). how many function calls would you say are in this file? carPrice.replaceAll(",", ""): This function call is used to replace commas in the string representation of carPrice with an empty string.