Skip to content

Commit 4120eff

Browse files
committed
compeltely finished tasks for this sprint, as deadline due
1 parent 3372770 commit 4120eff

File tree

11 files changed

+172
-46
lines changed

11 files changed

+172
-46
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// I think that the first letter of the string will be upper case and then the .slice method will extract a part of a string and return the extracted part, being --> apitalise.
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
7+
// function capitalise is already a variable and let str is decalring that variable again but str already exists.
68

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

1214
// =============> write your explanation here
1315
// =============> write your new code here
16+
17+
function capitalise(str) {
18+
return `${str[0].toUpperCase()}${str.slice(1)}`;
19+
}

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

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

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// You cannot re-declare decimalNumber, it already exists as a parameter.
56

67
// Try playing computer with the example to work out what is going on
78

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

12-
return percentage;
13-
}
13+
//return percentage;
14+
//}
1415

15-
console.log(decimalNumber);
16+
//console.log(decimalNumber);
1617

1718
// =============> write your explanation here
19+
// We have to remove the re-declaration and pass a value into the function, then log the result of the fucntion.
1820

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

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11

2-
// Predict and explain first BEFORE you run any code...
3-
2+
// Predict and explain first BEFORE you run any code..
43
// this function should square any number but instead we're going to get an error
54

65
// =============> write your prediction of the error here
6+
// You cannot put a number as a function parameter name
77

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

1212
// =============> write the error message here
13-
13+
// SyntaxError: Unexpected number
1414
// =============> explain this error message here
15-
15+
// The "Unexpected number" error arises when a numeral is improperly positioned or used within your JavaScript code.
1616
// Finally, correct the code to fix the problem
1717

1818
// =============> write your new code here
19+
function square(num) {
20+
return num * num;
21+
}
1922

2023

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

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

33
// =============> write your prediction here
4+
// the first console.log cant be there as it has to be a return.
45

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
6+
// function multiply(a, b) {
7+
// console.log(a * b);
8+
// }
89

9-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
10+
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1011

1112
// =============> write your explanation here
12-
13+
// It prints the number 320 in the terminal then undefined,
1314
// Finally, correct the code to fix the problem
1415
// =============> write your new code here
16+
17+
function multiply(a, b) {
18+
return a * b;
19+
}
20+
21+
console.log(`The result of multiplying 10 and 32 is ${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...
22
// =============> write your prediction here
3+
// after the return it exists the function so it ever reaches a + b.
4+
//function sum(a, b) {
5+
//return;
6+
//a + b;
7+
//}
38

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
8-
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+
// however it did still , print The sum of 10 and 32 is undefined
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: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,39 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// The function does not take a parameter.
6+
//const num = 103;
57

6-
const num = 103;
8+
//function getLastDigit() {
9+
//return num.toString().slice(-1);
10+
//}
711

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
11-
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
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
1922
// =============> write your explanation here
23+
// This happens because the functionn does not accept a parameter, it always uses the global variable num and the arguments passed in (42, 105, 806) are ignored.
2024
// Finally, correct the code to fix the problem
2125
// =============> write your new code here
2226

27+
function getLastDigit(num) {
28+
return num.toString().slice(-1);
29+
}
30+
31+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
32+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
33+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
34+
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+
// here is the output with the code corrects
38+
// the last digit of 42 is 2
39+
//The last digit of 105 is 5
40+
//The last digit of 806 is 6

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
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
18+
const bmi = weight / (height * height);
19+
return Number(bmi.toFixed(1));
1920
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces.
55

66
// Implement a function that:
7+
const sentence = "The quick brown fox jumps over the lazy dog.";
78

9+
console.log(sentence.toUpperCase());
10+
// Expected output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."
811
// Given a string input like "hello there"
912
// When we call this function with the input string
1013
// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE"
14+
const sentence2 = "The quick brown fox jumps over the lazy dog.";
1115

16+
console.log(sentence2.toUpperCase().replace(/\./g, "_").replace(/ /g, "_"));
1217
// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"
1318

19+
// Expected output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."
1420
// You will need to come up with an appropriate name for the function
1521
// Use the MDN string documentation to help you find a solution
1622
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase

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

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,32 @@ function formatTimeDisplay(seconds) {
1111
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1212
}
1313

14+
console.log(formatTimeDisplay(61));
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?
2021
// =============> write your answer here
22+
// its called 3 times.
2123

2224
// Call formatTimeDisplay with an input of 61, now answer the following:
25+
// formatTimeDisplay(61);
2326

2427
// b) What is the value assigned to num when pad is called for the first time?
2528
// =============> write your answer here
29+
// 0
2630

2731
// c) What is the return value of pad is called for the first time?
2832
// =============> write your answer here
33+
// "00"
34+
2935

3036
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3137
// =============> write your answer here
38+
// The last call is pad(remainingSeconds) and i worked out that remainingSeconds = 1, so because 61 seconds leaves 1 second after dividing by 60.
3239

3340
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
3441
// =============> write your answer here
42+
// "01" the reason being: padStart(2, "0") adds a zero to the front because the string length is only 1.

0 commit comments

Comments
 (0)