Skip to content

Commit bee1483

Browse files
committed
Complete task for the Sprint-2
1 parent 6818ea5 commit bee1483

10 files changed

Lines changed: 70 additions & 111 deletions

File tree

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
// Predict and explain first...
2-
// =============> write your prediction here
3-
4-
// call the function capitalise with a string input
5-
// interpret the error message and figure out why an error is occurring
1+
// Predict: SyntaxError or ReferenceError because `str` is re-declared with `let` inside the function
2+
// where it already exists as a parameter.
3+
// Explanation: You cannot use `let` to re-declare a variable that already exists in the same scope.
4+
// Fix: remove the `let` keyword — just reassign str.
65

76
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
7+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
98
return str;
109
}
11-
12-
// =============> write your explanation here
13-
// =============> write your new code here

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
// Predict and explain first...
2-
3-
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
5-
6-
// Try playing computer with the example to work out what is going on
1+
// Prediction: Two errors — (1) re-declaring decimalNumber with const inside the function
2+
// when it is already a parameter shadows and causes a SyntaxError.
3+
// (2) console.log(decimalNumber) outside the function will throw ReferenceError because
4+
// decimalNumber is not defined in the outer scope.
5+
// Explanation: The parameter decimalNumber already exists; const re-declaration is illegal.
6+
// Also, decimalNumber is not accessible outside the function.
7+
// Fix: remove the const re-declaration inside the function, and pass a value to the function.
78

89
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
1010
const percentage = `${decimalNumber * 100}%`;
11-
1211
return percentage;
1312
}
1413

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
14+
console.log(convertToPercentage(0.5));

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

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11

2-
// Predict and explain first BEFORE you run any code...
2+
// Prediction: SyntaxError — a function parameter must be a valid identifier, not a literal number.
3+
// Error: SyntaxError: Unexpected number
4+
// Explanation: `3` is a number literal and cannot be used as a parameter name.
5+
// Fix: use a named parameter like `num`.
36

4-
// this function should square any number but instead we're going to get an error
5-
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
}
11-
12-
// =============> write the error message here
13-
14-
// =============> explain this error message here
15-
16-
// Finally, correct the code to fix the problem
17-
18-
// =============> write your new code here
19-
20-

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
// Predict and explain first...
2-
3-
// =============> write your prediction here
1+
// Prediction: The template literal will print "undefined" for the function result because
2+
// multiply uses console.log internally but does not return a value (returns undefined).
3+
// Explanation: Functions without a return statement return undefined.
4+
// The outer console.log then interpolates undefined into the string.
5+
// Fix: replace console.log inside multiply with a return statement.
46

57
function multiply(a, b) {
6-
console.log(a * b);
8+
return a * b;
79
}
810

911
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
10-
11-
// =============> write your explanation here
12-
13-
// Finally, correct the code to fix the problem
14-
// =============> write your new code here

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
// Predict and explain first...
2-
// =============> write your prediction here
1+
// Prediction: The sum will print "undefined" because return; exits the function
2+
// immediately without a value, so a + b is never evaluated.
3+
// Explanation: return; with no expression returns undefined. The a + b on the next
4+
// line is unreachable dead code.
5+
// Fix: put a + b on the same line as return.
36

47
function sum(a, b) {
5-
return;
6-
a + b;
8+
return a + b;
79
}
810

911
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10-
11-
// =============> write your explanation here
12-
// Finally, correct the code to fix the problem
13-
// =============> write your new code here

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
// Predict and explain first...
1+
// Prediction: All three logs will print "3" (the last digit of the module-level const num = 103)
2+
// because getLastDigit ignores its parameter and always uses the outer num.
3+
// Output:
4+
// The last digit of 42 is 3
5+
// The last digit of 105 is 3
6+
// The last digit of 806 is 3
7+
// Explanation: The function has no parameter, so any argument passed in is discarded.
8+
// It always reads the outer `num` variable which is 103.
9+
// Fix: add a parameter to getLastDigit and use it.
210

3-
// Predict the output of the following code:
4-
// =============> Write your prediction here
5-
6-
const num = 103;
7-
8-
function getLastDigit() {
11+
function getLastDigit(num) {
912
return num.toString().slice(-1);
1013
}
1114

1215
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1316
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1417
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15-
16-
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
18-
// Explain why the output is the way it is
19-
// =============> write your explanation here
20-
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
22-
23-
// This program should tell the user the last digit of each number.
24-
// Explain why getLastDigit is not working properly - correct the problem

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

Lines changed: 1 addition & 1 deletion
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
18+
return Number((weight / (height * height)).toFixed(1));
1919
}
Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
// A set of words can be grouped together in different cases.
1+
function toUpperSnakeCase(str) {
2+
return str.replaceAll(" ", "_").toUpperCase();
3+
}
24

3-
// For example, "hello there" in snake case would be written "hello_there"
4-
// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces.
5-
6-
// Implement a function that:
7-
8-
// Given a string input like "hello there"
9-
// When we call this function with the input string
10-
// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE"
11-
12-
// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"
13-
14-
// You will need to come up with an appropriate name for the function
15-
// Use the MDN string documentation to help you find a solution
16-
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
5+
console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE"
6+
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
// In Sprint-1, there is a program written in interpret/to-pounds.js
1+
function toPounds(penceString) {
2+
const withoutP = penceString.substring(0, penceString.length - 1);
3+
const padded = withoutP.padStart(3, "0");
4+
const pounds = padded.substring(0, padded.length - 2);
5+
const pence = padded.substring(padded.length - 2).padEnd(2, "0");
6+
return ${pounds}.${pence}`;
7+
}
28

3-
// You will need to take this code and turn it into a reusable block of code.
4-
// You will need to declare a function called toPounds with an appropriately named parameter.
5-
6-
// You should call this function a number of times to check it works for different inputs
9+
console.log(toPounds("399p")); // £3.99
10+
console.log(toPounds("9p")); // £0.09
11+
console.log(toPounds("50p")); // £0.50
12+
console.log(toPounds("1000p")); // £10.00

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,20 @@ 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
19-
// to help you answer these questions
18+
// a) pad is called 3 times per call to formatTimeDisplay (once for hours, minutes, seconds).
2019

21-
// Questions
20+
// Call formatTimeDisplay(61):
21+
// remainingSeconds = 61 % 60 = 1
22+
// totalMinutes = (61 - 1) / 60 = 1
23+
// remainingMinutes = 1 % 60 = 1
24+
// totalHours = (1 - 1) / 60 = 0
25+
// pad is called with: totalHours=0, remainingMinutes=1, remainingSeconds=1
2226

23-
// a) When formatTimeDisplay is called how many times will pad be called?
24-
// =============> write your answer here
27+
// b) num = 0 (totalHours) when pad is called for the first time.
2528

26-
// Call formatTimeDisplay with an input of 61, now answer the following:
29+
// c) pad(0): "0".length < 2, so prepend "0" -> "00". Return value: "00"
2730

28-
// b) What is the value assigned to num when pad is called for the first time?
29-
// =============> write your answer here
31+
// d) num = 1 (remainingSeconds) when pad is called for the last time.
32+
// It is the last argument passed in the template literal.
3033

31-
// c) What is the return value of pad is called for the first time?
32-
// =============> write your answer here
33-
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
36-
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
34+
// e) pad(1): "1".length < 2, so prepend "0" -> "01". Return value: "01"

0 commit comments

Comments
 (0)