Skip to content

Commit a3ef048

Browse files
committed
Complete Sprint 3 mandatory coursework
1 parent d28242a commit a3ef048

10 files changed

Lines changed: 183 additions & 36 deletions

File tree

‎Sprint-3/1-key-errors/0.js‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// str is a parameter in the function capitalise, so there will likely be a syntax error when trying to declare a second variable (let str) with the same name in the same scope.
3+
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
67

8+
9+
//function capitalise(str) {
10+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
11+
//return str;
12+
//}
13+
14+
// =============> write your explanation here
15+
// Error message: SyntaxError: Identifier 'str' has already been declared
16+
// A parameter is already a variable declared inside the function's scope.
17+
// let cannot declare a second variable with the same name in the same scope, so
18+
// JavaScript rejects the whole file before running anything. That is why the
19+
// error appears even before capitalise is called.
20+
21+
// =============> write your new code here
722
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
23+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
924
return str;
1025
}
1126

12-
// =============> write your explanation here
13-
// =============> write your new code here
27+
console.log(capitalise("hello")); // Hello

‎Sprint-3/1-key-errors/1.js‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,37 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// I predict a SyntaxError, because decimalNumber is the parameter and is declared
6+
// again inside the function with const.
7+
// Even after fixing that, console.log(decimalNumber) is outside the function, where
8+
// decimalNumber does not exist, so it would cause a ReferenceError.
59

610
// Try playing computer with the example to work out what is going on
711

12+
// function convertToPercentage(decimalNumber) {
13+
// const decimalNumber = 0.5;
14+
// const percentage = `${decimalNumber * 100}%`;
15+
//
16+
// return percentage;
17+
// }
18+
//
19+
// console.log(decimalNumber);
20+
21+
// =============> write your explanation here
22+
// First error: SyntaxError: Identifier 'decimalNumber' has already been declared.
23+
// The parameter already declares decimalNumber in the function's scope, so const
24+
// cannot declare it again. The hardcoded 0.5 would also ignore whatever value was passed in.
25+
//
26+
// Second error (after removing the const line): ReferenceError: decimalNumber is not defined.
27+
// Parameters only exist inside the function's local scope. Outside the function we
28+
// should call the function and log its return value instead.
29+
30+
// Finally, correct the code to fix the problem
31+
// =============> write your new code here
832
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
1033
const percentage = `${decimalNumber * 100}%`;
1134

1235
return percentage;
1336
}
1437

15-
console.log(decimalNumber);
16-
17-
// =============> write your explanation here
18-
19-
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
38+
console.log(convertToPercentage(0.5)); // 50%

‎Sprint-3/1-key-errors/2.js‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
21
// Predict and explain first BEFORE you run any code...
32

43
// this function should square any number but instead we're going to get an error
54

65
// =============> write your prediction of the error here
6+
// I predict a SyntaxError, because 3 is written where a parameter name should be.
7+
// Parameters must be variable names, not values.
78

8-
function square(3) {
9-
return num * num;
10-
}
9+
// function square(3) {
10+
// return num * num;
11+
// }
1112

1213
// =============> write the error message here
14+
// SyntaxError: Unexpected number
1315

1416
// =============> explain this error message here
17+
// When declaring a function, the brackets hold parameter names: placeholders that
18+
// receive values when the function is called. JavaScript expected a name there and
19+
// found the number 3, which is not a valid name, so it reports an "Unexpected number".
20+
// The body also uses num, which is never declared, so num should be the parameter.
1521

1622
// Finally, correct the code to fix the problem
1723

1824
// =============> write your new code here
25+
function square(num) {
26+
return num * num;
27+
}
1928

29+
console.log(square(3)); // 9
2030

‎Sprint-3/2-mandatory-debug/0.js‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
// Predict and explain first...
22

33
// =============> write your prediction here
4+
// I predict it prints 320 on one line, then
5+
// "The result of multiplying 10 and 32 is undefined".
6+
// multiply logs the answer but never returns it.
47

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
8-
9-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
8+
// function multiply(a, b) {
9+
// console.log(a * b);
10+
// }
11+
//
12+
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1013

1114
// =============> write your explanation here
15+
// Actual output:
16+
// 320
17+
// The result of multiplying 10 and 32 is undefined
18+
// console.log only prints a value to the terminal; it does not send it back to
19+
// the caller. A function with no return statement returns undefined, so the
20+
// template literal receives undefined. The 320 appears first because multiply
21+
// runs (and logs) while the template literal is being built.
1222

1323
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
24+
// =============> write your new code here
25+
function multiply(a, b) {
26+
return a * b;
27+
}
28+
29+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
30+
// The result of multiplying 10 and 32 is 320

‎Sprint-3/2-mandatory-debug/1.js‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// I predict "The sum of 10 and 32 is undefined", because return is on its own line
4+
// and a + b is on the next line.
35

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
8-
9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
6+
// function sum(a, b) {
7+
// return;
8+
// a + b;
9+
// }
10+
//
11+
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1012

1113
// =============> write your explanation here
14+
// Actual output: The sum of 10 and 32 is undefined
15+
// return; with nothing after it ends the function immediately and returns undefined.
16+
// The line a + b is never reached. return must be followed by the value on the same line.
17+
1218
// Finally, correct the code to fix the problem
1319
// =============> write your new code here
20+
function sum(a, b) {
21+
return a + b;
22+
}
23+
24+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
25+
// The sum of 10 and 32 is 42

‎Sprint-3/2-mandatory-debug/2.js‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,43 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// I predict all three lines will say the last digit is 3, because getLastDigit
6+
// has no parameter and always uses the global num, which is 103.
57

