Skip to content

Commit c7c13a9

Browse files
Complete key errors, mandatory and interpret tasks for Sprint 2
1 parent 3372770 commit c7c13a9

File tree

11 files changed

+224
-10
lines changed

11 files changed

+224
-10
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// Predict and explain first...
22
// =============> write your prediction here
33

4+
// Prediction:
5+
// The code will throw an error because `str` is declared twice:
6+
// once as the function parameter and again with `let str` inside the function.
7+
// JavaScript does not allow redeclaring the same identifier in the same scope.
8+
49
// call the function capitalise with a string input
510
// interpret the error message and figure out why an error is occurring
611

@@ -10,4 +15,20 @@ function capitalise(str) {
1015
}
1116

1217
// =============> write your explanation here
18+
19+
// Explanation:
20+
// When the file runs, JavaScript gives a SyntaxError like:
21+
// "Identifier 'str' has already been declared".
22+
// This happens because `str` already exists as a parameter, so `let str` tries
23+
// to redeclare it in the same scope. To fix it, we should not redeclare `str`.
24+
// Instead, we can create a new variable or just return the new string.
25+
1326
// =============> write your new code here
27+
28+
function capitalise(str) {
29+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
30+
return str;
31+
}
32+
33+
console.log(capitalise("hello")); // Hello
34+

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
55

6+
// Prediction:
7+
// An error will occur because `decimalNumber` is declared twice.
8+
// It is first declared as a function parameter and then declared again
9+
// inside the function using `const decimalNumber = 0.5;`.
10+
// JavaScript does not allow redeclaring the same variable in the same scope.
11+
// Also, `console.log(decimalNumber);` is outside the function,
12+
// so `decimalNumber` is not defined in the global scope.
13+
614
// Try playing computer with the example to work out what is going on
715

816
function convertToPercentage(decimalNumber) {
@@ -16,5 +24,21 @@ console.log(decimalNumber);
1624

1725
// =============> write your explanation here
1826

27+
// Explanation:
28+
// The parameter `decimalNumber` is already defined in the function.
29+
// Declaring `const decimalNumber = 0.5;` again causes a SyntaxError:
30+
// "Identifier 'decimalNumber' has already been declared".
31+
// Additionally, `console.log(decimalNumber);` causes a ReferenceError
32+
// because `decimalNumber` only exists inside the function and
33+
// cannot be accessed outside of it.
34+
1935
// Finally, correct the code to fix the problem
2036
// =============> write your new code here
37+
38+
function convertToPercentage(decimalNumber) {
39+
const percentage = `${decimalNumber * 100}%`;
40+
return percentage;
41+
}
42+
43+
const result = convertToPercentage(0.5);
44+
console.log(result); // 50%

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
1-
21
// Predict and explain first BEFORE you run any code...
32

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
76

7+
// Prediction:
8+
// An error will occur because `3` is not a valid parameter name.
9+
// Function parameters must be variable names, not numbers.
10+
// JavaScript will throw a SyntaxError before running the program.
11+
812
function square(3) {
913
return num * num;
1014
}
1115

1216
// =============> write the error message here
1317

18+
// Error message:
19+
// SyntaxError: Unexpected number
20+
1421
// =============> explain this error message here
1522

23+
// Explanation:
24+
// The error occurs because `3` is used as the function parameter.
25+
// In JavaScript, function parameters must be identifiers (variable names).
26+
// A number cannot be used as a parameter name.
27+
// Because of this invalid syntax, JavaScript throws a SyntaxError
28+
// before the program can run.
29+
// Additionally, the variable `num` is used inside the function
30+
// but is not defined, which would cause another error
31+
// if the syntax error did not occur first.
32+
1633
// Finally, correct the code to fix the problem
1734

1835
// =============> write your new code here
1936

37+
function square(num) {
38+
return num * num;
39+
}
40+
41+
console.log(square(3)); // 9
42+
43+
44+
45+
2046

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,41 @@
22

33
// =============> write your prediction here
44

5+
// Prediction:
6+
// The program will print 320,
7+
// but it will also print "undefined" in the sentence.
8+
// This happens because the function uses console.log()
9+
// instead of returning a value.
10+
// If a function does not return anything,
11+
// JavaScript automatically returns undefined.
12+
513
function multiply(a, b) {
614
console.log(a * b);
715
}
816

917
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1018

19+
1120
// =============> write your explanation here
1221

22+
// Explanation:
23+
// The function multiply(a, b) prints the result using console.log(),
24+
// but it does not return the result.
25+
// When multiply(10, 32) is used inside the template string,
26+
// JavaScript expects a returned value.
27+
// Because nothing is returned, the value becomes undefined.
28+
// That is why the output shows:
29+
//
30+
// 320
31+
// The result of multiplying 10 and 32 is undefined
32+
33+
1334
// Finally, correct the code to fix the problem
1435
// =============> write your new code here
36+
37+
function multiply(a, b) {
38+
return a * b;
39+
}
40+
41+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);//320
42+

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
// Predict and explain first...
22
// =============> write your prediction here
33

