diff --git a/Sprint-1/errors/0.js b/Sprint-1/errors/0.js index cf6c5039f7..e66119fc13 100644 --- a/Sprint-1/errors/0.js +++ b/Sprint-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 +/*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? +because these sentences were not nested inside JS's comment syntax.*/ \ No newline at end of file diff --git a/Sprint-1/errors/1.js b/Sprint-1/errors/1.js index 7a43cbea76..48c7001a25 100644 --- a/Sprint-1/errors/1.js +++ b/Sprint-1/errors/1.js @@ -1,4 +1,6 @@ // trying to create an age variable and then reassign the value by 1 - -const age = 33; +/*when const is used to create a variable, it can not be reassigned. So for this practice, i replace +with let to fix this error.*/ +let age = 33; age = age + 1; +console.log(age); \ No newline at end of file diff --git a/Sprint-1/errors/2.js b/Sprint-1/errors/2.js index e09b89831d..53c6a0e72e 100644 --- a/Sprint-1/errors/2.js +++ b/Sprint-1/errors/2.js @@ -1,5 +1,9 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? +/*the answer is related to the order that the code was written. the variable declaration followed console.log +instead of preceding it resulting in a reference error. I cut the variable declaration and pasted it above +console.log to resolve the problem.*/ -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + diff --git a/Sprint-1/errors/3.js b/Sprint-1/errors/3.js index ec101884db..e24c1fc1e4 100644 --- a/Sprint-1/errors/3.js +++ b/Sprint-1/errors/3.js @@ -1,9 +1,12 @@ -const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const cardNumber = 4533787178994213; // const cardNumber = `${4533787178994213}` also fixes the error by changing data type to string. +const last4Digits = cardNumber.toString().slice(-4); +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 // 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? +/* the answer: slice() method does only work on strings and arrays. it does not work on numbers. +so I fixed it by changing the expression in Last4Digits variable. */ // Then try updating the expression last4Digits is assigned to, in order to get the correct value diff --git a/Sprint-1/errors/4.js b/Sprint-1/errors/4.js index 21dad8c5d1..a433849c4c 100644 --- a/Sprint-1/errors/4.js +++ b/Sprint-1/errors/4.js @@ -1,2 +1,5 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +let HourClockTime = "20:53"; +hourClockTime = "08:53"; +console.log(hourClockTime); // Output: 08:53 +/*Javascript variables can not be started with numbers. I changed const to let so that the variable can +be reassigned to a new value. */ \ No newline at end of file diff --git a/Sprint-1/exercises/count.js b/Sprint-1/exercises/count.js index 117bcb2b6e..0824a299a6 100644 --- a/Sprint-1/exercises/count.js +++ b/Sprint-1/exercises/count.js @@ -1,6 +1,8 @@ let count = 0; count = count + 1; +/*line 3 reassigns the value that was passed onto count variable by using an arithmetic operator to add +1 to count variable's value.*/ // 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 diff --git a/Sprint-1/exercises/decimal.js b/Sprint-1/exercises/decimal.js index cc5947ce2f..768fd4b663 100644 --- a/Sprint-1/exercises/decimal.js +++ b/Sprint-1/exercises/decimal.js @@ -1,5 +1,5 @@ const num = 56.5678; - +console.log(num); // You should look up Math functions for this exercise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math // Create a variable called wholeNumberPart and assign to it an expression that evaluates to 56 ( the whole number part of num ) @@ -7,3 +7,9 @@ const num = 56.5678; // 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); +console.log("wHole number is:"+ wholeNumberPart); +const decimalPart = num - wholeNumberPart; +console.log("decimal part is:"+ decimalPart); +const roundedNum = Math.round(num); +console.log("rounded number is:"+ roundedNum); \ No newline at end of file diff --git a/Sprint-1/exercises/initials.js b/Sprint-1/exercises/initials.js index 6b80cd1375..124f438363 100644 --- a/Sprint-1/exercises/initials.js +++ b/Sprint-1/exercises/initials.js @@ -2,5 +2,9 @@ let firstName = "Creola"; let middleName = "Katherine"; 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 = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); + +console.log("initials are:" + initials); \ No newline at end of file diff --git a/Sprint-1/exercises/paths.js b/Sprint-1/exercises/paths.js index c91cd2ab37..9bd17c947a 100644 --- a/Sprint-1/exercises/paths.js +++ b/Sprint-1/exercises/paths.js @@ -10,9 +10,21 @@ // (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"; +console.log(filePath); const lastSlashIndex = filePath.lastIndexOf("/"); -const base = filePath.slice(lastSlashIndex + 1); +console.log(lastSlashIndex); +const base = filePath.slice(lastSlashIndex+1); +console.log(base); 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(0,lastSlashIndex+1); +console.log(dir); +// Create a variable to store the ext part of the variables. +const lastDotIndex = filePath.lastIndexOf("."); +const ext = filePath.slice(lastDotIndex+1); +console.log(ext); + +//another way to extract ext part of filePath string is: +const ext2 = filePath.split(".").pop(); +console.log(ext2); \ No newline at end of file diff --git a/Sprint-1/exercises/random.js b/Sprint-1/exercises/random.js index 292f83aabb..d5cfad3339 100644 --- a/Sprint-1/exercises/random.js +++ b/Sprint-1/exercises/random.js @@ -2,8 +2,12 @@ const minimum = 1; const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +/*due to rules of precedence, parenthesis executes first. Inside parenthesis, subtraction executes first, +then addition executes.*/ +console.log("random number:" + num); +/*num keeps changing because Math.random() function outputs a different random value between 0 and 1 every time it runs. */ -// In this exercise, you will need to work out what num represents? +// In this exercise, you will need to work out what num represents? answer: a numerical value // 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