6-
const num = 103;
7-
8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
11-
12-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
8+
// const num = 103;
9+
//
10+
// function getLastDigit() {
11+
// return num.toString().slice(-1);
12+
// }
13+
//
14+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
15+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
16+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1517

1618
// Now run the code and compare the output to your prediction
1719
// =============> write the output here
20+
// The last digit of 42 is 3
21+
// The last digit of 105 is 3
22+
// The last digit of 806 is 3
23+
1824
// Explain why the output is the way it is
1925
// =============> write your explanation here
26+
// getLastDigit is declared with no parameters, so the arguments 42, 105 and 806
27+
// are ignored. Inside the function, num refers to the global constant 103.
28+
// 103.toString() is "103" and .slice(-1) takes the last character, "3", every time.
29+
2030
// Finally, correct the code to fix the problem
2131
// =============> write your new code here
32+
// Give the function a parameter so it uses the number passed in, and remove the global num.
33+
function getLastDigit(num) {
34+
return num.toString().slice(-1);
35+
}
36+
37+
console.log(`The last digit of 42 is ${getLastDigit(42)}`); // 2
38+
console.log(`The last digit of 105 is ${getLastDigit(105)}`); // 5
39+
console.log(`The last digit of 806 is ${getLastDigit(806)}`); // 6
2240

2341
// This program should tell the user the last digit of each number.
2442
// Explain why getLastDigit is not working properly - correct the problem
43+
// It ignored its input because it had no parameter. Adding num as a parameter means
44+
// each call works on the number it was given.

‎Sprint-3/3-mandatory-implement/1-bmi.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@
1616

1717
function calculateBMI(weight, height) {
1818
// return the BMI of someone based off their weight and height
19+
const heightSquared = height * height;
20+
const bmi = weight / heightSquared;
21+
return bmi.toFixed(1); // toFixed returns a string rounded to 1 decimal place
1922
}
23+
24+
console.log(calculateBMI(70, 1.73)); // "23.4"
25+
console.log(calculateBMI(85, 1.8)); // "26.2"
26+
console.log(calculateBMI(50, 1.6)); // "19.5"

‎Sprint-3/3-mandatory-implement/2-cases.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@
1414
// You will need to come up with an appropriate name for the function
1515
// Use the MDN string documentation to help you find a solution
1616
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
17+
18+
function toUpperSnakeCase(text) {
19+
// replaceAll swaps every space for an underscore, then toUpperCase capitalises every letter
20+
return text.replaceAll(" ", "_").toUpperCase();
21+
}
22+
23+
console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE"
24+
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
25+
console.log(toUpperSnakeCase("code your future")); // "CODE_YOUR_FUTURE"

‎Sprint-3/3-mandatory-implement/3-to-pounds.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,32 @@
44
// You will need to declare a function called toPounds with an appropriately named parameter.
55

66
// You should call this function a number of times to check it works for different inputs
7+
8+
function toPounds(penceString) {
9+
// Remove the trailing "p", e.g. "399p" -> "399"
10+
const penceStringWithoutTrailingP = penceString.substring(
11+
0,
12+
penceString.length - 1,
13+
);
14+
15+
// Pad with zeros so there is always at least one pounds digit and two pence digits
16+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
17+
18+
// Everything except the last two digits is pounds
19+
const pounds = paddedPenceNumberString.substring(
20+
0,
21+
paddedPenceNumberString.length - 2,
22+
);
23+
24+
// The last two digits are pence
25+
const pence = paddedPenceNumberString
26+
.substring(paddedPenceNumberString.length - 2)
27+
.padEnd(2, "0");
28+
29+
return `£${pounds}.${pence}`;
30+
}
31+
32+
console.log(toPounds("399p")); // £3.99
33+
console.log(toPounds("5p")); // £0.05
34+
console.log(toPounds("50p")); // £0.50
35+
console.log(toPounds("12345p")); // £123.45

‎Sprint-3/4-mandatory-interpret/time-format.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,29 @@ function formatTimeDisplay(seconds) {
2222

2323
// a) When formatTimeDisplay is called how many times will pad be called?
2424
// =============> write your answer here
25+
// 3 times: once each for totalHours, remainingMinutes and remainingSeconds in the return line.
2526

2627
// Call formatTimeDisplay with an input of 61, now answer the following:
28+
// With seconds = 61: remainingSeconds = 61 % 60 = 1, totalMinutes = (61 - 1) / 60 = 1,
29+
// remainingMinutes = 1 % 60 = 1, totalHours = (1 - 1) / 60 = 0.
30+
// formatTimeDisplay(61) returns "00:01:01".
2731

2832
// b) What is the value assigned to num when pad is called for the first time?
2933
// =============> write your answer here
34+
// 0. The first call is pad(totalHours), and totalHours is 0.
3035

3136
// c) What is the return value of pad when it is called for the first time?
3237
// =============> write your answer here
38+
// "00". num.toString() gives "0", which is 1 character long, so the while loop adds
39+
// one "0" to the front, making it 2 characters.
3340

3441
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3542
// =============> write your answer here
43+
// 1. The last call is pad(remainingSeconds), and remainingSeconds is 61 % 60 = 1.
3644

3745
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
3846
// =============> write your answer here
47+
// "01". "1" has length 1, so the while loop adds one leading "0", then stops once the
48+
// length is 2.
49+
50+
console.log(formatTimeDisplay(61)); // 00:01:01

0 commit comments

Comments
 (0)