Skip to content

Commit 2f56348

Browse files
committed
Completed Sprint-2
1 parent 3372770 commit 2f56348

File tree

10 files changed

+96
-3
lines changed

10 files changed

+96
-3
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// This function is trying to capitalise the first letter of a string.
4+
// it takes the first character using str[0], converts it to uppercase,and joins it with the rest of the string using str.slice(1).
35

46
// call the function capitalise with a string input
57
// interpret the error message and figure out why an error is occurring
@@ -10,4 +12,9 @@ function capitalise(str) {
1012
}
1113

1214
// =============> write your explanation here
13-
// =============> write your new code here
15+
// The error is occurring because the function is trying to declare a variable with the same name as the parameter, which is not allowed in JavaScript.
16+
// The variable `str` is being redeclared inside the function, causing a syntax error.
17+
function capitalise(str) {
18+
let result = str[0].toUpperCase() + str.slice(1);
19+
return result;
20+
}

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

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

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// I predict this function is trying to convert a decimal number
6+
// into a percentage. It multiples the decimal number by 100 and
7+
// then it adds the % symbol to the end of the number.
58

69
// Try playing computer with the example to work out what is going on
710

@@ -15,6 +18,18 @@ function convertToPercentage(decimalNumber) {
1518
console.log(decimalNumber);
1619

1720
// =============> write your explanation here
21+
// The error happens becouse 'decimalNumber' is already the parameter
22+
// of the function. Inside the function the code tries to declare 'decimalNumber'
23+
// again using the const, which is not allowed.
24+
25+
// a other problem is that console.log(decimalNumber) is outside
26+
// the function but 'decimalNumber' only exists inside the function.
1827

1928
// Finally, correct the code to fix the problem
2029
// =============> write your new code here
30+
function convertToPercentage(decimalNumber) {
31+
const percentage = `${decimalNumber * 100}%`;
32+
return percentage;
33+
}
34+
35+
console.log(convertToPercentage(0.5));

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,27 @@
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+
// I predict an error will happen becouse the function uses 3 as the parameter
8+
// but parameters should be varible names. Also the code uses "num" inside the
9+
// function even though it was never defiend.
710

811
function square(3) {
912
return num * num;
1013
}
1114

1215
// =============> write the error message here
16+
// SyntaxError: Unexpected number
1317

1418
// =============> explain this error message here
19+
// The error happens becouse 3 is used as the function parameter but parameters
20+
// must be varible names. Also the code tries to use "num" inside the function even
21+
// though it was never dec;ared.
1522

1623
// Finally, correct the code to fix the problem
1724

1825
// =============> write your new code here
26+
function square(num) {
27+
return num * num;
28+
}
1929

2030

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

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

33
// =============> write your prediction here
4+
// I predict the program will print 320 first.
5+
// Then it will print "the result of multipying 10 and 32 is undefined"
6+
// becouse the function logs the result but does not return it.
47

58
function multiply(a, b) {
69
console.log(a * b);
@@ -9,6 +12,14 @@ function multiply(a, b) {
912
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1013

1114
// =============> write your explanation here
15+
// The problem is that the function uses console.log insted ofreturning the value.
16+
// Becouse the function does not return anything, the value of multiply(10, 32)
17+
// becomes undefined when it is used insdie the temple string.
1218

1319
// Finally, correct the code to fix the problem
1420
// =============> write your new code here
21+
function multiply(a, b) {
22+
return a * b;
23+
}
24+
25+
console.log('The result of multiplying 10 and 32 is ' + multiply(10, 32));

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// I predict the program will print "The sum of 10 and 32 is underfined"
4+
// becouse the return statment does not return a value and the addition
5+
// is written on the next line.
36

47
function sum(a, b) {
58
return;
@@ -9,5 +12,12 @@ function sum(a, b) {
912
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1013

1114
// =============> write your explanation here
15+
// The problem is that the return us written on its own line.
16+
// Becouse of this, the function returns undefined before executing a + b.
1217
// Finally, correct the code to fix the problem
1318
// =============> write your new code here
19+
function sum(a,b) {
20+
return a + b;
21+
}
22+
23+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// I predict the program will print:
6+
// The last digit of 42 is 3
7+
// The last digit of 105 is 3
8+
// The last digit of 806 is 3
9+
// This happens because the function always uses the variable num which is 103.
510

611
const num = 103;
712

@@ -15,10 +20,25 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1520

1621
// Now run the code and compare the output to your prediction
1722
// =============> write the output here
23+
// The last digit of 42 is 3
24+
//The last digit of 105 is 3
25+
// The last digit of 806 is 3
26+
1827
// Explain why the output is the way it is
1928
// =============> write your explanation here
29+
// The problem is that the function always uses the varible num which is 103.
30+
// Becouse of this, the last digit is always 3.
31+
// The numbers passed into the function (42, 105, 806) are ignored.
32+
2033
// Finally, correct the code to fix the problem
2134
// =============> write your new code here
35+
function getLastDigit(num) {
36+
return num.toString().slice(-1);
37+
}
38+
39+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
40+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
41+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2242

2343
// This program should tell the user the last digit of each number.
2444
// Explain why getLastDigit is not working properly - correct the problem

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
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+
const bmi = weight / (height * height);
19+
return bmi.toFixed(1);
20+
}
21+
console.log(calculateBMI(70, 1.73));
22+
// return the BMI of someone based off their weight and height

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+
18+
function toUpperSnakeCase(str) {
19+
return str.replaceAll(" ", "_").toUpperCase();
20+
}
21+
console.log(toUpperSnakeCase("hello there"));

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@
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(pence) {
9+
return "£" + (pence / 100).toFixed(2);
10+
}
11+
console.log(toPounds(399));
12+
console.log(toPounds(250));
13+
console.log(toPounds(100));

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

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

1919
// a) When formatTimeDisplay is called how many times will pad be called?
2020
// =============> write your answer here
21+
// It will be called 3 times becouse it is used three times in the return line.
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?
2526
// =============> write your answer here
27+
// The value is 0 becouse totalHours is 0 when the input is 61 seconds.
2628

2729
// c) What is the return value of pad is called for the first time?
2830
// =============> write your answer here
31+
// The return value is "00" beccouse padStart adds a 0 in front to make it two digits.
2932

3033
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3134
// =============> write your answer here
35+
// The value is 1 becouse remainingSeconds is 1 when the input is 61 seconds.
3236

3337
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
3438
// =============> write your answer here
39+
// The return value is "01" becouse padStart adds a 0 at the beginning ti make it two digits.

0 commit comments

Comments
 (0)