Skip to content

Commit 6c49d14

Browse files
committed
Sprint 2 Tasks completion
1 parent 522d1bd commit 6c49d14

File tree

10 files changed

+111
-15
lines changed

10 files changed

+111
-15
lines changed

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

Lines changed: 12 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+
// Prediction: The function capitalise is possibly meant to capitalise the first letter in a string
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
@@ -9,5 +10,16 @@ function capitalise(str) {
910
return str;
1011
}
1112

13+
14+
1215
// =============> write your explanation here
16+
// The 'str' variable on the left hand side is the same used in the function name implementation which should not be.
1317
// =============> write your new code here
18+
19+
function capitalise(str) {
20+
let capFunction = `${str[0].toUpperCase()}${str.slice(1)}`;
21+
return capFunction;
22+
}
23+
24+
let cap = capitalise('name')
25+
console.log(cap)

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Predict and explain first...
2+
// The functions takes a decimal number as input and converts it to percentage by multiplying by 100
23

34
// Why will an error occur when this program runs?
45
// =============> write your prediction here
6+
// I suspect than an error will occur
57

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

@@ -18,3 +20,12 @@ console.log(decimalNumber);
1820

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

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

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

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

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

1821
// =============> write your new code here
1922

20-
23+
function square(num) {
24+
return num * num;
25+
}

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

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

33
// =============> write your prediction here
4-
4+
// It may likely have issues as console.log() is within function implementation, instead of using'return'
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

1111
// =============> write your explanation here
12-
12+
// Instead of having console.log() is within function implementation, it should be replaced with 'return'
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+
}
18+
19+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
// An error will result becasue of the ';' between return and a + b
44
function sum(a, b) {
55
return;
66
a + b;
@@ -9,5 +9,11 @@ function sum(a, b) {
99
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1010

1111
// =============> write your explanation here
12+
// 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+
}
18+
19+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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
55

66
const num = 103;
77

@@ -14,11 +14,33 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1414
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1515

1616
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
17+
// =============> write the output here:
18+
// The last digit of 42 is 3
19+
// The last digit of 105 is 3
20+
// The last digit of 806 is 3
1821
// Explain why the output is the way it is
19-
// =============> write your explanation here
22+
// =============> write your explanation here;
23+
// 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
26+
let num = 103;
2227

28+
function getLastDigit() {
29+
return num.toString().slice(-1);
30+
}
31+
32+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
33+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
34+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2335
// This program should tell the user the last digit of each number.
2436
// Explain why getLastDigit is not working properly - correct the problem
37+
// getLastDigit is not working properly becuase the value for num is fixed at '103'
38+
// Corrected program:
39+
40+
function getLastDigit(num) {
41+
return num.toString().slice(-1);
42+
}
43+
44+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
45+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
46+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

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

Lines changed: 2 additions & 1 deletion
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-
}
19+
return Math.round((weight) / (height ** 2)).toFixed(1)
20+
}

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+
function upCase(strCar) {
18+
let result = strCar.toUpperCase().replaceAll(" ", "_");
19+
return result;
20+
}
21+
22+
console.log(upCase("hello there"))
23+
console.log(upCase("lord of the rings"))

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,28 @@
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+
14+
const paddedPenceNumberString = moneyWithoutTrailingP.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+
result = pounds + "." + pence;
24+
res = ${pounds}.${pence}`;
25+
return res;
26+
27+
}
28+
29+
console.log(toPounds("399p"))
30+
31+
console.log(toPounds("1099p"))

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ 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+
// =============> 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+
// =============> 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+
// =============> 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+
// =============> 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+
// =============> the return value assigned to num when pad is called for the last time in this program is '01'
35+
36+
// 00:01:01

0 commit comments

Comments
 (0)