Skip to content

Commit 4f08500

Browse files
committed
sprint 3
1 parent d28242a commit 4f08500

10 files changed

Lines changed: 81 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@ function capitalise(str) {
1010
}
1111

1212
// =============> write your explanation here
13+
14+
// The code will produce a SyntaxError because str is declared twice as a function parameter and again with let.
1315
// =============> write your new code here
16+
17+
function capitalise(str) {
18+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
19+
return str;
20+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
55

6+
// An error will occur because decimalNumber is declared twice inside the function, and
7+
// decimalNumber is also not accessible outside the function.
8+
69
// Try playing computer with the example to work out what is going on
710

811
function convertToPercentage(decimalNumber) {
@@ -18,3 +21,10 @@ console.log(decimalNumber);
1821

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

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@
44
// this function should square any number but instead we're going to get an error
55

66
// =============> write your prediction of the error here
7+
// A SyntaxError will occur because 3 cannot be used as a function parameter.
78

89
function square(3) {
910
return num * num;
1011
}
1112

1213
// =============> write the error message here
14+
// SyntaxError: Invalid or unexpected token
1315

1416
// =============> explain this error message here
1517

18+
// Function parameters must be variable names and not a number,
19+
// hence this is a syntaxError
20+
1621
// Finally, correct the code to fix the problem
1722

1823
// =============> write your new code here
19-
24+
function square(num) {
25+
return num * num;
26+
}
2027

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

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

33
// =============> write your prediction here
4+
// It will probably print 320 so it will multiply the 10 by 32 and then undefined because the multiply() is not returning anything
45

56
function multiply(a, b) {
67
console.log(a * b);
@@ -9,6 +10,13 @@ function multiply(a, b) {
910
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1011

1112
// =============> write your explanation here
13+
// The function prints 320, but because there is no return statement, multiply(10, 32) evaluates to undefined.
1214

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

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// The function will return undefined,
34

45
function sum(a, b) {
56
return;
@@ -9,5 +10,11 @@ function sum(a, b) {
910
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1011

1112
// =============> write your explanation here
13+
// The return; statement ends the function immediately. Therefore, a + b is never executed.
1214
// Finally, correct the code to fix the problem
1315
// =============> write your new code here
16+
function sum(a, b) {
17+
return a + b;
18+
}
19+
20+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// The last digit of 42 is 3
6+
// The last digit of 105 is 3
7+
// The last digit of 806 is 3
58

69
const num = 103;
710

@@ -15,10 +18,26 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1518

1619
// Now run the code and compare the output to your prediction
1720
// =============> write the output here
21+
22+
// The last digit of 42 is 3
23+
// The last digit of 105 is 3
24+
// The last digit of 806 is 3
1825
// Explain why the output is the way it is
1926
// =============> write your explanation here
27+
28+
// The function doesn't accept a parameter. It always uses the global variable:
2029
// Finally, correct the code to fix the problem
2130
// =============> write your new code here
2231

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@
1616

1717
function calculateBMI(weight, height) {
1818
// return the BMI of someone based off their weight and height
19+
const bmi = weight / (height * height);
20+
return bmi.toFixed(1);
1921
}
22+

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
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(str) {
19+
return str.toUpperCase().replaceAll(" ", "_");
20+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,13 @@
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+
const pounds = Math.floor(pence / 100);
9+
const remainingPence = pence % 100;
10+
11+
return `£${pounds}.${remainingPence.toString().padStart(2, "0")}`;
12+
}
13+
14+
console.log(toPounds(399));
15+
console.log(toPounds(100));
16+
console.log(toPounds(1250));

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@ function formatTimeDisplay(seconds) {
2222

2323
// a) When formatTimeDisplay is called how many times will pad be called?
2424
// =============> write your answer here
25+
// 3 times
2526

2627
// Call formatTimeDisplay with an input of 61, now answer the following:
2728

2829
// b) What is the value assigned to num when pad is called for the first time?
2930
// =============> write your answer here
31+
// 1
3032

3133
// c) What is the return value of pad when it is called for the first time?
3234
// =============> write your answer here
35+
// "01"
3336

3437
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3538
// =============> write your answer here
36-
39+
// 1 — The last call to pad is for remainingSeconds, which is 1 when seconds is 61.
3740
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
3841
// =============> write your answer here
42+
// "01" — pad(1) converts 1 to a string and adds "0" to the beginning.

0 commit comments

Comments
 (0)