Skip to content

Commit d4e4eaf

Browse files
committed
sprint-2 exercies has been completed
1 parent 8f3d6cf commit d4e4eaf

File tree

11 files changed

+164
-45
lines changed

11 files changed

+164
-45
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
// Its retrurn an error
44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
66

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
7+
// function capitalise(str) {
8+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9+
// return str;
10+
// }
1111

1212
// =============> write your explanation here
13+
// SyntaxError: Identifier 'str' has already been declared
1314
// =============> write your new code here
15+
function capitalise(str) {
16+
let formatedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
17+
return formatedStr;
18+
}
19+
console.log(capitalise("info")) //==> Info

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,30 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// it logo an error
56

67
// Try playing computer with the example to work out what is going on
8+
// function convertToPercentage(decimalNumber) {
9+
// const decimalNumber = 0.5;
10+
// const percentage = `${decimalNumber * 100}%`;
711

8-
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
11-
12-
return percentage;
13-
}
12+
// return percentage;
13+
// }
1414

15-
console.log(decimalNumber);
15+
// console.log(decimalNumber);
1616

1717
// =============> write your explanation here
18+
// in line 8 and 9 trying to declare decimalNumber twice:
19+
// As a parameter to the function.
20+
// Again inside the function with const decimalNumber = 0.5;.
1821

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

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
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+
// =============> return SyntaxError can't be num
77

8-
function square(3) {
9-
return num * num;
10-
}
8+
// function square(3) {
9+
// return num * num;
10+
// }
1111

12-
// =============> write the error message here
12+
// =============> write the error message here. SyntaxError: Unexpected number
1313

14-
// =============> explain this error message here
14+
// =============> explain this error message here. parameters must be valid variable names . and using `num`, but `num` is not defined anywhere.
1515

1616
// Finally, correct the code to fix the problem
1717

1818
// =============> write your new code here
19-
19+
function square(num) {
20+
return num * num;
21+
}
22+
console.log(square(6))
2023

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

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

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

55
function multiply(a, b) {
66
console.log(a * b);
77
}
8-
9-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
8+
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
109

1110
// =============> write your explanation here
11+
// When `multiply(10, 32)` is called inside the template string,
12+
// it runs the function (which logs `320`), but since it doesn't return anything,
13+
// it inserts `undefined` into the string.
1214

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

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> it returns undefined, Because a + b line is never executed
33

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
4+
// function sum(a, b) {
5+
// return;
6+
// a + b;
7+
// }
88

9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
9+
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1010

1111
// =============> write your explanation here
12+
// Because there's a line break after `return`, the `a + b;` line is never executed.
1213
// Finally, correct the code to fix the problem
1314
// =============> write your new code here
15+
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-2/2-mandatory-debug/2.js

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> Write your prediction here .eturn undefiend
55

6-
const num = 103;
6+
// const num = 103;
77

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
8+
// function getLastDigit() {
9+
// return num.toString().slice(-1);
10+
// }
1111

12-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
12+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1515

1616
// Now run the code and compare the output to your prediction
1717
// =============> write the output here
1818
// Explain why the output is the way it is
19-
// =============> write your explanation here
19+
// =============> write your explanation here. doesn't take any parameters, so it always uses the constant `num = 103`.
2020
// Finally, correct the code to fix the problem
2121
// =============> write your new code here
2222

2323
// This program should tell the user the last digit of each number.
2424
// Explain why getLastDigit is not working properly - correct the problem
25+
26+
function getLastDigit(num) {
27+
return num.toString().slice(-1);
28+
}
29+
30+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
31+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
32+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

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+
let SnakeCaseStr = str.replace(/ /g, "_")
19+
return SnakeCaseStr.toUpperCase()
20+
}
21+
console.log(toUpperSnakeCase("hello there")) // ===> HELLO_THERE

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,31 @@
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+
// const penceString = "399p";
9+
// const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1);
10+
// const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
11+
// const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2);
12+
// const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
13+
14+
// console.log(`£${pounds}.${pence}`);
15+
16+
function toPounds(penceString) {
17+
const penceStringWithoutTrailingP = penceString.substring(
18+
0,
19+
penceString.length - 1
20+
);
21+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
22+
const pounds = paddedPenceNumberString.substring(
23+
0,
24+
paddedPenceNumberString.length - 2
25+
);
26+
const pence = paddedPenceNumberString
27+
.substring(paddedPenceNumberString.length - 2)
28+
.padEnd(2, "0");
29+
return ${pounds}.${pence}`;
30+
}
31+
console.log(toPounds("399p")); // £3.99
32+
console.log(toPounds("9p")); // £0.09
33+
console.log(toPounds("50p")); // £0.50
34+
console.log(toPounds("100p")); // £1.00

Sprint-2/3-mandatory-implement/1-bmi.js renamed to Sprint-2/3-mandatory-implement/c.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@
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+
let BMI = weight / (height * height);
19+
return parseFloat(BMI.toFixed(1));
20+
}
21+
console.log(calculateBMI(70, 1.73)) //===>23.4

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@ function formatTimeDisplay(seconds) {
1010

1111
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1212
}
13+
formatTimeDisplay(60);
1314

1415
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1516
// to help you answer these questions
1617

1718
// Questions
1819

1920
// a) When formatTimeDisplay is called how many times will pad be called?
20-
// =============> write your answer here
21+
// =============> write your answer here. one time
2122

2223
// Call formatTimeDisplay with an input of 61, now answer the following:
2324

2425
// b) What is the value assigned to num when pad is called for the first time?
25-
// =============> write your answer here
26+
// =============> write your answer here .is 60
2627

2728
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here
29+
// =============> write your answer here is 0
2930

3031
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3132
// =============> write your answer here

0 commit comments

Comments
 (0)