Skip to content
Open
Show file tree
Hide file tree
Changes from 15 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
3 changes: 3 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,6 @@ 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 3 is updating the value of the count variable by incrementing its current value by 1.
// = sign is an assignment operator that transfers its value on the right (count + 1) to the variable on the left (count).
5 changes: 2 additions & 3 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 = ``;

// https://www.google.com/search?q=get+first+character+of+string+mdn
let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);

console.log(initials);
11 changes: 6 additions & 5 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

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}`);
const base = filePath.slice(lastSlashIndex + 1); // from filePath it will show file.txt
console.log(`The base part of ${filePath} is ${base}`); // it will print the base part of the filePath variable.

// 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); // from FilePath it will show /Users/mitch/cyf/Module-JS1/week-1/interpret.
console.log(`The dir part of ${filePath} is ${dir}`); // it will print the dir part of the filePath variable.

// https://www.google.com/search?q=slice+mdn
const ext = base.slice(base.lastIndexOf(".")); // from base it will show .txt
console.log(`The ext part of ${filePath} is ${ext}`); // it will print the ext part of the filePath variable.
15 changes: 15 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,18 @@ 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);

// num is a random integer between 1 and 100.
/*
Breaking down what happens in the expression:

- Math.random() generates a random floating-point number between 0 and 1.

-Multiplying this by (maximum - minimum + 1) scales it to a range of 0 to 100.

-Math.floor() rounds down the result to the nearest integer number.

-Finally, adding minimum shifts the range to be between 1 and 100.

*/
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?
*/
5 changes: 3 additions & 2 deletions Sprint-1/2-mandatory-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;
age = age + 1;
let age = 30; // Change const to let to allow reassignment
age += 1; // Increment age by 1
console.log(age);
9 changes: 9 additions & 0 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";

/* The error:
The variable cityOfBirth should be declared before it is used in the console.log statement.
*/

/* The correct code:
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
*/
15 changes: 15 additions & 0 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
/* The old code:
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
*/

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

/* My prediction: The code will give an error because the minus sign(-4) and slice works only with strings, not numbers.

Consideration: The error is a TypeError: cardNumber.slice is not a function. mostly it was because I predicted that numbers do not have a slice method. About the minus sign, I have learned about positive and negative indexing in strings, now I can see the difference.


To fix this, we need to convert cardNumber to a string before calling the slice method.
*/

// Updated code:
const cardNumber = 4533787178994213;
const last4DigitsCorrected = cardNumber.toString().slice(-4);
console.log(last4DigitsCorrected);
13 changes: 12 additions & 1 deletion Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
/*
The old code:
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const 24hourClockTime = "08:53";
console.log(24hourClockTime);*/

// It is giving a syntax error
// The reason is that variable names cannot start with a number

// Updated code:
const hourClockTime12 = "20:53";
const hourClockTime24 = "08:53";
console.log(hourClockTime24, hourClockTime12);
36 changes: 36 additions & 0 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,46 @@ console.log(`The percentage change is ${percentageChange}`);

// 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 in this file:
// line 4: carPrice.replaceAll(",", "")
// line 4: Number(carPrice.replaceAll(",", ""))
//line 5: priceAfterOneYear.replaceAll("," "")
// line 5: Number(priceAfterOneYear.replaceAll("," ""))
// line 9: 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?
/*
It's a SyntaxError in line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));.
The error is occurring because there is a missing comma between the two arguments of the replaceAll function here ("," "").
To fix this problem, we need to add a comma between the two arguments: replaceAll(",", "").
*/



// c) Identify all the lines that are variable reassignment statements
/*reusing the same variable carPrice but updating its value:
carPrice = Number(carPrice.replaceAll(",", ""));

reusing the same variable priceAfterOneYear but updating its value:
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
*/



// d) Identify all the lines that are variable declarations
/*declaring a new variable carPrice:
let carPrice = "10,000";

declaring a new variable priceAfterOneYear:
let priceAfterOneYear = "8,543";
*/
Copy link

Choose a reason for hiding this comment

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

priceDifference and percentageChange are also variables. Although their values are fixed after assignment due to the use of the const keyword, they are still considered variables in JavaScript.

Copy link
Author

Choose a reason for hiding this comment

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

@tenzyns, I missed these two🤔😥 I will add them now.
Thanks a lot.




// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
/*
The expression Number(carPrice.replaceAll(",", "")) is converting the carPrice string into a number.
The purpose of this expression is to get a numeric value for carPrice that can be used for calculations.
*/
38 changes: 38 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,52 @@ 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:
1.Line 1 : const movieLength.
2.Line 3 : const remainingSeconds.
3.Line 4 : const totalMinutes.
4.Line 6 : const remainingMinutes.
5.Line 7 : const totalHours.
6.Line 9 : const result.
*/

// b) How many function calls are there?

/* There is one function call:
Line 10 : console.log().
*/

// c) Using documentation, explain what the expression movieLength % 60 represents

/* The movieLength shows the length of the movie in seconds, the operand % is remaining (Modulo) returns the remaining of the movieLength divided by 60(to convert sec into minutes) but it only gives the remaining of this operation.
*/

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

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

/*
The expression gives the completed total minutes of the movie, by subtract the movie length in sec from the remaining seconds and divide the result by 60 to convert it to minutes
Copy link

Choose a reason for hiding this comment

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

You're correct. Just to slightly rearrange the wording, you could say 'subtract the remainingSeconds from the movieLength...'

Copy link
Author

Choose a reason for hiding this comment

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

@tenzyns ,
Okay, I'll fix it right now 👍👍

*/

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

/*
The variable result shows(display) the total length of the movie in three sections (Hours:Minutes:seconds) in string format.

I can think of "MovieDuration" as a better name because it gives more description to the output.
Copy link

Choose a reason for hiding this comment

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

It's a convention to start a variable name with lowercase, though not a rule.

Copy link
Author

Choose a reason for hiding this comment

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

@tenzyns, I just heard this info on a video a couple of hours ago😁😁 it's like writing code.

*/

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

/*
I did experimenting the code by putting different value to the movieLength and it works just fine.

test numbers and its outcomes:
1. 8689693484 sec >> 2413803:44:44
2. 12 sec >> 0:0:12
3. 0 sec >> 0:0:0
4. -700 sec >> 0:-11:-40

As the results show the code is working fine even with unexpected negative numbers or large numbers.
*/
13 changes: 12 additions & 1 deletion Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);
//

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
Expand All @@ -24,4 +25,14 @@ 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 penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1): represent to takes a substring from index 0 up to (but not including) penceString.length - 1.

// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): assign to ensures the numeric string is at least 3 characters long by adding leading zeros if needed.

// 4. const pounds = paddedPenceNumberString.substring( 0,paddedPenceNumberString.length - 2): Takes the substring from the start up to (but not including) the last two characters.

// 5. substring(length - 2) : use to returns the last two characters (the pence digits). padEnd(2, "0") would append trailing zeros if the result were shorter than 2.

// 6. console.log(`£${pounds}.${pence}`): assign to display the price in a human-readable pounds-and-pence format (£x.yy)
5 changes: 5 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?

Answer: A pop up window displaying a text (Hello World!) will appear on the top and an OK button. It's a temporarily effect, but it required a user interaction to press OK before continuing.

Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`.

