You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// =============> It should take a string and capitalise the first letter but i predict it will throw an
3
3
4
4
// call the function capitalise with a string input
5
5
// interpret the error message and figure out why an error is occurring
6
6
7
+
/*The error occurs because str is declared twice. The function already receives str as a parameter, and then let str tries to create another variable with the same name inside the same scope. JavaScript does not allow redeclaring a variable with let, so it throws an error.*/
8
+
7
9
functioncapitalise(str){
8
10
letstr=`${str[0].toUpperCase()}${str.slice(1)}`;
9
11
returnstr;
10
12
}
11
13
12
14
// =============> write your explanation here
15
+
16
+
/* This function takes a string and capitalise the first letter. It uses `str[0].toUpperCase()` to convert the first character to uppercase and `str.slice(1)` to get the rest of the word unchanged. The function then joins both parts together and returns the new string. When `console.log(capitalise("hello"), capitalise("world"))` is run, the function executes twice and prints `Hello World`.
Copy file name to clipboardExpand all lines: Sprint-3/1-key-errors/1.js
+10-1Lines changed: 10 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,4 @@
1
1
// Predict and explain first...
2
-
3
2
// Why will an error occur when this program runs?
4
3
// =============> write your prediction here
5
4
@@ -16,5 +15,15 @@ console.log(decimalNumber);
16
15
17
16
// =============> write your explanation here
18
17
18
+
/*An error will occur when the program runs because decimalNumber is declared twice. The function already receives decimalNumber as a parameter, but then const decimalNumber = 0.5 tries to create another variable with the same name inside the same scope. JavaScript does not allow redeclaring variables with const.
19
+
Another error will happen at console.log(decimalNumber) because decimalNumber only exists inside the function and cannot be accessed outside of it.*/
Copy file name to clipboardExpand all lines: Sprint-3/2-mandatory-debug/0.js
+8Lines changed: 8 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
// Predict and explain first...
2
2
3
3
// =============> write your prediction here
4
+
/*The code will print a correct multiplication inside the function*/
4
5
5
6
functionmultiply(a,b){
6
7
console.log(a*b);
@@ -9,6 +10,13 @@ function multiply(a, b) {
9
10
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
10
11
11
12
// =============> write your explanation here
13
+
/*The result shows undefined because the function prints the answer with console.log instead of returning it, so nothing is passed back into the template string.*/
12
14
13
15
// Finally, correct the code to fix the problem
14
16
// =============> write your new code here
17
+
18
+
functionmultiply(a,b){
19
+
returna*b;
20
+
}
21
+
22
+
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
Copy file name to clipboardExpand all lines: Sprint-3/2-mandatory-debug/2.js
+23Lines changed: 23 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,11 @@
2
2
3
3
// Predict the output of the following code:
4
4
// =============> Write your prediction here
5
+
/*The output will be:
6
+
7
+
The last digit of 42 is 3
8
+
The last digit of 105 is 3
9
+
The last digit of 806 is 3*/
5
10
6
11
constnum=103;
7
12
@@ -15,10 +20,28 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
20
16
21
// Now run the code and compare the output to your prediction
17
22
// =============> write the output here
23
+
24
+
/*The last digit of 42 is 3
25
+
The last digit of 105 is 3
26
+
The last digit of 806 is 3*/
27
+
18
28
// Explain why the output is the way it is
19
29
// =============> write your explanation here
30
+
31
+
/*The function always returns the last digit of 103 because it ignores the input parameter and uses a fixed global variable instead of the number passed into it.*/
32
+
20
33
// Finally, correct the code to fix the problem
21
34
// =============> write your new code here
22
35
36
+
functiongetLastDigit(number){
37
+
returnnumber.toString().slice(-1);
38
+
}
39
+
40
+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
41
+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
42
+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
43
+
23
44
// This program should tell the user the last digit of each number.
24
45
// Explain why getLastDigit is not working properly - correct the problem
46
+
47
+
/*The function is not working properly because it ignores the input values and always uses the global variable num (which is 103), so every result returns the last digit of 103 instead of the number passed into the function.*/
Copy file name to clipboardExpand all lines: Sprint-3/4-mandatory-interpret/time-format.js
+7-1Lines changed: 7 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -22,17 +22,23 @@ function formatTimeDisplay(seconds) {
22
22
23
23
// a) When formatTimeDisplay is called how many times will pad be called?
24
24
// =============> write your answer here
25
+
// a) 3 times
25
26
26
27
// Call formatTimeDisplay with an input of 61, now answer the following:
27
28
28
29
// b) What is the value assigned to num when pad is called for the first time?
29
30
// =============> write your answer here
31
+
// b) 0
30
32
31
-
// c) What is the return value of pad when it is called for the first time?
33
+
// c) What is the return value of pad is called for the first time?
32
34
// =============> write your answer here
35
+
// c) "00"
33
36
34
37
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35
38
// =============> write your answer here
39
+
// d) 1, because the last time pad is called it is for the remaining seconds which is 1 second in this case
36
40
37
41
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38
42
// =============> write your answer here
43
+
44
+
// e) "01", because the last time pad is called it is for the remaining seconds which is 1 second in this case, and pad adds a leading zero to make it two digits
0 commit comments