Skip to content

Commit 200e06c

Browse files
committed
Completed Sprint 1 coursework
1 parent 237a6b1 commit 200e06c

File tree

14 files changed

+102
-7
lines changed

14 files changed

+102
-7
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ 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 taking the current value to count, then make the count to new value
7+
8+
Answer:
9+
10+
//Line 3 is assignment statement,its taking the current value to count,then make the count to a new value.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ let lastName = "Johnson";
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-
let initials = ``;
8+
Answer:
9+
let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);
910

1011
// https://www.google.com/search?q=get+first+character+of+string+mdn
1112

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ console.log(`The base part of ${filePath} is ${base}`);
1616

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
19-
20-
const dir = ;
21-
const ext = ;
19+
Anwer:
20+
const dir = filePath.slice(0, lastSlashIndex);
21+
const ext = base.slice(lastDotIndex + 1);
22+
console.log(`The dir part of ${filePath} is ${dir}`);
23+
console.log(`The ext part of ${filePath} is ${ext}`);
2224

2325
// https://www.google.com/search?q=slice+mdn

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@ 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+
Answer:
12+
13+
//num is a random number between 1 and 100.
14+
15+
//Math.random() makes a number between 0 and 1.
16+
17+
//times (maximum - minimum + 1) makes it bigger.
18+
19+
//Math.floor cuts off the decimal.
20+
21+
//+ minimum makes sure the random number starts at 1 instead of 0
22+

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
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?
2+
We don't want the computer to run these 2 lines - how can we solve this problem?
3+
4+
Answer:
5+
//We can turn those lines into comments by putting // at the start.
6+
//Then computer ignores them but humans can still read/see them.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@
22

33
const age = 33;
44
age = age + 1;
5+
6+
Answer:
7+
8+
//This gives an error because we used const, which can't be changed.
9+
// If we want to update the value, we should use let instead of const.
10+
let age = 33;
11+
age = age + 1;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@
33

44
console.log(`I was born in ${cityOfBirth}`);
55
const cityOfBirth = "Bolton";
6+
7+
8+
Answer:
9+
10+
//Error because cityOfBirth is used before it is declared.
11+
//We need to define the variable first, then use it.
12+
13+
const cityOfBirth = "Bolton";
14+
console.log(`I was born in ${cityOfBirth}`);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,12 @@ const last4Digits = cardNumber.slice(-4);
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+
Answer:
12+
13+
//I guessed it breaks because slice is for strings but cardNumber is a number.
14+
//When I try it, the error says slice is not a function (so I was right).
15+
//Fix: change the number to string first, then slice works.
16+
const cardNumber = 4533787178994213;
17+
const last4Digits = cardNumber.toString().slice(-4);
18+
console.log(last4Digits); // 4213

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
const 12HourClockTime = "20:53";
2-
const 24hourClockTime = "08:53";
2+
const 24hourClockTime = "08:53";
3+
4+
5+
Answer:
6+
7+
// The first line is wrong cuz variable names can't start with a number.
8+
// We should rename it like hour12ClockTime or something that doesn't start with a digit.
9+
const hour12ClockTime = "20:53";
10+
const hour24ClockTime = "08:53";

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@ console.log(`The percentage change is ${percentageChange}`);
1212
// Read the code and then answer the questions below
1313

1414
// a) How many function calls are there in this file? Write down all the lines where a function call is made
15+
carPrice = Number(carPrice.replaceAll(",", ""));
16+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
17+
console.log(`The percentage change is ${percentageChange}`);
18+
1519

1620
// 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+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
22+
fix;
23+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
1724

1825
// c) Identify all the lines that are variable reassignment statements
26+
carPrice = Number(carPrice.replaceAll(",", ""));
27+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
1928

2029
// d) Identify all the lines that are variable declarations
30+
let carPrice = "10,000";
31+
let priceAfterOneYear = "8,543";
32+
2133

2234
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
35+
replaceAll gets rid of the commas "10,000" becomes "10000".
36+
Number() turns that string into the number 10000.
37+
so we can do maths with it.

0 commit comments

Comments
 (0)