Skip to content

Commit 347d6c4

Browse files
committed
Complete Sprint 2 key errors, mandatory debug, implement, and interpret exercises
1 parent b31a586 commit 347d6c4

10 files changed

Lines changed: 93 additions & 50 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// ==============> This will throw a SyntaxError, because 'str' is already declared as the function's parameter, and line 8 tries to declare a new variable with the same name using let.
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)}`;
9-
return str;
8+
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
9+
return capitalisedStr;
1010
}
11-
12-
// =============> write your explanation here
13-
// =============> write your new code here
11+
// ==============> The error is "SyntaxError: Identifier 'str' has already been declared". This happens because you can't declare a new variable with let using a name that's already taken - in this case, the parameter str.
12+
// ==============> function capitalise(str) {
13+
// let capitalisedStr = ${str[0].toUpperCase()}${str.slice(1)};
14+
// return capitalisedStr;
15+
// }

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
// Predict and explain first...
2-
2+
//
33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// ==============> This will throw a SyntaxError because decimalNumber is already declared as the function's parameter, and line 9 tries to redeclare it with const.
55

6-
// Try playing computer with the example to work out what is going on
6+
// Try playing computer with the example to work out what will happen
77

88
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
109
const percentage = `${decimalNumber * 100}%`;
11-
1210
return percentage;
1311
}
1412

15-
console.log(decimalNumber);
13+
console.log(convertToPercentage(0.5));
1614

17-
// =============> write your explanation here
15+
// ==============> The error is "SyntaxError: Identifier 'decimalNumber' has already been declared". It happens for the same reason as before - you can't redeclare a variable with the same name as an existing function parameter using const.
1816

1917
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
18+
// ==============> function convertToPercentage(decimalNumber) {
19+
// const percentage = ${decimalNumber * 100}%;
20+
// return percentage;
21+
// }
22+
// console.log(convertToPercentage(0.5));
23+

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1+
// Predict and explain first BEFORE you run any code.
12

2-
// Predict and explain first BEFORE you run any code...
3+
// this function should square any number but instead
34

4-
// this function should square any number but instead we're going to get an error
5+
// ==============> This will throw a SyntaxError, because 3 is used as the parameter name in function square(3), but parameter names can't be numbers - they need to be valid identifiers like "num".
56

6-
// =============> write your prediction of the error here
7-
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+
// ==============> The error is "SyntaxError: Unexpected number"
1312

14-
// =============> explain this error message here
13+
// ==============> This happens because 3 is a number, not a valid parameter name. Parameter names must be identifiers, like "num" - JavaScript doesn't know what to do with a number in that position.
1514

1615
// Finally, correct the code to fix the problem
17-
18-
// =============> write your new code here
19-
20-
16+
// ==============> function square(num) {
17+
// return num * num;
18+
// }

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

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

3-
// =============> write your prediction here
3+
// ==============> This will print "320" first (from inside the function), then print "The result of multiplying 10 and 32 is undefined", because multiply() doesn't return a value - it only logs it.
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+
// ==============> The function was using console.log to display the answer, instead of returning it. This meant when the outer console.log tried to use the value from multiply(10, 32), it got undefined instead of the actual number, since the function returned nothing.
1212

1313
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
14+
// ==============> function multiply(a, b) {
15+
// return a * b;
16+
// }

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// ==============> This will print "The sum of 10 and 32 is undefined", because line 5 has a bare "return;" with nothing after it - this immediately exits the function and returns undefined. Line 6 (a + b) never runs.
33

44
function sum(a, b) {
5-
return;
6-
a + b;
5+
return a + b;
76
}
87

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

11-
// =============> write your explanation here
10+
// ==============> The function had "return;" on its own line, followed by "a + b;" on the next line. Once JavaScript hits return; with nothing after it, the function ends immediately and returns undefined - the a + b line is unreachable and never executes.
11+
1212
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
13+
// ==============> function sum(a, b) {
14+
// return a + b;
15+
// }

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// ==============> All three lines will print "3", because getLastDigit() ignores the number passed in and always uses the outer variable num (103), whose last digit is 3.
55

66
const num = 103;
77

8-
function getLastDigit() {
8+
function getLastDigit(num) {
99
return num.toString().slice(-1);
1010
}
1111

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

1616
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
17+
// ==============> The output was "3", "3", "3" as predicted.
1818
// Explain why the output is the way it is
19-
// =============> write your explanation here
19+
// ==============> getLastDigit didn't accept a parameter, so it always used the outer num variable (103) instead of the number passed in when calling the function.
2020
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
21+
// ==============> function getLastDigit(num) {
22+
// return num.toString().slice(-1);
23+
// }
2224

2325
// This program should tell the user the last digit of each number.
2426
// Explain why getLastDigit is not working properly - correct the problem

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@
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+
const bmi = weight / (height * height);
19+
return Math.round(bmi * 10) / 10;
20+
}
21+
console.log(calculateBMI(70, 1.73));

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@
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+
function toUpperSnakeCase(str) {
18+
return str.split(" ").join("_").toUpperCase();
19+
}
20+
21+
console.log(toUpperSnakeCase("hello there"));
22+
console.log(toUpperSnakeCase("lord of the rings"));

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

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

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,27 @@ function formatTimeDisplay(seconds) {
1515
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1616
}
1717

18-
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
18+
// You will need to play computer with this example - use the Python Visualiser https://
1919
// to help you answer these questions
2020

2121
// Questions
2222

2323
// a) When formatTimeDisplay is called how many times will pad be called?
24-
// =============> write your answer here
24+
// ==============> pad will be called 3 times - once for totalHours, once for remainingMinutes, once for remainingSeconds.
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+
// ==============> num is 0 (totalHours is called first, and its value is 0).
3030

3131
// c) What is the return value of pad is called for the first time?
32-
// =============> write your answer here
32+
// ==============> "00" (0 padded with a leading zero to make it 2 characters long).
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?
35+
// ==============> num is 1 (remainingSeconds is called last, and its value is 1).
36+
37+
// e) What is the return value of pad when it is called for the last time in this program?
38+
// ==============> "01" (1 padded with a leading zero).
39+
40+
console.log(formatTimeDisplay(61));
3641

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

0 commit comments

Comments
 (0)