Skip to content
Closed
17 changes: 17 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,20 @@ 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 this code variable is declare as count = 0, expression increments the value of count by 1 and variable count is updated to be count + 1.


//An expression is any valid unit of code that have value. In this code , we have different types of expressions:
// Assignment, arithmetic and compound assignment.
// An operand is a value or variable that an operator acts upon in an expression.

//count = 0 (Assignment count)
//count + 1 (Arithmetic count)
//count = count + 1 (Compound Assignment)

// count 0 =
//count 1 =
// count + 1 = (this is operand) all this (=, + and = are operator)
// So in this code count = 0 is an assignment expression because it assigns a value (0) to the variable count.
//The = operator is the assignment operator.
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);

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

//text.charAt(0) means it will return first letter, if we put 1 than it will return second letter.
12 changes: 8 additions & 4 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// │ dir │ base │
// ├──────┬ ├──────┬─────┤
// │ root │ │ name │ ext │
// " / home/user/dir / file .txt "
// / home/user/dir / file .txt
// └──────┴──────────────┴──────┴─────┘

// (All spaces in the "" line should be ignored. They are purely for formatting.)
Expand All @@ -17,7 +17,11 @@ 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 = ;
const dir = filePath.slice(0, lastSlashIndex);
console.log(`The dr part of ${filePath} is ${dir}`);
const lastDotIndex = base.lastIndexOf(".");
const ext = base.slice(lastDotIndex);
console.log(`The ext part of ${filePath} is ${ext}`);

// https://www.google.com/search?q=slice+mdn
// https://www.google.com/search?q=slice+mdn
//Using like lastIndex, lastSlashIndex, slice and LastDotIndex help to separate full file to other components in javascript.
43 changes: 43 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,51 @@ const minimum = 1;
const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
console.log(num);
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

/*Assignment Expressions:
minimum = 1
maximum = 100
const num = ...

Function Call Expressions:
Math.random()
Math.floor(...)
console.log(num)

Arithmetic Expressions:
(maximum - minimum + 1) (subtraction and addition)
Math.random() * (maximum - minimum + 1) (multiplication)
Math.floor(...) + minimum (addition)


When you log several time it will so different value.
Math.random gives u the value if decimal number is higher than 0.5 than the output will round up or less than 0.5 will rounds down. for example: 5.95 will be 6, 5.34 output will be 5 and -4.6 will be -5.

In the other hand Math.floor() function rounds a number down to the nearest integer, always moving towards negative infinity.
for example: 5.95 will be 5 and -4.1 output will be -5.

What is the significance of (maximum - minimum + 1)? What does this expression represent?
This expression represents (maximum - minimum + 1) is used to adjust the range of values generated by the random number, ensuring that the range is inclusive of both the minimum and maximum values.
Math.random() * (100 - 1 + 1) Generates a value between 0 and 100 (exclusive)
Math.floor(Math.random() * (100 - 1 + 1)) Generates a value between 0 and 99 (inclusive)
to get maximum we use +1.

What is the significance of ... + minimum?
The ... + minimum part of the expression is crucial because it shifts the generated random number to ensure that the range starts at the minimum value rather than at 0.
Math.floor(Math.random() * (100 - 1 + 1)) + 1 it will be from 1 to 100 and without Math.floor(Math.random() * (100 - 1 + 1)) will be 0 to 99.

What is the range of values that could be assigned to num?
The value will be range between 1 to 100.

To test your understanding, how would you write an expression (without using any variable in the expression) that can yield a random integer between -5 and 5 (including both -5 and 5)?
Math.floor(Math.random() * 11) - 5
To generate a random integer between -5 and 5 (inclusive), we need 11 possible integer values. */
3 changes: 2 additions & 1 deletion 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?
We don't want the computer to run these 2 lines - how can we solve this problem?
// Buy using comments syntax which is 2 slash
4 changes: 3 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// 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);
// I have change const to let, It won't let you to reassign, it will say error if we use const.
4 changes: 3 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// 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}`);
// variable should declare before console.log.
13 changes: 10 additions & 3 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);

