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

NW-ITP | Mohammed Alzaki | Module-Structuring-and-Testing-Data | Sprint1| week3 #389

Open
wants to merge 12 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
2 changes: 2 additions & 0 deletions Sprint-1/1-key-exercises/1-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
// In line three count is a constant variable but since we can not reassign constant variables we will use let here,
// We want to reassigning count to whatever the count value was before in this case 0 and we want to add 1 to that value meaning => 0 + 1 = 1.
4 changes: 3 additions & 1 deletion Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ 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 = ``;
let initials = `${firstName.charAt(0)}${ middleName.charAt(0)}${lastName.charAt(0)}`;
console.log(initials)
//I used charAt() method to retrieve a character from a specific index in a string.`;

// https://www.google.com/search?q=get+first+character+of+string+mdn

9 changes: 7 additions & 2 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ 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 = ;
const ext = ;
//// The directory part (everything before the last "/")
const dir = filePath.slice(0, lastSlashIndex);
console.log(`The directory part of ${filePath} is ${dir}`)
// The extension part is located 5 characters after the last "/"
const ext = filePath.slice(lastSlashIndex+ 5);
console.log(`The extension part of ${filePath} is ${ext}`)


// https://www.google.com/search?q=slice+mdn
10 changes: 10 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ 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

//Explanation :
//Math.floor() function is used to round a number down to the nearest integer.
// It always rounds a number down, regardless of the fractional part.
//The Math.random() function is used to generate a random floating-point number between 0 (inclusive) and 1 (exclusive).
//If we want to generate a random number between 0 and 100 we use Math.random() * 101
//By using Math.floor() we are dropping any decimal part of the number to keep only integers between 0 and 100
//maximum - minimum + 1 => maximum is 100 and minimum is 1, so:
//100 - 1 + 1 = 100
//The number num will be a random integer between 1 and 100 (inclusive).
5 changes: 3 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -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?
//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? */
/* Commenting them */
8 changes: 7 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
/* const age = 33;
age = age + 1; */

//The error : TypeError: Assignment to constant variable.
//Fix

let age = 33;
age = age + 1;
8 changes: 6 additions & 2 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// 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}`);