4+
// Prediction:
5+
// The program will print "The sum of 10 and 32 is undefined".
6+
// This happens because the return statement is written incorrectly.
7+
// JavaScript stops executing a function immediately after "return".
8+
// Since nothing is returned, the function returns undefined.
9+
410
function sum(a, b) {
511
return;
612
a + b;
713
}
814

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

17+
1118
// =============> write your explanation here
19+
20+
// Explanation:
21+
// In JavaScript, when "return" is written on its own line,
22+
// the function stops immediately and returns undefined.
23+
// The line "a + b;" is never executed.
24+
// That is why sum(10, 32) becomes undefined.
25+
// The issue is caused by the line break after return.
26+
27+
1228
// Finally, correct the code to fix the problem
1329
// =============> write your new code here
30+
31+
function sum(a, b) {
32+
return a + b;
33+
}
34+
35+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);//32

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
// Predict the output of the following code:
44
// =============> Write your prediction here
55

6+
// Prediction:
7+
// The function will always return "3".
8+
// Even though we pass 42, 105, and 806 into the function,
9+
// it will ignore those values and use the global variable `num = 103`.
10+
// Therefore, every line will print 3 as the last digit.
11+
612
const num = 103;
713

814
function getLastDigit() {
@@ -13,12 +19,43 @@ console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1319
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1420
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1521

22+
1623
// Now run the code and compare the output to your prediction
1724
// =============> write the output here
25+
26+
// Output:
27+
// The last digit of 42 is 3
28+
// The last digit of 105 is 3
29+
// The last digit of 806 is 3
30+
31+
1832
// Explain why the output is the way it is
1933
// =============> write your explanation here
34+
35+
// Explanation:
36+
// The function getLastDigit does not accept a parameter.
37+
// Because of that, it ignores the values passed inside the parentheses.
38+
// Instead, it always uses the global constant `num`, which is 103.
39+
// The function converts 103 to a string and takes the last character,
40+
// which is "3". That is why every output is 3.
41+
42+
2043
// Finally, correct the code to fix the problem
2144
// =============> write your new code here
2245

46+
function getLastDigit(number) {
47+
return number.toString().slice(-1);
48+
}
49+
50+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);//2
51+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);//5
52+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);//6
53+
54+
2355
// This program should tell the user the last digit of each number.
2456
// Explain why getLastDigit is not working properly - correct the problem
57+
58+
// The function was not working properly because it did not use a parameter.
59+
// It was hard-coded to use the global variable `num` instead of the value
60+
// passed into the function. By adding a parameter (number),
61+
// the function now works correctly for any input.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@
1616

1717
function calculateBMI(weight, height) {
1818
// return the BMI of someone based off their weight and height
19-
}
19+
let bmi =weight/(height*height);
20+
return bmi.toFixed(1)}
21+
console.log(calculateBMI(88, 1.78));//27.8
22+

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@
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+
22+
console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE"
23+
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
24+
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
// In Sprint-1, there is a program written in interpret/to-pounds.js
2-
32
// You will need to take this code and turn it into a reusable block of code.
43
// You will need to declare a function called toPounds with an appropriately named parameter.
5-
64
// You should call this function a number of times to check it works for different inputs
5+
6+
function toPounds(penceString) {
7+
const penceStringWithoutTrailingP = penceString.substring(
8+
0,
9+
penceString.length - 1
10+
);
11+
12+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
13+
14+
const pounds = paddedPenceNumberString.substring(
15+
0,
16+
paddedPenceNumberString.length - 2
17+
);
18+
19+
const pence = paddedPenceNumberString
20+
.substring(paddedPenceNumberString.length - 2)
21+
.padEnd(2, "0");
22+
23+
return ${pounds}.${pence}`;
24+
}
25+
26+
// Call the function a number of times to check it works for different inputs
27+
console.log(toPounds("399p")); // £3.99
28+
console.log(toPounds("5p")); // £0.05
29+
console.log(toPounds("45p")); // £0.45
30+
console.log(toPounds("120p")); // £1.20
31+
console.log(toPounds("0p")); // £0.00

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

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

14+
// Calling the function with 61 as the argument
15+
console.log(formatTimeDisplay(61));
16+
17+
18+
1419
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1520
// to help you answer these questions
1621

1722
// Questions
1823

1924
// a) When formatTimeDisplay is called how many times will pad be called?
20-
// =============> write your answer here
25+
// =============> pad will be called 3 times.
2126

2227
// Call formatTimeDisplay with an input of 61, now answer the following:
2328

2429
// b) What is the value assigned to num when pad is called for the first time?
25-
// =============> write your answer here
30+
// =============> The value is 0.
2631

2732
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here
33+
// =============> The return value is "00".
2934

3035
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
31-
// =============> write your answer here
36+
// =============> The value is 1 because 61 % 60 leaves 1 second remaining.
3237

3338
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
34-
// =============> write your answer here
39+
// =============> The return value is "01" because return num.toString().padStart(2, "0") formats 1 into "01".

0 commit comments

Comments
 (0)