Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

northwest |Rahwa Zeslus |module-structure-and-Testing-Data|week2 #226

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Sprint-1/errors/0.js
Original file line number Diff line number Diff line change
@@ -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?
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
3 changes: 3 additions & 0 deletions Sprint-1/errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions Sprint-1/errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
8 changes: 7 additions & 1 deletion Sprint-1/errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
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
// 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?
// 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
6 changes: 5 additions & 1 deletion Sprint-1/errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
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.
2 changes: 2 additions & 0 deletions Sprint-1/exercises/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 9 additions & 1 deletion Sprint-1/exercises/decimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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



2 changes: 2 additions & 0 deletions Sprint-1/exercises/initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 4 additions & 0 deletions Sprint-1/exercises/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
25 changes: 25 additions & 0 deletions Sprint-1/exercises/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 15 additions & 4 deletions Sprint-1/interpret/percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 24 additions & 4 deletions Sprint-1/interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
12 changes: 12 additions & 0 deletions Sprint-1/interpret/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
14 changes: 14 additions & 0 deletions Sprint-2/implement/bmi.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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));
28 changes: 28 additions & 0 deletions Sprint-2/implement/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(""))


5 changes: 5 additions & 0 deletions Sprint-2/implement/vat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));