Skip to content

Commit bd1f87c

Browse files
committed
sprint 2
1 parent b31a586 commit bd1f87c

10 files changed

Lines changed: 94 additions & 26 deletions

File tree

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> code will produce a SyntaxError the error will occur because the parameter str is being declared again with let str inside the function, which is not allowed
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
13-
// =============> write your new code here
12+
// =============> The error message says that str has already been declared this happens because str is already the function parameter,and the code tries to create another variable called str using let JavaScript does not allow a parameter and a let variable to have the same name in the same scope
13+
function capitalise(str) {
14+
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
15+
return capitalisedStr;
16+
}
17+
18+
console.log(capitalise("hello"));

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

Lines changed: 9 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+
// =============> this program will produce a SyntaxError because decimalNumber is declared twice inside the function
55

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

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

1515
console.log(decimalNumber);
1616

17-
// =============> write your explanation here
17+
// =============> The error happens because decimalNumber is already a parameter of the function, so we cannot declare it again using const
1818

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

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

Lines changed: 8 additions & 5 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+
// =============> will cause a SyntaxError because function parameters must be variable names, not values like 3
77

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

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

14-
// =============> explain this error message here
14+
// =============> This error happens because 3 is not a valid parameter name in JavaScript function parameters must be variable names like num, not fixed values num is used inside the function but was never defined, so JavaScript throws an error
1515

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

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

23+
console.log(square(3));

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

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

3-
// =============> write your prediction here
3+
// =============> will show undefined because the function does not return a value
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+
// =============> the function only uses console.log() so it prints the result but does not return it when a function has no return, JavaScript returns undefined,which is why undefined appears in the final sentence
1212

1313
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
14+
// =============>
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 & 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+
// =============> The sum of 10 and 32 is undefined because the function returns nothing
33

44
function sum(a, b) {
55
return;
@@ -8,6 +8,11 @@ function sum(a, b) {
88

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

11-
// =============> write your explanation here
11+
// =============> The problem is that the semicolon after return ends the function immediately. This means that a + b never runs, so the function returns undefined instead of the sum
1212
// Finally, correct the code to fix the problem
1313
// =============> write your new code here
14+
function sum(a, b) {
15+
return a + b;
16+
}
17+
18+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 13 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+
// =============> all three lines will output 3 as the last digit,even though the numbers are different
55

66
const num = 103;
77

@@ -14,11 +14,21 @@ 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+
// =============> The last digit of 42 is 3
18+
// The last digit of 105 is 3
19+
// The last digit of 806 is 3
1820
// Explain why the output is the way it is
19-
// =============> write your explanation here
21+
// =============> The function does not use the numbers passed into it instead, it always uses the variable num which is set to 103. The last digit of 103 is 3, so the function always returns 3
2022
// Finally, correct the code to fix the problem
2123
// =============> write your new code here
2224

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

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

Lines changed: 3 additions & 2 deletions
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
19-
}
18+
return (weight / (height * height)).toFixed(1);
19+
// return the BMI of someone based off their weight and height
20+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@
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+
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,33 @@
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+
function toPounds(penceString) {
8+
// takes the p from the end of the string
9+
const penceStringWithoutTrailingP = penceString.substring(
10+
0,
11+
penceString.length - 1
12+
);
13+
14+
// the number must have at least 3 digits
15+
// 8 becomes 008
16+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
17+
18+
// Get everything except the last 2 digits for pounds
19+
const pounds = paddedPenceNumberString.substring(
20+
0,
21+
paddedPenceNumberString.length - 2
22+
);
23+
24+
// Get the last 2 digits for pence
25+
const pence = paddedPenceNumberString
26+
.substring(paddedPenceNumberString.length - 2)
27+
.padEnd(2, "0");
28+
29+
// Return the result in pounds format
30+
return ${pounds}.${pence}`;
31+
}
32+
33+
console.log(toPounds("399p")); // £3.99
34+
console.log(toPounds("45p")); // £0.45
35+
console.log(toPounds("8p")); // £0.08
36+
console.log(toPounds("1234p")); // £12.34

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

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

2323
// a) When formatTimeDisplay is called how many times will pad be called?
24-
// =============> write your answer here
24+
// =============> 3 times hours minutes and seconds
2525

2626
// Call formatTimeDisplay with an input of 61, now answer the following:
2727

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

3131
// c) What is the return value of pad is called for the first time?
32-
// =============> write your answer here
32+
// =============> 00
3333

3434
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35-
// =============> write your answer here
35+
// =============> The remaining seconds are calculated using % 60, so when the input is 61, the result is 1 second left over
3636

3737
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38-
// =============> write your answer here
38+
// =============> 01 function adds a leading zero to any number that is less than 10 and time will be in two digits

0 commit comments

Comments
 (0)