From 0c81f794b3614de9e4a9c1fd8eaa8fd16cb24cea Mon Sep 17 00:00:00 2001 From: RAHWA ZESLUS Date: Sun, 27 Oct 2024 19:57:01 +0000 Subject: [PATCH 1/4] sprint-1 java script exercises --- Sprint-1/errors/0.js | 5 ++++- Sprint-1/errors/1.js | 3 +++ Sprint-1/errors/2.js | 2 ++ Sprint-1/errors/3.js | 8 ++++++- Sprint-1/errors/4.js | 6 +++++- Sprint-1/exercises/count.js | 2 ++ Sprint-1/exercises/decimal.js | 10 ++++++++- Sprint-1/exercises/initials.js | 2 ++ Sprint-1/exercises/paths.js | 4 ++++ Sprint-1/exercises/random.js | 25 ++++++++++++++++++++++ Sprint-1/interpret/percentage-change.js | 19 +++++++++++++---- Sprint-1/interpret/time-format.js | 28 +++++++++++++++++++++---- Sprint-1/interpret/to-pounds.js | 12 +++++++++++ 13 files changed, 114 insertions(+), 12 deletions(-) diff --git a/Sprint-1/errors/0.js b/Sprint-1/errors/0.js index cf6c5039..35152cfd 100644 --- a/Sprint-1/errors/0.js +++ b/Sprint-1/errors/0.js @@ -1,2 +1,5 @@ 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? +console.log() +//SyntaxError: Unexpected identifier +//It misses certain of java script structure like keywords ,variables and functions \ No newline at end of file diff --git a/Sprint-1/errors/1.js b/Sprint-1/errors/1.js index 7a43cbea..48ea01de 100644 --- a/Sprint-1/errors/1.js +++ b/Sprint-1/errors/1.js @@ -2,3 +2,6 @@ const age = 33; age = age + 1; +console.log(age) +//TypeError: Assignment to constant variable. + //he const keyword is initialized once,can not change or update the constant variable twice \ No newline at end of file diff --git a/Sprint-1/errors/2.js b/Sprint-1/errors/2.js index e09b8983..7331f61a 100644 --- a/Sprint-1/errors/2.js +++ b/Sprint-1/errors/2.js @@ -3,3 +3,5 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +// ReferenceError: Cannot access 'cityOfBirth' before initialization +//this occurs when it is attempted to access the variable before declaring it with in that scope ,so "const cityOfBirth = "Bolton"; should come first. \ No newline at end of file diff --git a/Sprint-1/errors/3.js b/Sprint-1/errors/3.js index ec101884..58bfa2f1 100644 --- a/Sprint-1/errors/3.js +++ b/Sprint-1/errors/3.js @@ -1,5 +1,5 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = cardNumber.toString().slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +7,9 @@ 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 +console.log(last4Digits); +//TypeError: cardNumber.slice is not a function +//the error is occured because cardNumber variable stored a number not a string ,this was as I predicted +//in order to make this string I add toString function to change it to string under the colon +//root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/errors# node 3.js +//4213 \ No newline at end of file diff --git a/Sprint-1/errors/4.js b/Sprint-1/errors/4.js index 21dad8c5..9815f649 100644 --- a/Sprint-1/errors/4.js +++ b/Sprint-1/errors/4.js @@ -1,2 +1,6 @@ const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +console.log(12HourClockTime) +const 24hourClockTime = "08:53"; +console.log(24HourClockTime) +//SyntaxError: Invalid or unexpected token +//Since the variable starts with number it returns SyntaxError. In java script variables can not start with number. \ No newline at end of file diff --git a/Sprint-1/exercises/count.js b/Sprint-1/exercises/count.js index 117bcb2b..f64bddaf 100644 --- a/Sprint-1/exercises/count.js +++ b/Sprint-1/exercises/count.js @@ -4,3 +4,5 @@ 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 + // the count is being increamented by one , and the initial value is 0 +console.log(count) \ No newline at end of file diff --git a/Sprint-1/exercises/decimal.js b/Sprint-1/exercises/decimal.js index cc5947ce..c785b26b 100644 --- a/Sprint-1/exercises/decimal.js +++ b/Sprint-1/exercises/decimal.js @@ -3,7 +3,15 @@ const num = 56.5678; // 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 ) +let wholeNumberPart = Math.floor(num); +console.log(wholeNumberPart); // Create a variable called decimalPart and assign to it an expression that evaluates to 0.5678 ( the decimal part of num ) +let decimalPart = (num%1).toFixed(4) +console.log(decimalPart) // Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number ) - +roundedNum=Math.round(num); +console.log(roundedNum); // Log your variables to the console to check your answers + + + diff --git a/Sprint-1/exercises/initials.js b/Sprint-1/exercises/initials.js index 6b80cd13..7a6ccf43 100644 --- a/Sprint-1/exercises/initials.js +++ b/Sprint-1/exercises/initials.js @@ -4,3 +4,5 @@ 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.slice(0,1) + middleName.slice(0,1) + lastName.slice(0,1) +console.log(initials) \ No newline at end of file diff --git a/Sprint-1/exercises/paths.js b/Sprint-1/exercises/paths.js index c91cd2ab..81e7dd2f 100644 --- a/Sprint-1/exercises/paths.js +++ b/Sprint-1/exercises/paths.js @@ -15,4 +15,8 @@ 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 +let dirPart = filePath.slice(7,44) +console.log(dirPart) // Create a variable to store the ext part of the variable +let extPart = filePath.slice(-4) +console.log(extPart) \ No newline at end of file diff --git a/Sprint-1/exercises/random.js b/Sprint-1/exercises/random.js index 292f83aa..f092cbb0 100644 --- a/Sprint-1/exercises/random.js +++ b/Sprint-1/exercises/random.js @@ -4,6 +4,31 @@ const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // In this exercise, you will need to work out what num represents? + //Math.floor(Math.random() * (maximum - minimum + 1)) + minimum + //Math.floor(Math.random() * (100 - 1 + 1)) + 1 + //Math.floor(Math.random() * (100)) + 1 + //Math.floor(0.1812 * (100)) + 1 + //Math.floor(18.12) + 1 + //18 + 1 + //19=this could be change as the Math.random() function could returns any random value // Try breaking down the expression and using documentation to explain what it means + //Math.random()= returns a random value between 0 and 1 + //minimum and maximum variables are already assigned to the values 1 and 100 respectively // 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 +// root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/exercises# node random.js +// 73 +// root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/exercises# node random.js +// 40 +// root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/exercises# node random.js +// 73 +// root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/exercises# node random.js +// 99 +// root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/exercises# node random.js +// 79 +// root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/exercises# node random.js +// 87 +// root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/exercises# node random.js +// 8 +// root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/exercises# +console.log(num) \ No newline at end of file diff --git a/Sprint-1/interpret/percentage-change.js b/Sprint-1/interpret/percentage-change.js index e24ecb8e..d1aa47b9 100644 --- a/Sprint-1/interpret/percentage-change.js +++ b/Sprint-1/interpret/percentage-change.js @@ -12,11 +12,22 @@ 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 - + //four functions + //carPrice = Number(carPrice.replaceAll(",", "")); + //priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + //replaceAll(",", "") + //replaceAll("," "") // 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? - + //line 5,the error is because of missing comma in replaceAll function // c) Identify all the lines that are variable reassignment statements - + // const priceDifference = carPrice - priceAfterOneYear; + // const percentageChange = (priceDifference / carPrice) * 100; // d) Identify all the lines that are variable declarations - + // let carPrice = "10,000"; + // let priceAfterOneYear = "8,543"; + // const priceDifference = carPrice - priceAfterOneYear; + // const percentageChange = (priceDifference / carPrice) * 100; // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + //Number(carPrice.replaceAll(",","")) here ,It has two functions replaceAll() and Number + //replaceAll() function removes all "," + //Number function change the the string data to numeric \ No newline at end of file diff --git a/Sprint-1/interpret/time-format.js b/Sprint-1/interpret/time-format.js index 83232e43..76cc0cff 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 = 9999; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,13 +12,33 @@ 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? + //Five // b) How many function calls are there? + //one // c) Using documentation, explain what the expression movieLength % 60 represents - + //movieLength % 60 calculates the remainder ,that found by dividing 8784/60 // d) Interpret line 4, what does the expression assigned to totalMinutes mean? - + //const totalMinutes = (movieLength - remainingSeconds) / 60; + //movieLength = 8784 + //remainingSeconds = 24 + // = (8784-24)/60=8760/60=146 // e) What do you think the variable result represents? Can you think of a better name for this variable? - + //`${totalHours}:${remainingMinutes}:${remainingSeconds}` + //formats the values of totalHours, remainingMinutes, and remainingSeconds into a time-like string in the format hours:minutes:seconds. + //durationTime=`${totalHours}:${remainingMinutes}:${remainingSeconds}` // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + //yes!it works for all values + //root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/interpret# node time-format.js + // 2:26:24 + // root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/interpret# node time-format.js + // 2:26:22 + // root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/interpret# node time-format.js + // 2:26:0 + // root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/interpret# node time-format.js + // 0:1:0 + // root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/interpret# node time-format.js + // 0:0:20 + // root@acer:~/CYF/Module-Structuring-and-Testing-Data/Sprint-1/interpret# node time-format.js + // 0:14:59 diff --git a/Sprint-1/interpret/to-pounds.js b/Sprint-1/interpret/to-pounds.js index 60c9ace6..0db3c690 100644 --- a/Sprint-1/interpret/to-pounds.js +++ b/Sprint-1/interpret/to-pounds.js @@ -25,3 +25,15 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + + // 2.const penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1);:it initializing the string from the start(index[0]) by excluding the last character therefore,the p will be excluded//399 + + //3.const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): padStart makes sure that paddedPenceNumberString is three characters long//399 + + //4.const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2):it initializing the string from the start(index[0]) by excluding the last two characters//3 + + //5.const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"):two functions here + + //the first one paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) : This calculates the starting index for the substring, which is two characters from the end of the string//99 + //the second one is padEnd(2, "0") which takes the last two characters of the string//99 + //6. console.log(`£${pounds}.${pence}`): this takes the value pounds and pence to be returned. \ No newline at end of file From 2e1e5fa092fcefcab67bf8f0baa977bca84ba0e5 Mon Sep 17 00:00:00 2001 From: RahwaZeslusHaile Date: Sun, 15 Dec 2024 00:33:48 +0000 Subject: [PATCH 2/4] correction of BMI calculation --- Sprint-2/implement/bmi.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Sprint-2/implement/bmi.js b/Sprint-2/implement/bmi.js index 259f62d4..0ff38e22 100644 --- a/Sprint-2/implement/bmi.js +++ b/Sprint-2/implement/bmi.js @@ -1,3 +1,4 @@ + // Below are the steps for how BMI is calculated // The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared. @@ -13,3 +14,16 @@ // Given someone's weight in kg and height in metres // Then when we call this function with the weight and height // It should return their Body Mass Index to 1 decimal place + + + +function BMICalculation(height, adultWeight) { + // Calculate BMI + const heightSquared = height * height; + const BMI = adultWeight / heightSquared; + + + return BMI.toFixed(1); +} + +console.log(BMICalculation(1.5, 60)); From 1d77039cf04a603b2d5cb9bc77ec1bc51e0a2320 Mon Sep 17 00:00:00 2001 From: RahwaZeslusHaile Date: Sun, 15 Dec 2024 00:39:09 +0000 Subject: [PATCH 3/4] Handling edge cases of the parametre --- Sprint-2/implement/to-pounds.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Sprint-2/implement/to-pounds.js b/Sprint-2/implement/to-pounds.js index 6265a1a7..08c8914c 100644 --- a/Sprint-2/implement/to-pounds.js +++ b/Sprint-2/implement/to-pounds.js @@ -4,3 +4,31 @@ // You will need to declare a function called toPounds with an appropriately named parameter. // You should call this function a number of times to check it works for different inputs +function toPounds(penceString) { + + if (!penceString || !penceString.endsWith("P")||isNaN(penceString)) { + return "Invalid input. Please provide a valid pence amount"; + } + + +const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1) + + +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2) + + +const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + return (`£${pounds}.${pence}`); +} + +console.log(toPounds("")) + + From d5fdd9ff0f862e9019bf66ef4bc9c708da8b5efd Mon Sep 17 00:00:00 2001 From: RahwaZeslusHaile Date: Sun, 15 Dec 2024 00:40:58 +0000 Subject: [PATCH 4/4] Proper declaration --- Sprint-2/implement/vat.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-2/implement/vat.js b/Sprint-2/implement/vat.js index 3fb16722..139516a5 100644 --- a/Sprint-2/implement/vat.js +++ b/Sprint-2/implement/vat.js @@ -8,3 +8,8 @@ // Given a number, // When I call this function with a number // it returns the new price with VAT added on +function priceWithoutVAT(price){ + const priceWithVAT = price + (price * 0.2); + return priceWithVAT; + } + console.log (priceWithoutVAT(50)); \ No newline at end of file