Skip to content
Open
Show file tree
Hide file tree
Changes from 16 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: 2 additions & 1 deletion Sprint-2/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ let count = 0;
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 add 1 to value of count and store result back in count,
// in this line = is not mean (is equal to ), It is a assignment
8 changes: 2 additions & 6 deletions Sprint-2/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
const firstName = "Creola";
const middleName = "Katherine";
const lastName = "Johnson";
const initials = `${firstName.charAt(0)}` + `${middleName.charAt(0)}` + `${lastName.charAt(0)}`;
console.log(`${initials}`);

// 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 = ``;

// https://www.google.com/search?q=get+first+character+of+string+mdn
12 changes: 4 additions & 8 deletions Sprint-2/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ 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
const dir = filePath.slice(0,`${lastSlashIndex}`-1);

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.

Run this file and read the dir line carefully. It prints .../week-1/interpre, and the folder is called interpret. One character is being lost. What is the - 1 doing at the end of this line, and does slice need 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.

The - 1 makes the slice stop one character too early, which removes the t from interpret. It isn’t needed. thank you for mention .

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.

That's it, the dir line prints the full folder name now.

const ext = filePath.slice(filePath.lastIndexOf("."));
console.log(`The dir part of ${filePath} is ${dir}`);
console.log(`The ext part of this URL is ${ext}`);
12 changes: 8 additions & 4 deletions Sprint-2/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ 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
//num represents a random whole number between 1 and 100,
//first we calculate value inside parentheses(maximum - minimum +1) = 100 -1 + 1 =100
// math.random return number between 0 and 1
// math.random() * 100 give us a integer 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.

Have a look at this line again. Try running console.log(Math.random() * 100) a few times. Is what you get back an integer? If it were, what would be left for Math.floor to do on the next 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.

Yeah I write wrong, Math.random() * 100 does not give us an integer. It gives us a decimal number between 0 and 100 for example 0.83488 *100 = 83.488. This is why we use Math.floor() . Math.floor(83.488) rounds the number down to 83. Then we add 1, giving us 84.

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.

Yes, exactly that.

// math.floor round down the number
// add 1 to the result
//for example math.floor((0.783 *100))=78 + 1 = 79
10 changes: 8 additions & 2 deletions Sprint-2/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
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?

/*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 can put "//" in the beginning of the line so computer ignore them
// or put them between /* and */
4 changes: 3 additions & 1 deletion Sprint-2/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}`);
//We should use let if we want to change the 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. In 2.js, 3.js and 4.js you also wrote down the message node printed, which is what this section asks for. What did node say here before you changed const to let?

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.

Before I changed const to let node show this error message: (Assignment to constant 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.

That's the one.

5 changes: 3 additions & 2 deletions Sprint-2/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// 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}`);
// This error (Cannot access 'cityOfBirth' before initialization) is because we need to declare the variable before using it.

11 changes: 3 additions & 8 deletions Sprint-2/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
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
const last4Digits = String(cardNumber).slice(-4);
console.log(`${last4Digits}`);
// we get error (cardNumber.slice is not a function) because slice is a String method
5 changes: 3 additions & 2 deletions Sprint-2/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";
const HourClockTime12 = "8:53pm";
const hourClockTime24 = "20:53";
//JavaScript variable names cannot start with a number.
15 changes: 14 additions & 1 deletion Sprint-2/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
/* Number() - line 4 and Line 5
replaceAll() - Line 4 and Line 5
console.log() - Line 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?
/* line 5 , There is a comma missing between "," and ""*/

// c) Identify all the lines that are variable reassignment statements
/* Line 4 :carPrice = Number(carPrice.replaceAll(",", ""));
Line5 : priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
carPrice and priceAfterOneYear were already declared on Line 1 and 2 using let */

// 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?
/*replaceAll remove all the commas from the string and Number convert string to the number*/
13 changes: 12 additions & 1 deletion Sprint-2/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const movieLength = 8784; // length of movie in seconds
const movieLength = 5467; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;
Expand All @@ -12,14 +12,25 @@ 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 declaration in this program.
1- movieLength , 2- remainingSeconds , 3- totalMinutes
4- remainingMinutes , 5- totalHours , 6- result */

// b) How many function calls are there?
/* one function
Line 10 : 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
// Remainder(%) : Return the remainder left over when one operand is divided by a second operand
// console.log(13 % 5 ); 3

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// This expression converts movie length from seconds into minutes

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// Variable result represent the movie length formatted as hours, minutes, seconds ,
// another name could be movieTime

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// This code works for normal positive number representing but for negative number and very big number it is not working

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.

Negative numbers are right. A very big number is fine though, try 999999 and see. Two things worth trying instead: 59, and a number with a decimal like 90.5. What does each one print, and would you show a time that way?

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.

The code work for big number, I try 999999 and it return correct number. However, if movieLength = 90.5, it prints 0:1:30.5. This is not a good way to show a time because the seconds contain decimals. So the code works properly for whole numbers, but not for negative numbers and decimal numbers because the result can contain decimal values.

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.

Good. 59 is worth a try too: it prints 0:0:59, which is the other thing that looks wrong.

16 changes: 16 additions & 0 deletions Sprint-2/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,19 @@ 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);
// create a new variable and put the string without p

// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// padStart() adds characters to the beginning of a string to make it 3 characters long.
// because we need to convert it to pound and pence

// 4. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2);
// We remove the last 2 characters and store the remaining part of the string as pounds.

// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
// Gets the last two characters as pence.”

// 6. console.log(`£${pounds}.${pence}`);
//. Finally combine pound and pence
5 changes: 4 additions & 1 deletion Sprint-2/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ Let's try an example.
In the Chrome console, invoke the function `alert` with one argument, the string `"Hello world!"`;

What effect does calling the `alert` function have?
alert(): shows the message to the users. in this case show "Hello world!" in the box and we can close it or click on the ok.

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 effect does calling the `prompt` function have?
prompt(): returns whatever the user enters. It is provide a textbox that we can type on it.
What is the return value of `prompt`?
The Return value of prompt is the text enter by user. in this example return name that we write in the box.
8 changes: 7 additions & 1 deletion Sprint-2/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ In this activity, we'll explore some additional concepts that you'll encounter i
Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?
ƒ log() { [native code] }

Now enter just `console` in the Console, what output do you get back?
console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}

Try also entering `typeof console`
'object'

Answer the following questions:

What does `console` store?
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
console stores object. It stores a collection of methods that allow you to interact with the browser's developer console.
what does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
access the log property/method of the console object.
access the assert method of the console object.
Loading