Skip to content

Commit 8e582d6

Browse files
complete exercises
1 parent d28242a commit 8e582d6

13 files changed

Lines changed: 130 additions & 16 deletions

File tree

Sprint-2/1-key-exercises/1-count.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ count = count + 1;
44

55
// Line 1 is a variable declaration, creating the count variable with an initial value of 0
66
// Describe what line 3 is doing, in particular focus on what = is doing
7+
// line 3 increments count by 1 and reassigns updated value back to the count variable
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
const firstName = "Creola";
2-
const middleName = "Katherine";
3-
const lastName = "Johnson";
1+
let firstName = "Creola";
2+
let middleName = "Katherine";
3+
let lastName = "Johnson";
44

55
// Declare a variable called initials that stores the first character of each string.
66
// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution.
77

8-
const initials = ``;
8+
let firstName = "Chris";
9+
let middleName = "Kevin";
10+
let lastName = "Jones";
911

12+
let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`;
13+
14+
console.log(initials);
1015
// https://www.google.com/search?q=get+first+character+of+string+mdn

Sprint-2/1-key-exercises/3-paths.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,31 @@ console.log(`The base part of ${filePath} is ${base}`);
1717
// Create a variable to store the dir part of the filePath variable
1818
// Create a variable to store the ext part of the variable
1919

20-
const dir = ;
21-
const ext = ;
20+
const filePath = "Documents/work/report.pdf";
2221

23-
// https://www.google.com/search?q=slice+mdn
22+
const lastSlashIndex = filePath.lastIndexOf("/");
23+
const lastDotIndex = filePath.lastIndexOf(".");
24+
25+
const dir = filePath.slice(0, lastSlashIndex);
26+
const ext = lastDotIndex === -1 ? "" : filePath.slice(lastDotIndex);
27+
28+
console.log(dir);
29+
console.log(ext);
30+
31+
const lastSlashIndex = filePath.lastIndexOf("/");
32+
const lastDotIndex = filePath.lastIndexOf(".");
33+
34+
// Store the directory
35+
const dir = filePath.slice(0, lastSlashIndex);
36+
37+
// Store the extension
38+
const ext = lastDotIndex === -1 ? "" : filePath.slice(lastDotIndex);
39+
40+
console.log(dir);
41+
console.log(ext);
42+
lastDotIndex;
43+
44+
console.log(`The dir part of ${filePath} is ${dir}`);
45+
console.log(`The ext part of ${filePath} is ${ext}`);
46+
47+
// https://www.google.com/search?q=slice+mdn

Sprint-2/1-key-exercises/4-random.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
77
// Try breaking down the expression and using documentation to explain what it means
88
// It will help to think about the order in which expressions are evaluated
99
// Try logging the value of num and running the program several times to build an idea of what the program is doing
10+
11+
// Print the random number to the console
12+
console.log(num);
13+
14+
//num is a random whole number between 1 and 100.
15+
16+
//Math.random() creates a random decimal.
17+
//Multiply by the range size.
18+
// Math.floor() rounds down.
19+
// Add minimum so it starts at 1.

Sprint-2/2-mandatory-errors/0.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
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?
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?*/

Sprint-2/2-mandatory-errors/1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// trying to create an age variable and then reassign the value by 1
22

3-
const age = 33;
3+
let age = 33;
44
age = age + 1;

Sprint-2/2-mandatory-errors/2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Currently trying to print the string "I was born in Bolton" but it isn't working...
22
// what's the error ?
33

4-
console.log(`I was born in ${cityOfBirth}`);
54
const cityOfBirth = "Bolton";
5+
console.log(`I was born in ${cityOfBirth}`);

Sprint-2/2-mandatory-errors/3.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
2+
const last4Digits = cardNumber.toString().slice(-4);
33

44
// The last4Digits variable should store the last 4 digits of cardNumber
55
// However, the code isn't working
66
// Before running the code, make and explain a prediction about why the code won't work
77
// Then run the code and see what error it gives.
88
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
99
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
10+
11+
//I predict the code will not work because cardNumber is a number and .slice() is used on strings (or arrays), not numbers.
12+
//When you run it, JavaScript gives an error which is TypeError: cardNumber.slice is not a function
13+
//the reason is because creates a number, and numbers do not have a .slice() method.
14+
//the fix is converting the number to a string
15+
16+
console.log(last4Digits);

Sprint-2/2-mandatory-errors/4.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
const 12HourClockTime = "8:53pm";
2-
const 24hourClockTime = "20:53";
1+
const twelveHourClockTime = "8:53pm";
2+
const twentyFourHourClockTime = "20:53";
3+
4+
//the code will fail because variable names cannot start with a number

Sprint-2/3-mandatory-interpret/1-percentage-change.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,33 @@ console.log(`The percentage change is ${percentageChange}`);
2020
// d) Identify all the lines that are variable declarations
2121

2222
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
23+
24+
//answers
25+
26+
/* a) there are there five
27+
carPrice.replaceAll(",", "")
28+
Number(carPrice.replaceAll(",", ""))
29+
Number(priceAfterOneYear.replaceAll(",", ""))
30+
console.log(...) -
31+
(priceDifference / carPrice) * 100 - line 7 (this is an expression that involves division and multiplication, but it does not involve a function call)*/
32+
33+
34+
/* b) priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
35+
The issue is a syntax mistake in the brackets/quotes, and JavaScript throws a SyntaxError before running.
36+
37+
the fix would be priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
38+
39+
*/
40+
41+
/* c)carPrice = Number(carPrice.replaceAll(",", ""));
42+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));*/
43+
44+
/* d) let carPrice = "10,000";
45+
let priceAfterOneYear = "8,543";
46+
const priceDifference = carPrice - priceAfterOneYear;
47+
const percentageChange = (priceDifference / carPrice) * 100;*/
48+
49+
/* e)carPrice is a string:
50+
replaceAll(",", "") removes commas:
51+
Number(...) converts string to a number:
52+
Convert a formatted string number into a real numeric value so calculations can work.*/

0 commit comments

Comments
 (0)