Skip to content
Open
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
// Describe what line 3 is doing, in particular focus on what = reassign a variable using the = + operator.
//Line 1 assigns count = 0. Line 3 uses that value (0) and adds 1, so count becomes 1.
7 changes: 3 additions & 4 deletions Sprint-2/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const firstName = "Creola";
const middleName = "Katherine";
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 = ``;

// https://www.google.com/search?q=get+first+character+of+string+mdn
const 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
8 changes: 5 additions & 3 deletions Sprint-2/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ 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);
const ext = base.slice(base.lastIndexOf("."));

// https://www.google.com/search?q=slice+mdn
// https://www.google.com/search?q=slice+mdn
console.log(`The dir part of ${filePath} is ${dir}`);
console.log(`The ext part of ${filePath} is ${ext}`);
9 changes: 9 additions & 0 deletions Sprint-2/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ 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

// In this exercise, num represent the value integer number between 1 to 100.
//Math.random generates a random decimal number from 0 up to 1, but not including, 1.
// down to the nearest whole number (integer).

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.

Line 13 starts in the middle of a sentence. I think it is about Math.floor. Please finish it.

Two steps are also missing. Math.random() * (maximum - minimum + 1) is Math.random() * 100. That gives a decimal from 0 up to 100. Math.floor then makes it a whole number from 0 to 99. So what does + minimum do at the end?

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 write the answer

// i think Changes the whole range to be greater by the minimum value
//Ensures the value never comes less than 1.
//Running the program several times generate the whole number(integer) like (1,10,15,44,66,55) several times between 1 to 100 all 100 number has a equal 1% chance to appear(generate).
console . log (num)
//
5 changes: 3 additions & 2 deletions 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?
/*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?
for single line we used // and for multiple line use */
9 changes: 8 additions & 1 deletion Sprint-2/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
// 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);

/* In this case age is not const means variable is not reassigned so that,
/*we throws a TypeError: Assignment to constant variable*/
//I try by let where console.log(age) shows Running] node "c:\Users\Desktop\code your future\Module-Onboarding\Module-JavaScript-Fundamentals\Sprint-2\Sprint-2\2-mandatory-errors\1.js"
//34.
3 changes: 3 additions & 0 deletions Sprint-2/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

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


/*we need to put declare variables(const cityOfBirth = "Bolton";) in first line after using expression console.log.

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 reason is right. The variable must exist before console.log uses it. Now swap lines 4 and 5, so the file runs.

Also, write the error name. Run the file now, before you swap the lines. Is it a SyntaxError, a TypeError or a ReferenceError?

9 changes: 7 additions & 2 deletions Sprint-2/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = String(cardNumber).slice(-4);
console.log (last4Digits);

// 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?
// 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
//Card number is a number not a string so it will throw typeerror.
// i did not think slice method can not run in number method so that i change it in string
// when i put capital letter ReferenceError: string is not defined so that nothing last4Digits not showed
//in the terminal String is the function but string is the normal text.
8 changes: 6 additions & 2 deletions Sprint-2/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";
const HourClockTime12 = "8:53pm";
const hourClockTime24 = "20:53";


/*variable is not start with number show syntaxerror.*/
// i just move the number at the end.
31 changes: 22 additions & 9 deletions 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

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

// c) Identify all the lines that are variable reassignment statements

// d) Identify all the lines that are variable declarations

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
/* there has five function in line 4 and and console . log also has one
line 4 and 5 has two Number() function
line 4 and 5 has two replaceALL() function
line 10 has one console . log ()

/* 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?
In line five error shown in replaceAll("," "")); missing comma before last two comma(",","").

/* c) Identify all the lines that are variable reassignment statements
line 4 carPrice = Number(carPrice.replaceAll(",", ""));
line 5 priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
As per the carPrice and priceAfterOneYear has been declared in line 1 and 2 by using let variable.

/*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 throwout all the commas from the string and number became numerical value.
13 changes: 11 additions & 2 deletions Sprint-2/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const movieLength = 8784; // length of movie in seconds

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

Expand All @@ -8,18 +7,28 @@ 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?
/* line 1 const movieLength line 3 const remainingSeconds line 4 const totalMinutes
line 6 const remainingMinutes line 7 const totalHours line 9 const result

// b) How many function calls are there?
/* there are one function
console.log(result);

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
/*The remainder (%) operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.

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 is what % does in general. In this program, movieLength % 60 gives 24. Are those 24 seconds, minutes or hours? Write what the 24 means in this program.


// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
/* This expression convert movies time second into minutes dividend by 60 second.

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.

Dividing by 60 is the second step. First, (movieLength - remainingSeconds) takes the 24 leftover seconds away. Why do that before dividing by 60? Try 8784 / 60 in node and look at the result.


// e) What do you think the variable result represents? Can you think of a better name for this variable?
/* The variable represent the movieLength in second,minutes and hours.
We can change this name as movieTime.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
/* In this section we can only use the positive natural numbers but if we put numbers that divide by 60 without a remainder get the exact time like 10, 20 50.

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 are right that positive whole numbers work. Now try -90 and 90.5 as movieLength. What does the program print each time? Would you show a time like that? Write what you see.

Also, 10, 20 and 50 cannot be divided by 60 with no remainder. Which numbers can?

7 changes: 6 additions & 1 deletion Sprint-2/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

// remove the p from the "399p" and make "399"
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
//padStart() add characters to the starting of the string and provide 3 long characters.
//Because we have to convert pound and pence.
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);
// we take out the 2 character and store the remaining part of the string as the pound.

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
// At last we get the last two characters as the pence.

console.log(`£${pounds}.${pence}`);
//We can display the price of pound and pence like £3.99.

// This program takes a string representing a price in pence
// The program then builds up a string representing the price in pounds
Expand Down
4 changes: 4 additions & 0 deletions Sprint-2/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ 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?
Answer An alert popped up

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?
Answer A popup prompts me to write my name.

What is the return value of `prompt`?
Answer The value i entered in the text box.
7 changes: 7 additions & 0 deletions Sprint-2/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ 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?
Answer The output is ƒ log() { [native code] }

Now enter just `console` in the Console, what output do you get back?
Answer console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, ...} but i can't copy it.


Try also entering `typeof console`
Answer It shows object'

Answer the following questions:

What does `console` store?
console is an object with properties that are function values. These functions are called methods because they belong to the console object.

What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
console.log means "access the log method from the console object" and console.assert means "access the assert method from the console object.