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
2 changes: 2 additions & 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,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
//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
3 changes: 2 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,7 @@ 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 = `${firstName[0]}${middleName[0]}${lastName[0]}`;

console.log(initials); // Output CKJ
// https://www.google.com/search?q=get+first+character+of+string+mdn
19 changes: 16 additions & 3 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 @@ -17,7 +17,20 @@ 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 = ;
// 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
// 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 -_-
14 changes: 13 additions & 1 deletion 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 @@ -2,8 +2,20 @@ const minimum = 1;
const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;

console.log(num);
// In this exercise, you will need to work out what num represents?
// 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

//First, const minimum and const maximum are variables
// = is the assignment 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) and then pass it to Math.floor
//We are asking the program to round whatever result we have to the nearest whole number

//done
3 changes: 2 additions & 1 deletion Sprint-2/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?
We don't want the computer to run these 2 lines - how can we solve this problem?
//we use a (//)) to comment it. So the computer doesn't execute 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;
//a shorthand assignment operator
//yes, +=
//an error message shows up: TypeError: Assignment to constant variable.

//to solve this, we replace const with let to make the variable value changeable, then we run the program
5 changes: 4 additions & 1 deletion 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
@@ -1,5 +1,8 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?
//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 execute before declaring the variable

//to solve this, we reverse the order of both lines so the declaration comes before the print functions

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
8 changes: 6 additions & 2 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
@@ -1,9 +1,13 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);

const last4Digits = cardNumber.toString().slice(-4);
console.log(last4Digits);
//the output is 4213
// 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
//it won't work because cardNumber is not a string
// Then run the code and see what error it gives.
//TypeError: cardNumber.slice is not a function so a string method can't br used with it
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
// The error is caused because card number is a number not a string and toString is a string method so its a different type of data
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
13 changes: 11 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,11 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";
const twelveHourClockTime = "8:53pm";
const twintyFourhourClockTime = "20:53";

//we see the error message SyntaxError: Invalid or unexpected token,

//because variable conventions don't allow numbers to be the start of variable names

//to solve this issue, we change the numbers to words format

//done

19 changes: 13 additions & 6 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
@@ -1,8 +1,8 @@
let carPrice = "10,000";
let priceAfterOneYear = "8,543";

//console.log(typeof carPrice);
carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Expand All @@ -12,11 +12,18 @@ 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

//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 SyntaxError: missing ) after argument list
//The error is coming from line 5, column 61. There is nothing seprating the two indexes. A comma can should 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 expression Number(carPrice.replaceAll(",","")) change the string value to anumber that can be calculated and subtracted
//line 7 needs it so the calculation could work, a string can't be subtractedn from a number so it has to be changed to a number

//console.log(typeof carPrice);

//done
18 changes: 12 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
@@ -1,5 +1,5 @@
const movieLength = 8784; // length of movie in seconds

const movieLength = -1000000; // length of movie in seconds
//not complete
const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;

Expand All @@ -12,14 +12,20 @@ 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?

// 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
// it represent the number of seconds left over after taking all the complete minutes

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

//here in the expression we have the movieLength taken from it the number of remainingSeconds, so the operator / 60 represents the conversion of the remaining seconds into minuttes, so we left with complete minutes only
// 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
//8784
//if experimented values into the movieLength variable what will happen? Lets start with the following values:
//-10 , 0, 10 , 10000000, -1000000
//0:0:-10, 0:0:0,0:0:10,2777:46:40, -277:-46:-40
//ok I notice the result get strange with big negative numbers, so I think the code needs to be modified to restrict using negative values
38 changes: 36 additions & 2 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 @@ -2,13 +2,13 @@ const penceString = "399p";

const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
penceString.length - 1,
);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
paddedPenceNumberString.length - 2,
);

const pence = paddedPenceNumberString
Expand All @@ -25,3 +25,37 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"

//for this code I struggled with some part so I had to ask copilot to explain some lines of the code to me
//const penceStringWithoutTrailingP = penceString.substring(
// 0,
// penceString.length - 1
//);
//const penceStringWithoutTrailingP: a string variable is initialised and assigned
// using the = operator to be equal to the value of string penceString when the method substring is used to cut part of the string
//so substring(0, penceString.length - 1);: means use substring method starting from index zero and end before the last character pencestring.length-1 : which is the length of the string
//but how dom we know which index? Its is basically the characters in string where "399p" means we have 4 characters and we want to take 1
//do 4 - 1 = 3 , so we are removing p character from the string so it becomes 399
//this line says take the string penceStringWithoutTrailingP and use padStart()string method
// (3, "0"); this means make the string 3 characters at least and if not add o to the beginning
//const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
//this line means take the string paddedPenceNumberString and use the
//substring(0, length - 2) method to remove the last 2 characters , than store
//the value into the pounds variable
//const pounds = paddedPenceNumberString.substring(
// 0,
// paddedPenceNumberString.length - 2
//);

//this (paddedPenceNumberString.length - 2) mean find the position of the last 2 characters
//in the string paddedPenceNumberString
//substring()means take the last 2 characters you found from the string
//.padEnd(2, "0"); means if the result is less than two characters , than add zero at
//the end until it reaches two digits
//const pence = paddedPenceNumberString
//.substring(paddedPenceNumberString.length - 2)
// .padEnd(2, "0");
//this line uses console.log to print the value of ponds and pence
//it uses literal template to find the value of pounds, uses a decimal point , than finds the value of pence
//it also uses a pound sign at the front
// console.log(`£${pounds}.${pence}`);
Loading