Skip to content

Commit 319bc30

Browse files
committed
Complete Sprint 2 mandatory exercises
1 parent b31a586 commit 319bc30

10 files changed

Lines changed: 76 additions & 41 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// Prediction: The program will throw a SyntaxError because 'str' is declared twice in the same function.
33

44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
66

77
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
8+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
99
return str;
1010
}
1111

12-
// =============> write your explanation here
12+
console.log(capitalise("hello"));
13+
14+
// Explanation: The function parameter is already called 'str'. Declaring another variable with 'let str' inside the same function is not allowed, so JavaScript throws a SyntaxError.
15+
16+
// Finally, correct the code to fix the problem
1317
// =============> write your new code here

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
// Predict and explain first...
22

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// Prediction: The program will throw a SyntaxError because 'decimalNumber' is declared twice. If that is fixed, it will then throw a ReferenceError because 'decimalNumber' is used outside the function where it is defined.
55

66
// Try playing computer with the example to work out what is going on
77

88
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
109
const percentage = `${decimalNumber * 100}%`;
1110

1211
return percentage;
1312
}
1413

15-
console.log(decimalNumber);
14+
console.log(convertToPercentage(0.5));
1615

17-
// =============> write your explanation here
16+
// Explanation: The function parameter is already called 'decimalNumber', so declaring 'const decimalNumber' again is not allowed. Also, 'decimalNumber' only exists inside the function, so trying to use it outside the function causes a ReferenceError.
1817

1918
// Finally, correct the code to fix the problem
2019
// =============> write your new code here

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
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

6-
// =============> write your prediction of the error here
5+
// Prediction: The program will throw a SyntaxError because function parameters cannot be numbers. They must be valid variable names.
76

8-
function square(3) {
9-
return num * num;
7+
function square(num) {
8+
return num * num;
109
}
1110

12-
// =============> write the error message here
11+
console.log(square(3));
12+
13+
// Error message:
14+
// SyntaxError: Unexpected number
1315

14-
// =============> explain this error message here
16+
// Explanation: The parameter '3' is not a valid parameter name. Function parameters must be variable names so that values can be passed into the function.
1517

1618
// Finally, correct the code to fix the problem
1719

1820
// =============> write your new code here
19-
20-

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

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

3-
// =============> write your prediction here
3+
// Prediction: The function will print 320, but the final message will show "undefined".
44

55
function multiply(a, b) {
6-
console.log(a * b);
6+
return a * b;
77
}
88

99
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1010

11-
// =============> write your explanation here
11+
// Explanation: The original function used console.log() instead of return. console.log() displays the result on the screen but does not return a value, so the function returned undefined. Changing console.log() to return fixes the problem.
1212

1313
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
14+
// =============> write your new code here

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
3+
// Prediction: The function will return undefined because the return statement ends before a + b is executed.
34

45
function sum(a, b) {
5-
return;
6-
a + b;
6+
return a + b;
77
}
88

99
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1010

11-
// =============> write your explanation here
11+
// Explanation: In the original code, JavaScript automatically inserts a semicolon after `return` because it is on its own line. This means the function returns undefined immediately, and `a + b` is never executed.
12+
1213
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
14+
// =============> write your new code here

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// Prediction:
5+
// The program will print 3 for all three lines because the function always uses the constant 'num', which is 103.
56

6-
const num = 103;
7-
8-
function getLastDigit() {
7+
function getLastDigit(num) {
98
return num.toString().slice(-1);
109
}
1110

@@ -14,11 +13,17 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1413
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1514

1615
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
16+
// Output:
17+
// The last digit of 42 is 2
18+
// The last digit of 105 is 5
19+
// The last digit of 806 is 6
20+
1821
// Explain why the output is the way it is
19-
// =============> write your explanation here
22+
// Explanation:
23+
// The original function always used the constant 'num' (103), so it always returned 3. By giving the function a parameter called 'num', it now uses the value passed into the function each time it is called.
24+
2025
// Finally, correct the code to fix the problem
2126
// =============> write your new code here
2227

2328
// This program should tell the user the last digit of each number.
24-
// Explain why getLastDigit is not working properly - correct the problem
29+
// Explain why getLastDigit is not working properly - correct the problem.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
// It should return their Body Mass Index to 1 decimal place
1616

1717
function calculateBMI(weight, height) {
18-
// return the BMI of someone based off their weight and height
19-
}
18+
return (weight / (height * height)).toFixed(1);
19+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
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+
return text.toUpperCase().replaceAll(" ", "_");
20+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,23 @@
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+
const penceStringWithoutTrailingP = penceString.substring(
10+
0,
11+
penceString.length - 1
12+
);
13+
14+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
15+
16+
const pounds = paddedPenceNumberString.substring(
17+
0,
18+
paddedPenceNumberString.length - 2
19+
);
20+
21+
const pence = paddedPenceNumberString
22+
.substring(paddedPenceNumberString.length - 2)
23+
.padEnd(2, "0");
24+
25+
return ${pounds}.${pence}`;
26+
}

Sprint-2/4-mandatory-interpret/time-format.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ function formatTimeDisplay(seconds) {
2121
// Questions
2222

2323
// a) When formatTimeDisplay is called how many times will pad be called?
24-
// =============> write your answer here
24+
// Answer: 3 times
2525

2626
// Call formatTimeDisplay with an input of 61, now answer the following:
2727

2828
// b) What is the value assigned to num when pad is called for the first time?
29-
// =============> write your answer here
29+
// Answer: 0
3030

31-
// c) What is the return value of pad is called for the first time?
32-
// =============> write your answer here
31+
// c) What is the return value of pad when it is called for the first time?
32+
// Answer: "00"
3333

34-
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35-
// =============> write your answer here
34+
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35+
// Answer: 1
36+
// Explanation: The last call to pad is pad(remainingSeconds). When the input is 61 seconds, the remaining seconds are 1.
3637

37-
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38-
// =============> write your answer here
38+
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
39+
// Answer: "01"
40+
// Explanation: The pad function adds a leading zero to numbers with only one digit, so 1 becomes "01".

0 commit comments

Comments
 (0)