Skip to content

Commit 569665b

Browse files
committed
Complete sprint 2 tasks
1 parent 5b7c4bd commit 569665b

10 files changed

Lines changed: 133 additions & 21 deletions

File tree

Sprint-2/1-key-errors/0.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+
//A) Error message - str has already been declared
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
}
1111

12-
// =============> write your explanation here
13-
// =============> write your new code here
12+
console.log(capitalise("hello there"));

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

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
5+
// The error happens because decimalNumber is declared twice.
6+
57

68
// Try playing computer with the example to work out what is going on
79

@@ -16,5 +18,17 @@ console.log(decimalNumber);
1618

1719
// =============> write your explanation here
1820

21+
//// The error happens because decimalNumber is already a parameter,
22+
// so it cannot be declared again using const.
23+
// Also, decimalNumber is inside the function, so it can't be
24+
// used outside the function with console.log().
25+
1926
// Finally, correct the code to fix the problem
2027
// =============> write your new code here
28+
29+
function convertToPercentage(decimalNumber) {
30+
const percentage = `${decimalNumber * 100}%`;
31+
return percentage;
32+
}
33+
34+
console.log(convertToPercentage(0.5));

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

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

22
// Predict and explain first BEFORE you run any code...
33

4+
// I think the code will give an error because 3 cannot be used
5+
// as a function parameter name. Parameters must be variable names.
6+
47
// this function should square any number but instead we're going to get an error
58

69
// =============> write your prediction of the error here
@@ -11,10 +14,20 @@ function square(3) {
1114

1215
// =============> write the error message here
1316

17+
// SyntaxError: Unexpected number
18+
1419
// =============> explain this error message here
20+
// The error means JavaScript found a number where it was
21+
// expecting a parameter name. Function parameters must be
22+
// identifiers like 'num', not values like 3.
1523

1624
// Finally, correct the code to fix the problem
1725

1826
// =============> write your new code here
1927

28+
function square(num) {
29+
return num * num;
30+
}
31+
32+
console.log(square(3));
2033

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

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

3-
// =============> write your prediction here
3+
// I think the code will not give an error, but it will print
4+
// "The result of multiplying 10 and 32 is undefined" because
5+
// the function only logs the answer and does not return a value.
6+
47

58
function multiply(a, b) {
69
console.log(a * b);
@@ -10,5 +13,14 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1013

1114
// =============> write your explanation here
1215

16+
// The problem is that the multiply function uses console.log()
17+
// instead of returning the result. When a function does not
18+
// return anything, JavaScript gives it the value undefined.
19+
1320
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
21+
22+
function multiply(a, b) {
23+
return a * b;
24+
}
25+
26+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
3+
// I think the code will not give an error, but it will print
4+
// "The sum of 10 and 32 is undefined" because the return statement
5+
// stops the function before a + b can run.
6+
7+
38

49
function sum(a, b) {
510
return;
@@ -9,5 +14,15 @@ function sum(a, b) {
914
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1015

1116
// =============> write your explanation here
17+
18+
// The problem is that `return;` ends the function immediately.
19+
// The code after return will never be reached, so the function
20+
// does not return the sum of a and b. It returns undefined instead.
21+
1222
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
23+
24+
function sum(a, b) {
25+
return a + b;
26+
}
27+
28+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// I think the output will be wrong because the function does not use
5+
// the numbers passed into it. It always uses the const num = 103,
6+
// so it will always return 3.
57

68
const num = 103;
79

@@ -14,11 +16,31 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1416
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1517

1618
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
19+
// The last digit of 42 is 3
20+
// The last digit of 105 is 3
21+
// The last digit of 806 is 3
22+
1823
// Explain why the output is the way it is
19-
// =============> write your explanation here
24+
25+
// The function is not working properly because it does not have a
26+
// parameter to receive the numbers. It uses the global variable num,
27+
// which is always 103, so it always returns the last digit of 103.
28+
29+
2030
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
31+
32+
function getLastDigit(num) {
33+
return num.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)}`);
2239

2340
// This program should tell the user the last digit of each number.
2441
// Explain why getLastDigit is not working properly - correct the problem
42+
43+
// The function is not working properly because it does not use the
44+
// number that is passed into it. It uses the global variable `num`,
45+
// which is always 103, so it always returns 3.
46+
// The function needs a parameter so it can use each number given to it.

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
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 bmi.toFixed(1);
20+
}
21+
22+
console.log(calculateBMI(70, 1.73));

Sprint-2/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+
18+
function toUpperSnakeCase(text) {
19+
return text.toUpperCase().replaceAll(" ", "_");
20+
}
21+
22+
console.log(toUpperSnakeCase("hello there"));
23+
console.log(toUpperSnakeCase("lord of the rings"));

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,27 @@
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+
15+
const pounds = paddedPenceNumberString.substring(
16+
0,
17+
paddedPenceNumberString.length - 2
18+
);
19+
20+
const pence = paddedPenceNumberString
21+
.substring(paddedPenceNumberString.length - 2)
22+
.padEnd(2, "0");
23+
24+
return ${pounds}.${pence}`;
25+
}
26+
27+
console.log(toPounds("399p"));
28+
console.log(toPounds("45p"));
29+
console.log(toPounds("5p"));
30+
console.log(toPounds("1234p"));

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@ 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+
// 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+
30+
// 0
3031

3132
// c) What is the return value of pad is called for the first time?
32-
// =============> write your answer here
33+
// 00
3334

3435
// 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+
// 1, the modulo % operator means that 61 gives a remainder of 1
3637

3738
// 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+
// "01"
40+
41+
// pad adds a leading 0 to single-digit numbers, so 1 becomes "01".

0 commit comments

Comments
 (0)