Skip to content

Commit a37b81a

Browse files
London | 26-ITP-Sep | Chandaramani Gaire | Sprint 2 | Sprint 2 Coursework
1 parent 6a65d99 commit a37b81a

14 files changed

Lines changed: 269 additions & 0 deletions

File tree

‎1-key-exercises/2-initials.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const firstName = "Creola";
2+
const middleName = "Katherine";
3+
const lastName = "Johnson";
4+
// Declare a variable called initials that stores the first character of each string.
5+
// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution.
6+
7+
const initials = `${firstName.charAt(0)}${middleName.charAt(0)}${lastName.charAt(0)}`;
8+
console.log(initials)
9+
// // https://www.google.com/search?q=get+first+character+of+string+mdn

‎1-key-exercises/3-paths.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// The diagram below shows the different names for parts of a file path on a Unix operating system
2+
3+
// ┌─────────────────────┬────────────┐
4+
// │ dir │ base │
5+
// ├──────┬ ├──────┬─────┤
6+
// │ root │ │ name │ ext │
7+
// " / home/user/dir / file .txt "
8+
// └──────┴──────────────┴──────┴─────┘
9+
10+
// (All spaces in the "" line should be ignored. They are purely for formatting.)
11+
12+
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
13+
const lastSlashIndex = filePath.lastIndexOf("/");
14+
const base = filePath.slice(lastSlashIndex + 1);
15+
console.log(`The base part of ${filePath} is ${base}`);
16+
17+
// Create a variable to store the dir part of the filePath variable
18+
// Create a variable to store the ext part of the variable
19+
20+
const dir = filePath.slice(0, lastSlashIndex);
21+
const ext = base.slice(base.lastIndexOf("."));
22+
23+
// https://www.google.com/search?q=slice+mdn
24+
console.log(`The dir part of ${filePath} is ${dir}`);
25+
console.log(`The ext part of ${filePath} is ${ext}`);

‎1-key-exercises/4-random.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const minimum = 1;
2+
const maximum = 100;
3+
4+
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
5+
6+
// In this exercise, you will need to work out what num represents?
7+
// Try breaking down the expression and using documentation to explain what it means
8+
// It will help to think about the order in which expressions are evaluated
9+
// Try logging the value of num and running the program several times to build an idea of what the program is doing
10+
11+
// In this exercise, num represent the value integer number between 1 to 100.
12+
//Math.random generates a random decimal number from 0 up to 1, but not including, 1.
13+
// down to the nearest whole number (integer).
14+
// i think Changes the whole range to be greater by the minimum value
15+
//Ensures the value never comes less than 1.
16+
//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).
17+
console . log (num)
18+
//

‎2-mandatory-errors/0.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*This is just an instruction for the first activity - but it is just for human consumption
2+
We don't want the computer to run these 2 lines - how can we solve this problem?
3+
for single line we used // and for multiple line use */

‎2-mandatory-errors/1.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// trying to create an age variable and then reassign the value by 1
2+
3+
let age = 33;
4+
age = age + 1;
5+
6+
console.log(age);
7+
8+
/* In this case age is not const means variable is not reassigned so that,
9+
/*we throws a TypeError: Assignment to constant variable*/
10+
//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"
11+
//34.

‎2-mandatory-errors/2.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Currently trying to print the string "I was born in Bolton" but it isn't working...
2+
// what's the error ?
3+
4+
console.log(`I was born in ${cityOfBirth}`);
5+
const cityOfBirth = "Bolton";
6+
7+
8+
/*we need to put declare variables(const cityOfBirth = "Bolton";) in first line after using expression console.log.

‎2-mandatory-errors/3.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const cardNumber = 4533787178994213;
2+
const last4Digits = String(cardNumber).slice(-4);
3+
console.log (last4Digits);
4+
5+
// The last4Digits variable should store the last 4 digits of cardNumber
6+
// However, the code isn't working
7+
// Before running the code, make and explain a prediction about why the code won't work
8+
// Then run the code and see what error it gives.
9+
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
10+
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
11+
//Card number is a number not a string so it will throw typeerror.
12+
// i did not think slice method can not run in number method so that i change it in string
13+
// when i put capital letter ReferenceError: string is not defined so that nothing last4Digits not showed
14+
//in the terminal String is the function but string is the normal text.

‎2-mandatory-errors/4.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const 12HourClockTime = "8:53pm";
2+
const 24hourClockTime = "20:53";
3+
/*variable is not start with number show syntaxerror.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
let carPrice = "10,000";
2+
let priceAfterOneYear = "8,543";
3+
4+
carPrice = Number(carPrice.replaceAll(",", ""));
5+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",",""));
6+
7+
const priceDifference = carPrice - priceAfterOneYear;
8+
const percentageChange = (priceDifference / carPrice) * 100;
9+
10+
console.log(`The percentage change is ${percentageChange}`);
11+
12+
// Read the code and then answer the questions below
13+
14+
// a) How many function calls are there in this file? Write down all the lines where a function call is made
15+
/* there has five function in line 4 and and console . log also has one
16+
line 4 and 5 has two Number() function
17+
line 4 and 5 has two replaceALL() function
18+
line 10 has one console . log ()
19+
20+
/* 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?
21+
In line five error shown in replaceAll("," "")); missing comma before last two comma(",","").
22+
23+
/* c) Identify all the lines that are variable reassignment statements
24+
line 4 carPrice = Number(carPrice.replaceAll(",", ""));
25+
line 5 priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
26+
As per the carPrice and priceAfterOneYear has been declared in line 1 and 2 by using let variable.
27+
28+
/*d) Identify all the lines that are variable declarations
29+
line 1 let carPrice = "10,000";
30+
line 2 let priceAfterOneYear = "8,543";
31+
line 7 const priceDifference = carPrice - priceAfterOneYear;
32+
line 8 const percentageChange = (priceDifference / carPrice) * 100;
33+
34+
/* e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
35+
replaceALL throwout all the commas from the string and number became numerical value.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const movieLength = 8784;
2+
const remainingSeconds = movieLength % 60;
3+
const totalMinutes = (movieLength - remainingSeconds) / 60;
4+
5+
const remainingMinutes = totalMinutes % 60;
6+
const totalHours = (totalMinutes - remainingMinutes) / 60;
7+
8+
const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
9+
console.log(result);
10+
//
11+
12+
// For the piece of code above, read the code and then answer the following questions
13+
14+
// a) How many variable declarations are there in this program?
15+
/* line 1 const movieLength line 3 const remainingSeconds line 4 const totalMinutes
16+
line 6 const remainingMinutes line 7 const totalHours line 9 const result
17+
18+
// b) How many function calls are there?
19+
/* there are one function
20+
console.log(result);
21+
22+
// c) Using documentation, explain what the expression movieLength % 60 represents
23+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
24+
/*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.
25+
26+
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
27+
/* This expression convert movies time second into minutes dividend by 60 second.
28+
29+
// e) What do you think the variable result represents? Can you think of a better name for this variable?
30+
/* The variable represent the movieLength in second,minutes and hours.
31+
We can change this name as movieTime.
32+
33+
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
34+
/* 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.

0 commit comments

Comments
 (0)