////Erorr : ReferenceError: Cannot access 'cityOfBirth' before initialization
//Fix
// Is to put the variable declaration line before the console.log()
10 changes: 8 additions & 2 deletions Sprint-1/2-mandatory-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);
console.log(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?
// Then try updating the expression last4Digits is assigned to, in order to get the correct value

//Solution:
// I'm guessing the code wont't work because slice() dose not work with numbers
//Error : TypeError: cardNumber.slice is not a function.
// The first way to correct the code is by changing cardNumber to a string => const cardNumber = "4533787178994213";
//Second option is to use toString() method to convert any value to a string.
12 changes: 10 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
/* const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53"; */

//// Erorr : SyntaxError: Invalid or unexpected token
// A varible name cannot start with a number
//First fix => const HourClockTime12 = "20:53";
//const hourClockTime24 = "08:53"
//Second fix:
const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";
22 changes: 18 additions & 4 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,25 @@ 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

//There are three function calls the first one in line 4 replaceAll() called on carPrice string => carPrice = Number(carPrice.replaceAll(",", ""));
//The second time in line 5 replaceAll() called on priceAfterOneYear string => priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ,""));
// The function Number() to convert all strings to numbers.
// 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 is coming from line 5 execution where it throws a SyntaxError: missing ) after argument list
//Fix => by adding the missing colon "," => priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ,""));
// c) Identify all the lines that are variable reassignment statements

// Line 4 => carPrice = Number(carPrice.replaceAll(",", ""));
//Line 5 => priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ,""));
//Line 7 => const priceDifference = carPrice - priceAfterOneYear;
//Line 8 => const percentageChange = (priceDifference / carPrice) * 100;
// d) Identify all the lines that are variable declarations

//Line 1 => let carPrice = "10,000";
//Line 2 => let priceAfterOneYear = "8,543";
//line 7 => const priceDifference = carPrice - priceAfterOneYear;
//line 8 => const percentageChange = (priceDifference / carPrice) * 100;
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
//carPrice.replaceAll(",", "") This part removes all commas from the string carPrice. The replaceAll() used to replace all coma's with nothing = ""
// carPrice = "10,000"; => carPrice ="10000"
//then used the Number() function/method to convert strings to numbers => carPrice = "10000" => carPrice = 1000;
//The purpose of the expression is to convert a price that is in string type (with commas) into a number so that it can be used for mathematics operations,
// such as calculating the price difference or percentage change.
23 changes: 23 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,37 @@ 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 six variable declarations :
//Line 1 => const movieLength = 8784; // length of movie in seconds
//Line 3 => const remainingSeconds = movieLength % 60;
//Line 4 => const totalMinutes = (movieLength - remainingSeconds) / 60;
//Line 6 => const remainingMinutes = totalMinutes % 60;
//Line 7 => const totalHours = (totalMinutes - remainingMinutes) / 60;
//Line 9 => const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;

// b) How many function calls are there?
// One => console.log()

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
//The % symbol is the modulo operator.the modulo operator returns the remainder of a division.
//So, movieLength % 60 calculates the remainder when movieLength is divided by 60.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
//Lets brake it down:
// First => (movieLength - remainingSeconds) First, it subtracts the remaining seconds from the total movie length to get the
// remaining time in seconds that represents full minutes.
//Next /60 => Next, it divides the remaining seconds by 60 to convert the result into full minutes.
// totalMinutes => represents the total number of full minutes in the movie, excluding the remaining seconds.

// e) What do you think the variable result represents? Can you think of a better name for this variable?
//The variable result represents the time format of the movie, in hours, minutes, and seconds in the format HH:MM:SS
// Result as a variable name dose not describe really well what this variable actually stores a better name would be:
// movieTimeFormat or movieDuration

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// The current output for this code when movieLength = 8784; is => 2:26:24
// lets say movieLength = 0; the output => 0:0:0 although the output is correct it's not informative or useful or possible
// for any movie to exist without any screen time that would = 0.
// lets say movieLength = -320; the output => 0:-5:-20 the output dose not make sense because we got negative minutes and seconds
// and this dose not represent a valid movie time duration.
30 changes: 29 additions & 1 deletion Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,32 @@ 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": initialises a string variable with the value "399p"

2.const penceString = "399p";:
This line initializes a constant variable named penceString and assigns it the string value "399p". This string represents a price in pence. The const keyword means the variable's value cannot be reassigned later in the program.
const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);:
This line creates a new constant variable called penceStringWithoutTrailingP. It uses the substring() method to extract a portion of the penceString. penceString.length gets the total length of the string (5 in this case). Subtracting 1 gives us 4. substring(0, 4) extracts the characters from index 0 up to (but not including) index 4, effectively removing the last character "p". The result, "399", is stored in penceStringWithoutTrailingP. The rationale is to isolate the numerical part of the pence value.
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");:

This line creates another constant, paddedPenceNumberString. It uses the padStart() method to
pad the penceStringWithoutTrailingP with leading zeros until it reaches a length of 3. If the
string is already 3 or more characters long, no padding is added. In our example, "399" is
already 3 digits, so no padding occurs. The result, "399", is stored in paddedPenceNumberString.
The rationale for padding is to ensure that even single-digit or double-digit pence values are
formatted correctly when converted to pounds and pence (e.g., "7p" becomes "007").

const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);:
This line extracts the pounds portion of the price. paddedPenceNumberString.length - 2 calculates the index
two characters from the end. substring(0, paddedPenceNumberString.length - 2) extracts the
characters from the beginning up to that index.
In our example, paddedPenceNumberString.length is 3,
so paddedPenceNumberString.length - 2 is 1. substring(0, 1) extracts the first character, "3".
The result, "3", is assigned to the pounds constant. The rationale is to separate the pounds from
the pence.

const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");:
This line extracts the pence portion. paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) extracts the last two characters of the padded number string ("99" in our example). The padEnd(2, "0") method ensures that the pence value has at least two digits, adding trailing zeros if necessary. In our case, "99" already has two digits, so no padding is added. The result, "99", is assigned to the pence constant. The rationale is to isolate the pence value.
console.log(£pounds.{pence});: This line uses a template literal (backticks) to create a string that combines the pounds and pence values with a pound symbol and a decimal point. It then uses console.log() to print this formatted string to the console. In our example, it will print "£3.99". This is the final formatted price in pounds and pence.

*/
Loading