Skip to content

Commit d90778b

Browse files
committed
Complete Sprint 3 coursework
1 parent d28242a commit d90778b

11 files changed

Lines changed: 69 additions & 57 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// ===========> I predict there will be an error when the function is called.
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
}
11-
12-
// =============> write your explanation here
13-
// =============> write your new code here
11+
capitalise("hello");
12+
// ===========> The error happens because str has already been declared as a function parameter.
13+
// ===========> Removed let because str is already declared as a function parameter.

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

Lines changed: 4 additions & 5 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+
// ===========> I predict there will be an error because decimalNumber is declared more than once.
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+
// ===========> The error happens because decimalNumber is already declared as a function parameter.
1817

1918
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
19+
// ===========> Removed the duplicate declaration and called the function with 0.5.

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
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 cannot be used as a function parameter.
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+
// ===========> SyntaxError: Unexpected number
1312

14-
// =============> explain this error message here
13+
// ===========> The error happens because a number cannot be used as a function parameter name.
1514

1615
// Finally, correct the code to fix the problem
1716

18-
// =============> write your new code here
19-
20-
17+
// ===========> Changed 3 to num so the function has a valid parameter name.

‎Sprint-3/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+
// ===========> I predict the multiplication will print 320, but the final result 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+
// ===========> The function prints the result instead of returning it, so the function returns undefined.
1212

1313
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
14+
// ===========> Changed console.log to return so the function returns the multiplication result.

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// ===========> I predict the result will be undefined because the function returns before adding the numbers.
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 returns before a + b is calculated, so the result is undefined.
1211
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
12+
// ===========> Changed the return statement so it returns a + b.

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// ===========> I predict all three results will be 3 because the function always uses num, which is 103.
55

6-
const num = 103;
7-
8-
function getLastDigit() {
6+
function getLastDigit(num) {
97
return num.toString().slice(-1);
108
}
119

@@ -15,10 +13,11 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1513

1614
// Now run the code and compare the output to your prediction
1715
// =============> write the output here
18-
// Explain why the output is the way it is
16+
// ============> The output is 3 for all three numbers.
17+
// ============> The function ignores the numbers passed to it and always uses num, which is 103.
1918
// =============> write your explanation here
2019
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
20+
// ============> Added num as a parameter so the function uses the number passed to it.
2221

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
// It should return a string of 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+
const bmi = weight / (height * height);
19+
return bmi.toFixed(1);
1920
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@
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+
const upperCaseString = str.toUpperCase();
19+
const snakeCaseString = upperCaseString.replaceAll(" ", "_");
20+
return snakeCaseString;
21+
}
22+
console.log(toUpperSnakeCase("hello there"));
23+
console.log(toUpperSnakeCase("lord of the rings"));

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@
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(pence) {
8+
return pence / 100;
9+
}
10+
11+
console.log(toPounds(100));
12+
console.log(toPounds(250));
13+
console.log(toPounds(500));

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ 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+
// ============> 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+
// ============> 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+
// ============> "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+
// ============> 1, because remainingSeconds is 1.
3636

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
37+
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38+
// ============> "01", because pad adds a 0 to make the value two characters long.

0 commit comments

Comments
 (0)