From 3edb6aa6de0d40498a0e836ef2b9b42a1bc2a803 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Wed, 13 Nov 2024 12:51:09 +0000 Subject: [PATCH 01/15] solution 0.js --- Sprint-1/errors/0.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/errors/0.js b/Sprint-1/errors/0.js index cf6c5039..e6b744a0 100644 --- a/Sprint-1/errors/0.js +++ b/Sprint-1/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? From 672cf119ffcf5e59b1ed4b7f0440240e172dafc6 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Wed, 13 Nov 2024 13:28:52 +0000 Subject: [PATCH 02/15] solution 1.js --- Sprint-1/errors/1.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-1/errors/1.js b/Sprint-1/errors/1.js index 7a43cbea..e57b2e97 100644 --- a/Sprint-1/errors/1.js +++ b/Sprint-1/errors/1.js @@ -1,4 +1,5 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; +console.log(age); From 54b0fa24a72308f5ee695f110c4a3236b15346e5 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Wed, 13 Nov 2024 13:29:46 +0000 Subject: [PATCH 03/15] solution 2.js --- Sprint-1/errors/2.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/errors/2.js b/Sprint-1/errors/2.js index e09b8983..c46e9808 100644 --- a/Sprint-1/errors/2.js +++ b/Sprint-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 ? - -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + From c3997abffe276478c4d7643f255d3d3c22595aba Mon Sep 17 00:00:00 2001 From: fatima safana Date: Wed, 13 Nov 2024 14:26:30 +0000 Subject: [PATCH 04/15] solution 3.js --- Sprint-1/errors/3.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/errors/3.js b/Sprint-1/errors/3.js index ec101884..eb1991b3 100644 --- a/Sprint-1/errors/3.js +++ b/Sprint-1/errors/3.js @@ -1,5 +1,6 @@ -const cardNumber = 4533787178994213; +const cardNumber = "4533787178994213"; const last4Digits = cardNumber.slice(-4); +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +8,6 @@ const last4Digits = cardNumber.slice(-4); // 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 +//Answers: +/*The code should print out 4213, because .slice() is a string type function, thus +value needs to be "string" */ \ No newline at end of file From 42eb38cf561ec212a5a29b4fc506cc89aa8c3eaf Mon Sep 17 00:00:00 2001 From: fatima safana Date: Wed, 13 Nov 2024 14:30:50 +0000 Subject: [PATCH 05/15] solution 4.js --- Sprint-1/errors/4.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-1/errors/4.js b/Sprint-1/errors/4.js index 21dad8c5..4b3edd71 100644 --- a/Sprint-1/errors/4.js +++ b/Sprint-1/errors/4.js @@ -1,2 +1,3 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "08:53"; +//variable names cannot start with numbers. \ No newline at end of file From 8b3d0658eba74e53e1ca544e96b4236733692cd9 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Wed, 13 Nov 2024 15:01:57 +0000 Subject: [PATCH 06/15] solution to count.js --- Sprint-1/exercises/count.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-1/exercises/count.js b/Sprint-1/exercises/count.js index 117bcb2b..7f2bf99d 100644 --- a/Sprint-1/exercises/count.js +++ b/Sprint-1/exercises/count.js @@ -4,3 +4,4 @@ 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 +// = is re-assigning the value of count to be count plus 1. From 4c77ac5c79bd1a484e8eb747bb38e2cdaf802466 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Wed, 13 Nov 2024 15:43:25 +0000 Subject: [PATCH 07/15] solution decimal.js --- Sprint-1/exercises/decimal.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sprint-1/exercises/decimal.js b/Sprint-1/exercises/decimal.js index cc5947ce..fb1f5cd6 100644 --- a/Sprint-1/exercises/decimal.js +++ b/Sprint-1/exercises/decimal.js @@ -7,3 +7,13 @@ 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 + +let wholeNumberPart = Math.round(num); + console.log(wholeNumberPart) + +let decimalPart = num - Math.floor(num); + console.log(decimalPart); + +let roundedNum = Math.round(num); + console.log(roundedNum); + From 82d28fb2e873b815c64bce2b4e48e30df8ba44a5 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Wed, 13 Nov 2024 15:51:14 +0000 Subject: [PATCH 08/15] solution initials.js --- Sprint-1/exercises/initials.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/exercises/initials.js b/Sprint-1/exercises/initials.js index 6b80cd13..7322679e 100644 --- a/Sprint-1/exercises/initials.js +++ b/Sprint-1/exercises/initials.js @@ -4,3 +4,7 @@ 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[0] + middleName[0] + lastName[0]; + +console.log(initials); From 8903a5e47002e404b118ae779781e6adea7e4356 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Wed, 13 Nov 2024 16:32:22 +0000 Subject: [PATCH 09/15] solution path.js --- Sprint-1/exercises/paths.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sprint-1/exercises/paths.js b/Sprint-1/exercises/paths.js index c91cd2ab..d94dbff5 100644 --- a/Sprint-1/exercises/paths.js +++ b/Sprint-1/exercises/paths.js @@ -11,8 +11,16 @@ const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; const lastSlashIndex = filePath.lastIndexOf("/"); + 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 dirPart = filePath.substring(45, -1); + console.log(`The directory path of ${dirPart}`); + +const extPart = dirPart + "ext.txt"; + console.log(`The ext path is ${extPart}`); \ No newline at end of file From c112a9a0da4faff9c9fbc4fc6dd4b07b173c9c12 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Wed, 13 Nov 2024 16:46:30 +0000 Subject: [PATCH 10/15] solutions random.js --- Sprint-1/exercises/random.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/exercises/random.js b/Sprint-1/exercises/random.js index 292f83aa..8ddf31af 100644 --- a/Sprint-1/exercises/random.js +++ b/Sprint-1/exercises/random.js @@ -7,3 +7,7 @@ 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); + +// The program creates a random whole number between 1 and 100, max and min inclusive. \ No newline at end of file From 841618bdb277b6c78b0ece7eb7c81d4024c5efa7 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Wed, 13 Nov 2024 21:46:29 +0000 Subject: [PATCH 11/15] solution percentage-change.js --- Sprint-1/interpret/percentage-change.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sprint-1/interpret/percentage-change.js b/Sprint-1/interpret/percentage-change.js index e24ecb8e..eeac4edc 100644 --- a/Sprint-1/interpret/percentage-change.js +++ b/Sprint-1/interpret/percentage-change.js @@ -2,21 +2,28 @@ 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(carPrice) // 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. These are in line 4,5,10. // 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? + // the error was coming from line 5, there was no , between the two arguments. // c) Identify all the lines that are variable reassignment statements + // line 4 and 5. // d) Identify all the lines that are variable declarations + //line 1, 2, 7, 8 // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + //The purpose is to convert the string value to numerical value, so that it can be evaluated. + //The expression replaces , with nothing "" then converts to number. From e51addc09a1613d66473ac6f4f1d7348944f93d6 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Wed, 13 Nov 2024 22:08:32 +0000 Subject: [PATCH 12/15] solution time-format.js --- Sprint-1/interpret/time-format.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-1/interpret/time-format.js b/Sprint-1/interpret/time-format.js index 83232e43..326be7cf 100644 --- a/Sprint-1/interpret/time-format.js +++ b/Sprint-1/interpret/time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = 10000; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,13 +12,19 @@ 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 in this program. // b) How many function calls are there? + //1 // c) Using documentation, explain what the expression movieLength % 60 represents + // The expression utilizes the modulus operator % which returns the value reminder after dividing the movielenght by 60 // d) Interpret line 4, what does the expression assigned to totalMinutes mean? + //the expression calculate the total minutes by converting the remaining seconds to minutes. // e) What do you think the variable result represents? Can you think of a better name for this variable? + // movie duration // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + // This code will work for all values. From 6635aa3152931b2e47af15e0bab7e0bcfb8676d6 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Fri, 15 Nov 2024 06:28:54 +0000 Subject: [PATCH 13/15] solutions to-pounds.js --- Sprint-1/interpret/to-pounds.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Sprint-1/interpret/to-pounds.js b/Sprint-1/interpret/to-pounds.js index 60c9ace6..f0050e3e 100644 --- a/Sprint-1/interpret/to-pounds.js +++ b/Sprint-1/interpret/to-pounds.js @@ -4,16 +4,21 @@ const penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1 ); + console.log(penceStringWithoutTrailingP); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "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}`); @@ -24,4 +29,16 @@ 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" +// 3. initializes a variable that removes the "p" at the end, it uses method .substring +// which returns the part of the string corresponding to the index and end index +//-1 excluding the p +//9. uses the method padstart to add "0" to the start of string penceStringWithoutTrailingP, +// it returns the same string unchanged because the target length is equal to the current length. +// this effectively multiplies any value input as penceStringWithoutTrailingP by 100 +//12. uses the substring method to return the first value of the string, this is to get +// the pound. it also used length -2 to make sure its in a valid range so the program +// can go through the steps with any value input. +//18.the code gets the last two digits from the paddedPenceNumberString and ensures that the result +//is a two-digit string by padding it with 0. +//23. Print the value of pounds and pence with a period in between using template literals. From e93889928bbb59792f8ee1b660078c867922480c Mon Sep 17 00:00:00 2001 From: fatima safana Date: Mon, 9 Dec 2024 20:00:20 +0000 Subject: [PATCH 14/15] implemented suggestion --- Sprint-1/errors/3.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-1/errors/3.js b/Sprint-1/errors/3.js index eb1991b3..89ebb87c 100644 --- a/Sprint-1/errors/3.js +++ b/Sprint-1/errors/3.js @@ -2,6 +2,12 @@ const cardNumber = "4533787178994213"; const last4Digits = cardNumber.slice(-4); console.log(last4Digits); +function last4(num) { + return String(num.slice(-4)); +} + +console.log(last4("12237890")); + // 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 From 4b79d647b63c26fb25789737b92f5aea77f44184 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Mon, 9 Dec 2024 20:06:12 +0000 Subject: [PATCH 15/15] adjusted method to output to fixed 4 decimal points --- Sprint-1/exercises/decimal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/exercises/decimal.js b/Sprint-1/exercises/decimal.js index fb1f5cd6..83fe4d45 100644 --- a/Sprint-1/exercises/decimal.js +++ b/Sprint-1/exercises/decimal.js @@ -11,7 +11,7 @@ const num = 56.5678; let wholeNumberPart = Math.round(num); console.log(wholeNumberPart) -let decimalPart = num - Math.floor(num); +let decimalPart = (num - Math.floor(num)).toFixed(4); console.log(decimalPart); let roundedNum = Math.round(num);