Skip to content

Commit efe68c9

Browse files
committed
sprint_2
1 parent 124ae45 commit efe68c9

File tree

10 files changed

+72
-46
lines changed

10 files changed

+72
-46
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> When we call capitalise("fay"), it will throw an error instead of returning "Fay"
33

44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
66

7+
// =============> "str" has already been declared, in JavaScript we can not redeclare a parameter using let inside the same scope.
8+
// =============> write your new code here
79
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10+
return str = `${str[0].toUpperCase()}${str.slice(1)}`;
1011
}
11-
12-
// =============> write your explanation here
13-
// =============> write your new code here
12+
console.log(capitalise("fay"));

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

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// =============> 'decimalNumber' has already been declared.
5+
56

67
// Try playing computer with the example to work out what is going on
8+
9+
// =============>The variable in line 9 "decimalNumber" is declared as a parameter passed to the function
10+
// but in line 10 the variable redeclared again.
11+
// Finally, correct the code to fix the problem
12+
// =============> write your new code here
13+
14+
const decimalNumber = 0.5;
715

816
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
17+
1018
const percentage = `${decimalNumber * 100}%`;
1119

1220
return percentage;
1321
}
1422

1523
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

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33

44
// this function should square any number but instead we're going to get an error
55

6-
// =============> write your prediction of the error here
6+
// =============> Function parameters must be variable names not values.
77

8-
function square(3) {
9-
return num * num;
10-
}
118

12-
// =============> write the error message here
9+
// =============> SyntaxError: Unexpected number
1310

14-
// =============> explain this error message here
11+
// =============> We can not put a number (3) as a parameter name.
1512

1613
// Finally, correct the code to fix the problem
1714

1815
// =============> write your new code here
16+
function square(num) {
17+
return num * num;
18+
}
19+
console.log(square(3));
1920

2021

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

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

3-
// =============> write your prediction here
3+
// =============> The result of the multiplying is undefined
44

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
8-
9-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
10-
11-
// =============> write your explanation here
5+
// =============> The function logs the result but does not return anything.
126

137
// Finally, correct the code to fix the problem
148
// =============> write your new code here
9+
function multiply(a, b) {
10+
return a * b;
11+
}
12+
13+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> The sum of 10 and 32 is undefined
33

4+
// =============> The line break and the semicolon after "return" stops the function and returns undefined.
5+
// Finally, correct the code to fix the problem
6+
// =============> write your new code here
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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
// Predict and explain first...
22

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> The last digit will be 3 with all the function.
55

6+
// Now run the code and compare the output to your prediction
7+
// =============> write the output here
8+
// Explain why the output is the way it is
9+
// =============> A function "getLastDigit" must declare parameters to receive arguments.
10+
//If it doesn’t, any arguments passed in are ignored.
11+
// Finally, correct the code to fix the problem
12+
// =============> write your new code here
613
const num = 103;
714

8-
function getLastDigit() {
15+
function getLastDigit(num) {
916
return num.toString().slice(-1);
1017
}
1118

1219
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1320
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1421
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1522

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-
2323
// This program should tell the user the last digit of each number.
2424
// Explain why getLastDigit is not working properly - correct the problem

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

Lines changed: 4 additions & 1 deletion
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-
}
19+
const bmi = weight / height ** 2;
20+
return parseFloat(bmi.toFixed(1));
21+
}
22+
console.log(calculateBMI(60, 1.70));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@
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+
}
20+
console.log(toUpperSnakeCase("hello there"));
21+
console.log(toUpperSnakeCase("lord of the rings"));

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,22 @@
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+
8+
function toPounds(penceString) {
9+
const penceStringWithoutTrailingP = penceString.substring(
10+
0,
11+
penceString.length - 1
12+
);
13+
14+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
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+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ function formatTimeDisplay(seconds) {
1616

1717
// Questions
1818

19-
// a) When formatTimeDisplay is called how many times will pad be called?
20-
// =============> write your answer here
19+
// a) When formatTimeDisplay is called how many times will pad be called?// =============> 3
2120

2221
// Call formatTimeDisplay with an input of 61, now answer the following:
2322

0 commit comments

Comments
 (0)