Skip to content

Commit 4b1b82b

Browse files
committed
sprint 2 solutions
1 parent b41a825 commit 4b1b82b

14 files changed

Lines changed: 103 additions & 4 deletions

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,12 @@ let count = 0;
33
count = count + 1;
44

55
// Line 1 is a variable declaration, creating the count variable with an initial value of 0
6-
// Describe what line 3 is doing, in particular focus on what = is doing
6+
// Describe what line 3 is doing, in particular focus on what = means
7+
//
8+
= means "put this value in the box."
9+
10+
Line 3 is saying: take whatever's currently in the `count` box (that's `0`), add 1 to it, and shove the answer back in the same box.
11+
12+
So it goes: `0 + 1 = 1`, and now `count` holds `1` instead of `0`.
13+
14+
That's really it — `=` in code just means "store this," not "these are the same thing."

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,12 @@ const lastName = "Johnson";
77

88
const initials = ``;
99

10-
// https://www.google.com/search?q=get+first+character+of+string+mdn
10+
// https://www.google.com/search?q=get+first+character+of+string
11+
//
12+
grab the first letter of each string using `[0]`, and glue them together:
13+
14+
```javascript
15+
const initials = firstName[0] + middleName[0] + lastName[0];
16+
```
17+
18+
`firstName[0]` grabs whatever's sitting at position 0 in the string — the first letter — so that's `"C"`. Same deal for the other two, giving you `"K"` and `"J"`. The `+` just sticks the three letters together into one string, so you end up with `"CKJ"`

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ console.log(`The base part of ${filePath} is ${base}`);
2020
const dir = ;
2121
const ext = ;
2222

23-
// https://www.google.com/search?q=slice+mdn
23+
// https://www.google.com/search?q=slice+mdn
24+
25+
const dir = filePath.slice(0, lastSlashIndex);
26+
const ext = base.slice(base.lastIndexOf("."));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ 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+
Math.random() gives you a random decimal between 0 and just-under-1 — like 0.628....
12+
Then you multiply that by 100 (which is maximum - minimum + 1), stretching it out into a decimal between 0 and just-under-100.
13+
Math.floor() chops off everything after the decimal point, rounding down, you've got a whole number from 0 to 99.
14+
+ minimum (which is +1) shifts the whole thing up by one, so instead of 0–99 you get 1–100.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
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+
by adding // infront of each line before writing a text/code

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

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

33
const age = 33;
44
age = age + 1;
5+
6+
const means "this value is locked in, you can't change it later." So when line 2 tries to reassign age, JavaScript throws:
7+
8+
TypeError: Assignment to constant variable.
9+
//instead use 'let' i.e let age = 33;
10+
age = age + 1;
11+
12+
console.log(age); // 34
13+
const for stuff that stays the same, let for stuff that's meant to change such as age.

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

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

44
console.log(`I was born in ${cityOfBirth}`);
55
const cityOfBirth = "Bolton";
6+
7+
ReferenceError: Cannot access 'cityOfBirth' before initialization
8+
JavaScript reads top to bottom, so at the point it hits that console.log, cityOfBirth doesn't exist yet.

‎Sprint-2/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+
cardNumber is just a plain number, and .slice() only exists on strings and arrays, not numbers
12+
'TypeError: cardNumber.slice is not a function'
13+
numbers just don't have a .slice() method built in.
14+
15+
const cardNumber = 4533787178994213;
16+
const last4Digits = cardNumber.toString().slice(-4);
17+
18+
console.log(last4Digits); // "4213"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
const 12HourClockTime = "8:53pm";
22
const 24hourClockTime = "20:53";
3+
4+
it'll throw a SyntaxError before anything executes, because both variable names start with a number.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ 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+
a) 5 calls across 3 lines
25+
b) error is on line 5, there's a missing comma between ',' and ""
26+
c)reassignments on line 4 and 5
27+
d) declarations on lines 1,2,7,8
28+
e) carPrice starts as the string "10,000" — you can't do maths on that as-is, the comma would mess things up. So first, .replaceAll(",", "") strips out the comma, leaving "10000". Then Number(...) converts that string into an actual number, 10000, that you can subtract and divide like normal.

0 commit comments

Comments
 (0)