Skip to content
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
1 change: 1 addition & 0 deletions Sprint-2/1-key-exercises/1-count.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great job! You correctly identified how the variable is initialized and reassigned.

Quick question: How would you specifically describe the role of the = sign in that second line?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

//Line two we see the variable count using the = sign to assign a new value whcih is count previous value + 1
//the equal sign in line two is assigned operator that stores the new value to the variable

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
//a variable is initialised and assigned a value zero, than it was reassigned additing 1 to its value.
6 changes: 5 additions & 1 deletion Sprint-2/1-key-exercises/2-initials.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great job! This is a very clean and clever solution using template literals and bracket notation.

Quick question: Is there another built-in string method besides bracket notation you could use to get the first character?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

string.prototype.replace()

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const 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.

const initials = ``;
//const initials = ``;

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

const initials = `${firstName[0]}${middleName[0]}${lastName[0]}`;

console.log(initials); // Output CKJ
49 changes: 43 additions & 6 deletions Sprint-2/1-key-exercises/3-paths.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Spot-on implementation for extracting dir and ext! To polish your code, you can make the comments a bit more direct and concise.

Quick question: When using filePath.slice(0, lastSlashIndex) for dir, why do we include 0 as the starting index, and what happens if you omit it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If we omit the first index, the string won't break but will go to the wrong location; in the case of omitting the 0 index, it will delete anything before the last index ''/'

Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,52 @@

// (All spaces in the "" line should be ignored. They are purely for formatting.)

//const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
//const lastSlashIndex = filePath.lastIndexOf("/");
//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
// Create a variable to store the ext part of the variable


//const dir = ;
//const ext = ;

// https://www.google.com/search?q=slice+mdn

// from here start my explaination of the code I see for education purposes.
// this is a file system that allows users to manipulate what files they see and what information they omit, and they use string
//methods to do that
// most common methods developers use to manipulate the files are included in this file Sprint-2\1-key-exercises\manipulating files.txt

// in this example the string methods are used to manipulate the file system

const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
const base = filePath.slice(lastSlashIndex + 1);
console.log(`The base part of ${filePath} is ${base}`);
// a variable is created to store the file path of a file in the file system
//const lastSlashIndex = filePath.lastIndexOf("/");
// a variable is created and identified to find the last occurance of the "/" character in the filePath variable, which is used to separate directories and files in a file system
//const base = filePath.slice(lastSlashIndex + 1);
// a variable is created and identified to extract the base part of the filePath variable, which is the name of the file and its extension, by using the slice method
//console.log(`The base part of ${filePath} is ${base}`);
// console.log is used to print the base part of the filePath variable to the console, which is the name of the file and its extension

// 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 = ;
const lastSlashIndex = filePath.lastIndexOf("/");
// here we have the variable lastslashIndex already created and identified its position
// stores the position or (index) of "/" in filePath, so this "/" is used as a symbol that storers the number index or a place that seprate dir from base
const dir = filePath.slice(0, lastSlashIndex);
console.log(dir);
// we show our result in the console
const lastDotIndex = filePath.lastIndexOf('.');
// we create a variable and assign its value using the indexOf to be from the dot location
const ext = filePath.slice(lastDotIndex);
// we create a variable where we assign its value using the slice method to be from the dot only

// https://www.google.com/search?q=slice+mdn
console.log(ext)
// we print our result in the console
//slice(startIndex, endIndex)
// means, start here and stop here. we are doing this to seprate the dir from base so we need to tell it where to start and where to end, hence that is why we use slice method,
// my comments needs correcting to look profissional -_-
13 changes: 13 additions & 0 deletions Sprint-2/1-key-exercises/4-random.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Clear breakdown of the math logic!

Just remember the order: Math.random() runs first to generate a decimal, then the math scales it, and finally Math.floor() rounds it down.

Quick question: What purpose does the + 1 serve inside (maximum - minimum + 1)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I had to search for this one to understand it. Math.random() generates a decimal that ranges between the values 0 and 0.99999, so when we try to scale it up Math.random() will generate values between 0 and 8.9999, so it doesn't include 9. This becomes an issue when we use Math.floor as it will round it to the nearest whole number, and because it doesn't include the number 9, it will round down from 0 to 8.

Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@ 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


//here starts my learning

// first const minimun, const maximum are the variables
// = the assignmen operator
// what comes after = is what the variable will store
// Math.floor is a method and here we are telling it that its argument is to use the random method
// and multiply the result before rounding with the maximum and minimum variables we created
// * is a multiplication operator
// to understand what is going on we need to understand what the floor and random methods do
//Math.floor (rounds down to the nearest whole number) and than pass it to Math.floor
// we are asking the program to round whatever result we have to the nearest whole number

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Two things here.

  1. Math.floor always rounds down, as you said on line 21. Math.floor(4.9) is 4, not 5. Change "nearest" on line 22.
  2. The question asks what num represents. Add console.log(num); after line 4 and run the file ten times. Then finish with one line:
