Skip to content

Commit 1d8c408

Browse files
committed
Completion of Sprint 2 tasks
1 parent 0e99abc commit 1d8c408

File tree

10 files changed

+75
-21
lines changed

10 files changed

+75
-21
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> Prediction: The function capitalise is possibly meant to capitalise the first letter in a string.
33

44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
@@ -9,5 +9,10 @@ function capitalise(str) {
99
return str;
1010
}
1111

12-
// =============> write your explanation here
12+
// =============> =============> write your explanation here
13+
// The 'str' variable on the left hand side is the same used in the function name implementation which should not be.
1314
// =============> write your new code here
15+
function capitalise(str) {
16+
let capFunction = `${str[0].toUpperCase()}${str.slice(1)}`;
17+
return capFunction;
18+
}

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

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// =============> I suspect than an error will occur
55

66
// Try playing computer with the example to work out what is going on
77

@@ -14,7 +14,10 @@ function convertToPercentage(decimalNumber) {
1414

1515
console.log(decimalNumber);
1616

17-
// =============> write your explanation here
18-
17+
// =============> write your explanation here. The functions takes a decimal number as input and converts it to percentage by multiplying by 100
1918
// Finally, correct the code to fix the problem
2019
// =============> write your new code here
20+
function convertToPercentage(decimalNumber) {
21+
const percentage = `${decimalNumber * 100}%`;
22+
return percentage;
23+
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
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+
// =============> write your prediction of the error here. The error is likely to be caused by square(3), '3' is supposed to be replaced by a string representing the number.
77

88
function square(3) {
99
return num * num;
1010
}
1111

1212
// =============> write the error message here
13-
13+
// function square(3) {
14+
// ^
15+
// SyntaxError: Unexpected number
1416
// =============> explain this error message here
15-
17+
// The muber '3' should be used when calling the function and it should not be used as a parameter.
1618
// Finally, correct the code to fix the problem
1719

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

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

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

3-
// =============> write your prediction here
3+
// =============> write your prediction here. It may likely have issues as console.log() is within function implementation, instead of using'return'
44

55
function multiply(a, b) {
66
console.log(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+
// =============> write your explanation here. Instead of having console.log() is within function implementation, it should be replaced with 'return'
1212

1313
// Finally, correct the code to fix the problem
1414
// =============> write your new code here
15+
function multiply(a, b) {
16+
return (a * b);
17+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> write your prediction here. An error will result becasue of the ';' between return and a + b
3+
34

45
function sum(a, b) {
56
return;
@@ -8,6 +9,9 @@ function sum(a, b) {
89

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

11-
// =============> write your explanation here
12+
// =============> write your explanation here. The ideal statement should be 'return a + b; ' but this statement is separated by ';'
1213
// Finally, correct the code to fix the problem
1314
// =============> write your new code here
15+
function sum(a, b) {
16+
return a + b;
17+
}

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

Lines changed: 13 additions & 3 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+
// =============> Write your prediction here. It may run but give incorrect results because of const num & no parameter specified
5+
56

67
const num = 103;
78

@@ -15,10 +16,19 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1516

1617
// Now run the code and compare the output to your prediction
1718
// =============> 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
1822
// Explain why the output is the way it is
19-
// =============> write your explanation here
23+
// =============> write your explanation here. num is a constant variable, it's value cannot change.
2024
// Finally, correct the code to fix the problem
2125
// =============> write your new code here
22-
26+
function getLastDigit() {
27+
return num.toString().slice(-1);
28+
}
2329
// This program should tell the user the last digit of each number.
2430
// Explain why getLastDigit is not working properly - correct the problem
31+
// getLastDigit is not working properly because the value for num is fixed at '103'
32+
function getLastDigit(num) {
33+
return num.toString().slice(-1);
34+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616

1717
function calculateBMI(weight, height) {
1818
// return the BMI of someone based off their weight and height
19+
return Math.round((weight) / (height ** 2)).toFixed(1)
1920
}

Sprint-2/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+
function upCase(strCar) {
18+
let result = strCar.toUpperCase().replaceAll(" ", "_");
19+
return result;
20+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,23 @@
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(money) {
9+
const moneyWithoutTrailingP = money.substring(
10+
0,
11+
money.length - 1
12+
);
13+
const paddedPenceNumberString = moneyWithoutTrailingP.padStart(3, "0");
14+
const pounds = paddedPenceNumberString.substring(
15+
0,
16+
paddedPenceNumberString.length - 2
17+
);
18+
const pence = paddedPenceNumberString
19+
.substring(paddedPenceNumberString.length - 2)
20+
.padEnd(2, "0");
21+
result = pounds + "." + pence;
22+
res = ${pounds}.${pence}`;
23+
return res;
24+
}
25+
console.log(toPounds("399p"))
26+
console.log(toPounds("1099p"))

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ function formatTimeDisplay(seconds) {
1717
// Questions
1818

1919
// a) When formatTimeDisplay is called how many times will pad be called?
20-
// =============> write your answer here
20+
// =============> write your answer here.pad will be called three times.
2121

2222
// Call formatTimeDisplay with an input of 61, now answer the following:
2323

2424
// b) What is the value assigned to num when pad is called for the first time?
25-
// =============> write your answer here
25+
// =============> write your answer here. the value assigned to num when pad is called for the first time is 00
2626

2727
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here
28+
// =============> write your answer here. the return value of pad is called for the first time is '00'
2929

3030
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
31-
// =============> write your answer here
31+
// =============> write your answer here. the value assigned to num when pad is called for the last time is 01
3232

3333
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
34-
// =============> write your answer here
34+
// =============> write your answer here. the return value assigned to num when pad is called for the last time in this program is '01'

0 commit comments

Comments
 (0)