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

WM4 | Fatima Safana |Module 2/Sprint1 Exercises | Week4 #213

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions Sprint-1/errors/0.js
Original file line number Diff line number Diff line change
@@ -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?
//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?
3 changes: 2 additions & 1 deletion Sprint-1/errors/1.js
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 2 additions & 2 deletions Sprint-1/errors/2.js
Original file line number Diff line number Diff line change
@@ -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}`);

12 changes: 11 additions & 1 deletion Sprint-1/errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
const cardNumber = 4533787178994213;
const cardNumber = "4533787178994213";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you cannot modify this statement const cardNumber = 4533787178994213;
(that is, keep the variable's value unchanged),
how would you modify the code so that it can still extract the last 4 digits from its value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the type of variable needs to string for .slice() method to work.

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
// 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" */
5 changes: 3 additions & 2 deletions Sprint-1/errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const twelveHourClockTime = "20:53";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you also noticed the variable names do not quite match the values assigned to the variable?

const twentyFourHourClockTime = "08:53";
//variable names cannot start with numbers.
1 change: 1 addition & 0 deletions Sprint-1/exercises/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 10 additions & 0 deletions Sprint-1/exercises/decimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)).toFixed(4);
console.log(decimalPart);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you run the program, you may notice that console.log() outputs the value of decimalPart
with more than 4 decimal places (which is normal because of floating point arithmetic).

If you were asked to print the value of decimalPart to exactly 4 decimal places,
how would you modify the code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let decimalPart = (num - Math.floor(num)).toFixed(4);
console.log(decimalPart);


let roundedNum = Math.round(num);
console.log(roundedNum);

4 changes: 4 additions & 0 deletions Sprint-1/exercises/initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
8 changes: 8 additions & 0 deletions Sprint-1/exercises/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code should be written to to work for any valid value of filePath.
For example, "/path1/path2/package.json".

console.log(`The directory path of ${dirPart}`);

const extPart = dirPart + "ext.txt";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "ext part" refers to the file extension of the file represented by the file path, which is the string after the dot in the base (including the dot).

So the file extension of "package.json" would be ".json".

console.log(`The ext path is ${extPart}`);
4 changes: 4 additions & 0 deletions Sprint-1/exercises/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exercise was expecting you to break down
the expression Math.floor(Math.random() * (maximum - minimum + 1)) + minimum, and explain
why such expression can correctly produce a random integer in the range [1, 100].

Also, to test your understanding, how would you write an expression
(without using any variable in the expression) that yields a random
integer between -5 and 3 (including -5 and 3)?

9 changes: 8 additions & 1 deletion Sprint-1/interpret/percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
8 changes: 7 additions & 1 deletion 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 = 10000; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;
Expand All @@ -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.
19 changes: 18 additions & 1 deletion Sprint-1/interpret/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);

Expand All @@ -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,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After line 9, what would paddedPenceNumberString be if penceString was "1234p" initially?
After line 9, what would paddedPenceNumberString be if penceString was "4p" initially?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will return 1234
It will return 004

// 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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "first value of the string"?
length - 2 is a valid range but "making sure its in a valid range" is not quite the reason why length - 2
is used in the code.

//18.the code gets the last two digits from the paddedPenceNumberString and ensures that the result
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we expect this program to work as intended even if we deleted .padEnd(2, "0") from the code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it will not work for single digit or double digits input

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, can you find a value of penceString to show that the function won't work properly without .padEnd(2, "0")?

//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.