What effect does calling the `prompt` function have?
What is the return value of `prompt`?

Answer: A pop up window appears with a title and a question (What is your name?), also an answering space area. At the right down of the window there are two buttons to the user, Ok and cancel.
After typing myName and press OK the console display the answer(myName).
19 changes: 18 additions & 1 deletion Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,28 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?

-The output : ƒ log() { [native code] }

Now enter just `console` in the Console, what output do you get back?

-The output : console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} + a lot of functions under it.

Try also entering `typeof console`

Answer the following questions:
-The output : object

-Answer the following questions:

What does `console` store?

-Answer : console is an object with debugging methods.
It does not store variables or result. It provide methods for outputting , data , object and more.

What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?

Answer:
-console is a built-in object.
-log and assert are properties of that object, specifically functions (methods).
-The console.assert() static method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.
Copy link

Choose a reason for hiding this comment

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

for console.assert(condition, message); you provide a boolean expression as the 1st argument and message string if false.

Copy link
Author

Choose a reason for hiding this comment

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

@tenzyns ,
Do I need to rephrase my expression, for example, [if the condition is false, it prints an error message in the console. If the condition is true, it doesn’t output anything. Should I avoid using Boolean words (true, false)?

Copy link

Choose a reason for hiding this comment

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

@Fares-Bakhet, Apologies, didn't get notified of this. No you don't need to rephrase. Just to be aware of the what arguments it accepts.

-The console.log() static method outputs a message to the console.
-'.' means use this method from this object.