// So num is a random whole number from ... to ...

Your thread reply already explains why the + 1 is there, so you can copy that in too.

6 changes: 4 additions & 2 deletions Sprint-2/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
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?

//we use slash to comment it. So the computer doesn't excute the text.
7 changes: 6 additions & 1 deletion Sprint-2/2-mandatory-errors/1.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Spot-on distinction between const and let for handling variable updates.

Quick question: Is there a shorthand assignment operator you could use instead of writing age = age + 1?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

yes, +=

Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
let age = 33;
age = age + 1;

//we change the variable declaration and reassign its value

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your fix is right. This section also wants the error message node gives for the original code. To see it:

  1. Change let back to const on line 3 and run node 1.js.
  2. Copy the line that starts with TypeError into a comment here.
  3. Change const back to let.

Do the same in 4.js (remove the // from line 5 for a moment), and in 2.js and 3.js before you fix them. Each file should end up with the error message and your explanation.

// variable declaration keywords
// const can't be used to reassign a value
// let can be used to reassign a value
2 changes: 2 additions & 0 deletions Sprint-2/2-mandatory-errors/2.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Accurate explanation! Trying to use cityOfBirth before its declaration triggers a ReferenceError because the variable hasn't been initialized yet.

Quick question: Do you know the technical term for the area in the code before a const or let is declared where JavaScript won't let you access it?

Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

console.log(`I was born in ${cityOfBirth}`);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your explanation on line 7 is right. JavaScript runs the file from top to bottom, and line 4 uses cityOfBirth before line 5 has created it.

So what order should these two lines be in? Change it, run node 2.js, and you should see I was born in Bolton.

const cityOfBirth = "Bolton";

// the variable was declared after the print function so the program doesn't know what is cityofBirth. Therefore it can't perform the print function or excuted before declaring the variable
1 change: 1 addition & 0 deletions Sprint-2/2-mandatory-errors/3.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Not quite a ReferenceError here—cardNumber is actually declared, but because it's a number rather than a string, JavaScript throws a TypeError because .slice() isn't a valid method for numbers.

Quick question: What method can you call on cardNumber to convert it into a string first so .slice(-4) will work?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

toString()

Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ 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
// in this case its a reference error the program is trying to access a value that wasn't declared. In this case a variable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This one isn't a ReferenceError. Run the file and look at the last line of the error: node says TypeError: cardNumber.slice is not a function. cardNumber is there (line 1), but it holds a number, and .slice() only works on strings.

  1. In your thread reply you found toString(). Put it between cardNumber and .slice(-4) on line 2. Add console.log(last4Digits); underneath and run it: you should see 4213.
  2. Replace line 10 with three comments in your own words:
// Prediction: ...
// Error: TypeError: cardNumber.slice is not a function
// Why: ...

8 changes: 6 additions & 2 deletions Sprint-2/2-mandatory-errors/4.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Spot-on explanation! Variable names cannot start with numbers.

Quick question: While starting with a capital letter works syntactically, what is the standard JavaScript naming convention (case style) usually used for variables made of multiple words like this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

camelCase

Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";
const TwelveHourClockTime = "8:53pm";
const TwentyFourHourClockTime = "20:53";
// an identifier or keyword can't follow a numeric literal. We fix this by writing the numbers in words format

//const 12HourClockTime = "8:53pm";
//const 24hourClockTime = "20:53";
14 changes: 9 additions & 5 deletions Sprint-2/3-mandatory-interpret/1-percentage-change.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Spot-on answers! Your breakdown of line 5's syntax error, reassignments, and the purpose of replaceAll() is completely accurate.

Quick question: Why is replaceAll() a better choice here than a standard replace() method?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

replaceAll includes all the specified characters and changes them, unlike replace(), which searches for the character but stops once it is found.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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;
Expand All @@ -11,12 +11,16 @@ 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

// a) How many function calls are there in this file? Write down all the lines where a function call is made
//5 FUNCTION calls. 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 is coming from line 5, column 61. Thhere is nothing seprating the two indexes. A comma can be used to seprate them.
// c) Identify all the lines that are variable reassignment statements

//Two lines 4 and 5
// d) Identify all the lines that are variable declarations

//four lines 1,2,7,8
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
//The replace.all method is used to include the car price and the price after one year of depreciation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You've explained replaceAll. Now Number(...). Add these two lines and run the file:

console.log(typeof carPrice); // put this before line 4
console.log(typeof carPrice); // put this after line 4

What changes? Write one sentence about what Number(...) did, and why line 7 needs it before subtracting. Then delete the two lines.

//The purpose of the expression is to take the price string and search for every comma and replace it
// we are saying replace this "," , with this "" and because of the empty string it means without a comma.
14 changes: 8 additions & 6 deletions Sprint-2/3-mandatory-interpret/2-time-format.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Spot-on analysis! Your observation about leading zeros triggering an octal syntax error is a brilliant and advanced catch.

Quick question: If remainingSeconds is 5, the output currently shows ...:5 instead of ...:05. How might you ensure single-digit numbers always display with two digits?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Since the digit zero is before the 5. We create a numerical string, then convert the string to a numerical value or add the zero digit as a string before the number or we modify the function to include strings so we can manipulate the numbers.

Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ 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?

// a) How many variable declarations are there in this program?
// 6 declarations
// b) How many function calls are there?

// 1 function call in line 10
// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

// "operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend."
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

// it means the number of minutes in a film is calculated by calculating the number of seconds in the film

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

c) and d) need the numbers from this program. Add these two lines after line 4 and run the file:

