Skip to content

Commit c68c814

Browse files
committed
Complete Sprint 3 Modules
1 parent 14c80c5 commit c68c814

14 files changed

Lines changed: 338 additions & 89 deletions

File tree

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// I predict an error will occur because str is already the name of the function parameter.
33

44
// call the function capitalise with a string input
5+
capitalise("hello");
6+
57
// interpret the error message and figure out why an error is occurring
68

79
function capitalise(str) {
810
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
911
return str;
1012
}
1113

12-
// =============> write your explanation here
13-
// =============> write your new code here
14+
// The error occurs because str is declared twice in the same scope.
15+
// str is already a parameter of the capitalise function, so we cannot declare it again using let.
16+
17+
// New code:
18+
function capitalise(str) {
19+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
20+
return str;
21+
}
22+
23+
console.log(capitalise("hello"));

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

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// I predict an error will occur because decimalNumber is already the name
5+
// of the function parameter and it is declared again using const.
56

67
// Try playing computer with the example to work out what is going on
78

@@ -14,7 +15,15 @@ function convertToPercentage(decimalNumber) {
1415

1516
console.log(decimalNumber);
1617

17-
// =============> write your explanation here
18+
// The error occurs because decimalNumber is declared twice in the same scope.
19+
// It is already a parameter of the function, so it cannot be declared again using const.
1820

1921
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
22+
23+
function convertToPercentage(decimalNumber) {
24+
const percentage = `${decimalNumber * 100}%`;
25+
26+
return percentage;
27+
}
28+
29+
console.log(convertToPercentage(0.5));

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
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+
// I predict there will be a syntax error because 3 is a number
6+
// and a function parameter must be a valid variable name.
77

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

12-
// =============> write the error message here
12+
// Error message:
13+
// Identifier expected.
1314

14-
// =============> explain this error message here
15+
// The error occurs because 3 is being used as the function parameter.
16+
// A function parameter must be a valid variable name, such as num.
17+
// The number 3 should be passed into the function as an argument instead.
1518

1619
// Finally, correct the code to fix the problem
1720

18-
// =============> write your new code here
19-
21+
function square(num) {
22+
return num * num;
23+
}
2024

25+
console.log(square(3));

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

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

3-
// =============> write your prediction here
3+
// I predict the multiplication will calculate 320,
4+
// but the final message will show undefined because the function
5+
// logs the result instead of returning it.
46

57
function multiply(a, b) {
68
console.log(a * b);
79
}
810

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

11-
// =============> write your explanation here
13+
// The multiply function uses console.log() to display the result,
14+
// but it does not return a value.
15+
// A function without a return statement returns undefined.
16+
// This is why the final message contains undefined.
1217

1318
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
19+
20+
function multiply(a, b) {
21+
return a * b;
22+
}
23+
24+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
3+
// I predict the result will be undefined because the return statement
4+
// does not return the result of a + b.
35

46
function sum(a, b) {
57
return;
@@ -8,6 +10,14 @@ function sum(a, b) {
810

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

11-
// =============> write your explanation here
13+
// The function returns immediately when it reaches the return statement.
14+
// Because there is no value after return, the function returns undefined.
15+
// The line a + b is never reached.
16+
1217
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
18+
19+
function sum(a, b) {
20+
return a + b;
21+
}
22+
23+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// I predict that all three console.log statements will show 3
5+
// because the function uses the global variable num, which is 103.
56

67
const num = 103;
78

@@ -14,11 +15,24 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1415
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1516

1617
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
18+
19+
// The output is:
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
19-
// =============> write your explanation here
25+
// The getLastDigit function does not have a parameter.
26+
// It always uses the global variable num, which has the value 103.
27+
// Therefore, the arguments 42, 105 and 806 are ignored.
28+
// The last digit of 103 is 3, so the function returns 3 every time.
29+
2030
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
2231

23-
// This program should tell the user the last digit of each number.
24-
// Explain why getLastDigit is not working properly - correct the problem
32+
function getLastDigit(number) {
33+
return number.toString().slice(-1);
34+
}
35+
36+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
37+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
38+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@
1717
function calculateBMI(weight, height) {
1818
// return the BMI of someone based off their weight and height
1919
}
20+
function calculateBMI(weight, height) {
21+
const bmi = weight / (height * height);
22+
return bmi.toFixed(1);
23+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@
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.toUpperCase().replaceAll(" ", "_");
19+
}
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1-
// In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js
1+
function toPounds(penceString) {
2+
const penceStringWithoutTrailingP = penceString.substring(
3+
0,
4+
penceString.length - 1
5+
);
26

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.
7+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
58

6-
// You should call this function a number of times to check it works for different inputs
9+
const pounds = paddedPenceNumberString.substring(
10+
0,
11+
paddedPenceNumberString.length - 2
12+
);
13+
14+
const pence = paddedPenceNumberString
15+
.substring(paddedPenceNumberString.length - 2)
16+
.padEnd(2, "0");
17+
18+
return `£${pounds}.${pence}`;
19+
}
20+
21+
console.log(toPounds("399p"));
22+
console.log(toPounds("50p"));
23+
console.log(toPounds("1250p"));

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,28 @@ 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
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.
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+
// The value assigned to num is 0.
3030

3131
// c) What is the return value of pad when it is called for the first time?
32-
// =============> write your answer here
32+
// The return value is "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+
// The value assigned to num is 1.
36+
// The last call is pad(remainingSeconds), and when the input is 61,
37+
// remainingSeconds is 1.
3638

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
39+
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
40+
// The return value is "01".
41+
// num is converted to the string "1". Because it has fewer than 2 characters,
42+
// the while loop adds a "0" to the beginning, producing "01".

0 commit comments

Comments
 (0)