const cardNumber = 4533787178994213;
const last4Digits = String(cardNumber).slice(-4); // convert to string and extract last 4 digit
console.log(last4Digits); //print4213
// 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

//IN line 1 their was't any string that may be the reason.
//With out changing code its says TypeError

/* how would you modify the code (through type conversion) so that it can still extract the last 4 digits from its value.
To use slice() method we need to use string.

7 changes: 5 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";
console.log(twelveHourClockTime);
console.log(twentyFourHourClockTime);
//it says syntax error because variable should start with letter or underscore.
15 changes: 14 additions & 1 deletion Sprint-1/3-mandatory-interpret/1-percentage-change.js
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 @@ -12,11 +12,24 @@ 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
//ans- There are 5 function calls:
//carPrice = Number(carPrice.replaceAll(",", ""));
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
//console.log(`The percentage change is ${percentageChange}`);

// 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?
// ans: It was SyntaxError: missing ) which is replaceAll(",", "")comma separate argument.

// c) Identify all the lines that are variable reassignment statements
// ans: carPrice = Number(carPrice.replaceAll(",", ""));
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",",""));

// d) Identify all the lines that are variable declarations
// ans: 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?
// ans: The expression Number(carPrice.replaceAll(",", "")) is used to convert a formatted string representation of a number (with commas) into an actual numeric value.
//Basically Number(carPrice.replaceAll(",","")) remove comma, convert strings in to number.
20 changes: 19 additions & 1 deletion Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,39 @@ const totalMinutes = (movieLength - remainingSeconds) / 60;

const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
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?
// ans:There are 6 variable declaration which are const.

// b) How many function calls are there?
//ans: There is only 1 function call which is 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
//ans: This is called remainder operator which divide the length of movie 8784 by 60 and place the remaining second which is 24.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// ans: It means movie length subtract the remaining seconds and use remainder operator like (8784-24=8760/60) which 146.
Copy link
Contributor

Choose a reason for hiding this comment

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

You gave a literal translation of the code, but it does not quite explain what the expression (movieLength - remainingSeconds) / 60 does.

Can you describe in terms of whole minute, or use ChatGPT to find out how else the code can be described?

Copy link
Author

Choose a reason for hiding this comment

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

hi, Cjyuan, thank you for your review, I have changed the ans as it was mention. Can you please review.

// Also, this line interpret the expression calculates total number of complete minutes in movieLength (which is in seconds).
//movieLength-remainingSecond
// remainingSEcond = movieLength % 60

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// anns: Better name for this variable will be "formattedMovieDuration" because it is more descriptive, clearly indicating that the value is a formatted string representing the movie's duration.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer

const movieLength = 2080; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;

const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;
const timeString = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
console.log(timeString);
// It does work with another value as i change the movie length, it does work.
25 changes: 22 additions & 3 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,27 @@ console.log(`£${pounds}.${pence}`);
// This program takes a string representing a price in pence
// The program then builds up a string representing the price in pounds

// You need to do a step-by-step breakdown of each line in this program
// Try and describe the purpose / rationale behind each step
// with the value "399p"You need to do a step-by-step breakdown of each line in this program
// Try and describe the purpose / rationale behind each stepconst penceString = "399p";

// 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
// this one initializes the penceString value with of 399 in pence.

//const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
//In this line,the last character of penceString variable will be remove and converts price string like "399p" will be 399.
//

//const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
//This line make sure the string "penceStringWithoutTrailingP" has 3 character and also adding "0" if necessary and it also penceStringWithoutTrailingP is "99" also padded to "099".

//const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
//This line remove the part of the string which is pound and substring from the start(0) to characters before the end paddedPenceNumberString.
Copy link
Contributor

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 "... to characters before the end paddedPenceNumberString"?


//const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0");
//This line of code extracts 2 characters of the string "paddedPenceNumberString" also pads the results to make sure it has 2 characters by adding 0 to format properly.
//Calculate the starting index of the last two characters of 399 (length - 2) input will be 99.
// It also Extract everything from that point to the end of the string using substring().

//console.log(`£${pounds}.${pence}`);
// It will print price in pound and pence in format. which will print 3.99
Loading