console.log(remainingSeconds);
console.log(totalMinutes);

Then fill in:

// c) movieLength % 60 is ..., which is the seconds left over after ...
// d) totalMinutes is ..., the number of whole minutes. Line 4 takes away remainingSeconds first so that ...

Delete the two console.log lines afterwards.

// e) What do you think the variable result represents? Can you think of a better name for this variable?

//the variable result represesnt the length of the film in hours,minutes,and seconds. A better name would be movie time.
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// However, it doesn't work when the leading number of a decimal is zero like this 08784 because it interpreted as octal literal and 8 can't work in octal numeration

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

const movieLength = 08784; actually runs and prints 2:26:24, because 8 isn't an octal digit, so JavaScript reads it as a normal number. So this answer needs changing.

Change line 1 to each of these, run the file, and write down what it prints: 59, 3605, -100, 90.5. Would you show 0:0:59 to someone as a time? What would you expect it to look like instead? Write one sentence for each value that doesn't look right. Put line 1 back to 8784 afterwards.

//"The octal number system is a base-8 math and computing system that uses only eight digits: 0, 1, 2, 3, 4, 5, 6, and 7"
30 changes: 30 additions & 0 deletions Sprint-2/3-mandatory-interpret/3-to-pounds.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great step-by-step breakdown! You clearly traced how substring, padStart, and padEnd work together to parse and format the price string correctly.

Quick question: Why is padStart(3, "0") particularly important if someone inputs a smaller value like "9p" instead of "399p"?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It specify the starting values so we don't have random numbers that could cause a bug in the program. We restrict it.

Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,33 @@ 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
//);
//INITIALISES A STRING VARIABLE AND ASSIGN ITS VALUE USEING THE penceString.substring
// so what happins is penceString.substring is using the value from the application or calculation of (
//0,
// penceString.length - 1
//);
// we are tillING IT TO start from the first inex (0) and not to include index 3 which penceString.length than use the operator - to subtract 1
// we intialise a string variable const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); and assign its value to be
//const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
//const pounds = paddedPenceNumberString.substring(
// 0,
//paddedPenceNumberString.length - 2
//);
// we assign the value to be starting with MINUMUM LENGTH IS 3 CHARACTERS than if not ADD THE VALUE 0 TO THE BEGINNING

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your steps say which method is used, but not what it gives. Write the value each line produces for "399p". Here is step 2 as an example:

// 2. penceString.length is 4, so penceString.length - 1 is 3.
//    substring(0, 3) keeps characters 0, 1 and 2, giving "399" (the p is removed).

Now do the same for the rest, filling in the ...:

// 3. padStart(3, "0") gives "..."
// 4. pounds is "..."
// 5. pence is "..."
// 6. the console.log prints ...

Then change line 1 to "9p", run it, and write down what it prints. What did padStart do to "9"? Change line 1 back to "399p" afterwards.

// we initialise a variable const pounds and assign its value to be the value from using paddedPenceNumberString.substring
//paddedPenceNumberString.substring value is found by starting from the first index WITH LENGTH 3 and ending with paddedPenceNumberString.length subtracted by the operator 2

// IN THIS LINE const pence = paddedPenceNumberString
const pence = paddedPenceNumberString

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Lines 49 to 53 are copies of the program's own code (lines 14 to 18), so const pence is now created twice. Node refuses to run the file: SyntaxError: Identifier 'pence' has already been declared.

Delete lines 48 to 53, then run node 3-to-pounds.js. You should see £3.99 printed once. Your explanation should only be // comments; the program itself stays exactly as it was given.

.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

console.log(`£${pounds}.${pence}`);
// we initialise a string variable and assign its value to be the paddedPenceNumberString variable with the value from using the substring method
// the substring method is calculated by using the length property or the value of paddedPenceNumberString subtracting it by 2 using operator -
// than using the method .padEnd to specify that if the length is not two add the value 0
// last we ask the console to print the values in pounds and pence
Loading