Skip to content

Commit ab3d116

Browse files
Sprint 2: complete coursework tasks
1 parent 3372770 commit ab3d116

File tree

11 files changed

+217
-19
lines changed

11 files changed

+217
-19
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
// Predict and explain first...
1+
// Predict and explain first...
2+
//Syntax error
23
// =============> write your prediction here
4+
// We pass str as a parameter to the function.
5+
//The parameter becomes a local variable inside the function scope.
6+
//Therefore, we cannot declare another variable with the same name inside the same scope.
37

48
// call the function capitalise with a string input
59
// interpret the error message and figure out why an error is occurring
@@ -10,4 +14,16 @@ function capitalise(str) {
1014
}
1115

1216
// =============> write your explanation here
17+
// The parameter str is already a local variable inside the function.
18+
// Previously, redeclaring it using let caused an error because variables cannot be declared twice in the same scope.
19+
// Instead of redeclaring it, I modified the existing str variable and returned it.
20+
// This avoids the redeclaration error and the code runs correctly.
1321
// =============> write your new code here
22+
function capitalise(str) {
23+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
24+
return str;
25+
}
26+
let str = capitalise("Arun");
27+
console.log(str);
28+
29+

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// Predict and explain first...
2-
2+
// syntax error identifier decimalNumber be already declare
3+
// there is no function call and then you can't use the local variable decimalNumber out of the function
34
// Why will an error occur when this program runs?
5+
//decimalNumber is already declared as a parameter, so declaring it again inside the function causes an error.
6+
7+
//There is no function call.
8+
9+
//You cannot use the local variable decimalNumber outside the function scope.
410
// =============> write your prediction here
511

612
// Try playing computer with the example to work out what is going on
@@ -11,10 +17,24 @@ function convertToPercentage(decimalNumber) {
1117

1218
return percentage;
1319
}
14-
1520
console.log(decimalNumber);
1621

1722
// =============> write your explanation here
23+
//The error occurred because a local variable was redeclared inside the function using a variable keyword. Since the parameter already acts as a local variable, redeclaring it caused an error.
24+
25+
//To fix this, I removed the variable keyword and modified the existing parameter instead.
26+
27+
//I also removed the unused variable from console.log because it was outside the function scope.
1828

29+
//Finally, I called the function directly inside console.log using convertToPercentage() so the function executes properly and returns the correct value.
1930
// Finally, correct the code to fix the problem
2031
// =============> write your new code here
32+
function convertToPercentage(decimalNumber) {
33+
34+
const percentage = `${decimalNumber * 100}%`;
35+
36+
return percentage;
37+
}
38+
39+
const decimalNumber = convertToPercentage(0.5) ;
40+
console.log(decimalNumber);

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
11

22
// Predict and explain first BEFORE you run any code...
33

4+
// The code has a syntax error because a number is used instead of a parameter name in the function definition.
5+
// Additionally, num is used inside the function without being declared or passed as an argument, which causes a ReferenceError.
6+
// If the function is not called, it will not execute.
47
// this function should square any number but instead we're going to get an error
58

69
// =============> write your prediction of the error here
710

11+
// The code has a syntax error because a number is used instead of a parameter name in the function definition.
12+
// Additionally, num is used inside the function without being declared or passed as an argument, which causes a ReferenceError.
13+
// If the function is not called, it will not execute.
14+
815
function square(3) {
916
return num * num;
1017
}
1118

19+
20+
1221
// =============> write the error message here
22+
// syntax error :unexpected number.
1323

1424
// =============> explain this error message here
25+
// The error occurs because a number is used in the function definition instead of a parameter name. A function definition must contain a parameter (a variable name), not a value.
26+
27+
// Inside the function, num is used but it was never declared or passed as a parameter, which causes a ReferenceError because num is not defined in the scope.
28+
29+
// Also, if the function is not called, it will not execute.
1530

1631
// Finally, correct the code to fix the problem
32+
// I corrected the function by properly defining num as a parameter instead of using a number in the function definition. This resolved the syntax error.
33+
34+
// Inside the function, num is now defined, so the ReferenceError is fixed.
1735

36+
// I then called the function with an argument and stored the returned value in a variable named num. Although the same variable name is used, the function parameter and the outer variable exist in different scopes, so there is no conflict.
1837
// =============> write your new code here
1938

2039

40+
function square(num) {
41+
return num * num;
42+
}
43+
let num = square(3);
44+
console.log(num);

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
// Predict and explain first...
22

33
// =============> write your prediction here
4+
// The function multiplies the numbers and prints the answer.
5+
6+
// But it does not return the answer.
7+
8+
// Because there is no return, the function automatically gives back undefined.
9+
10+
// So when we use the function inside the second console.log, the value is undefined.
411

512
function multiply(a, b) {
613
console.log(a * b);
14+
715
}
816

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

1119
// =============> write your explanation here
20+
// console.log and return are not the same.
21+
22+
// console.log only prints the value to the console.
23+
24+
// It does not send the value back to the function call.
25+
26+
// Because there was no return statement, the function automatically returned undefined.
1227

13-
// Finally, correct the code to fix the problem
28+
// To fix the issue, I replaced console.log with return.
29+
30+
// By returning a * b, the function now sends the calculated value back to where it was called.
31+
32+
// As a result, the correct value is displayed instead of undefined.
1433
// =============> write your new code here
34+
function multiply(a, b) {
35+
return (a * b);
36+
37+
}
38+
39+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
// The function returns nothing because the return statement has no value.
4+
// Once JavaScript reaches return, the function stops executing.
5+
// Therefore, the line after it is dead code and never runs.
6+
// Since the function does not return a value, it returns undefined, which is why the template string displays undefined.
47
function sum(a, b) {
58
return;
69
a + b;
@@ -9,5 +12,14 @@ 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 function now correctly returns the result of a + b.
16+
// Since the addition is included in the return statement, the function sends back the calculated value.
17+
// Therefore, when the function is called inside the template string, it displays the correct result, 42.
1218
// Finally, correct the code to fix the problem
1319
// =============> write your new code here
20+
21+
function sum(a, b) {
22+
return a + b;
23+
}
24+
25+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 37 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+
// The output will be 3 for every function call.
6+
// This is because the function does not have a parameter, so it ignores the values passed in the function calls.
7+
// Instead, it uses the global variable num, which is set to 103.
8+
// The last digit of 103 is 3, so the function always returns 3.
9+
// variable num inside function take the input from the global variable num in the outside of the function
510

611
const num = 103;
712

@@ -15,10 +20,42 @@ 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
1826
// Explain why the output is the way it is
1927
// =============> write your explanation here
28+
// The function getLastDigit does not have a parameter.
29+
// Even though values like 42, 105, and 806 are passed when calling the function, they are ignored.
30+
31+
// Inside the function, the variable num refers to the global variable num, which is set to 103.
32+
33+
// Each time the function runs, it converts 103 to a string and extracts the last character using slice(-1).
34+
35+
// The last digit of 103 is "3".
36+
37+
// Therefore, every function call returns "3".
2038
// Finally, correct the code to fix the problem
2139
// =============> write your new code here
2240

41+
function getLastDigit(num) {
42+
return num.toString().slice(-1);
43+
}
44+
45+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
46+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
47+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
48+
2349
// This program should tell the user the last digit of each number.
2450
// Explain why getLastDigit is not working properly - correct the problem
51+
// In the previous version, the function was not working properly because it used a global variable instead of a parameter.
52+
// Since the function did not define a parameter, it ignored the values passed during the function calls and always used the same global value.
53+
54+
// In the corrected version, the global variable was removed and a parameter num was added to the function definition.
55+
// Now, the function receives the argument passed during each function call and correctly returns the last digit of that number.
56+
57+
// Therefore, the output becomes:
58+
59+
// The last digit of 42 is 2
60+
// The last digit of 105 is 5
61+
// The last digit of 806 is 6

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@
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+
let bmi = weight / (height * height);
19+
bmi = bmi.toFixed(1);
20+
return Number(bmi);
21+
22+
}
23+
console.log(calculateBMI(62 , 1.8));

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@
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 upperSnakeCase(str){
19+
str = str.toUpperCase()
20+
21+
return str.replaceAll(" " , "_");
22+
;
23+
24+
}
25+
console.log(upperSnakeCase("hello there"));
26+
console.log(upperSnakeCase("lord of the rings"));

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

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

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,36 @@ function formatTimeDisplay(seconds) {
88
const remainingMinutes = totalMinutes % 60;
99
const totalHours = (totalMinutes - remainingMinutes) / 60;
1010

11-
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
11+
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1212
}
1313

14-
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
14+
console.log(formatTimeDisplay(61))
15+
16+
// You will need to play computer with this example - use the Python Visualizer https://pythontutor.com/visualize.html#mode=edit
1517
// to help you answer these questions
1618

1719
// Questions
1820

1921
// a) When formatTimeDisplay is called how many times will pad be called?
2022
// =============> write your answer here
21-
22-
// Call formatTimeDisplay with an input of 61, now answer the following:
23+
// ans: 3 times
24+
// Call formatTimeDisplay with an input of 61, now answer the following:
2325

2426
// b) What is the value assigned to num when pad is called for the first time?
25-
// =============> write your answer here
26-
27-
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here
27+
// =============> write your answer here : 0
28+
// c) What is the return value of pad is called for the first time?
29+
// =============> write your answer here value : 00
2930

3031
// 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
32-
32+
// =============> write your answer here : 1 When formatTimeDisplay(61) is called, the value 61 is passed as the argument to seconds.
33+
// Then remainingSeconds is calculated using seconds % 60, which gives 1.
34+
// The last call to pad is pad(remainingSeconds).
35+
// Since remainingSeconds is 1, the value assigned to num in the last call is 1.
3336
// 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
37+
// =============> write your answer here : 01 When formatTimeDisplay(61) is called, the value 1 is passed to pad during the last call because remainingSeconds equals 1.
38+
// Inside the pad function:
39+
// The number 1 is converted to a string.
40+
// padStart(2, "0") ensures the string has at least two characters.
41+
// Since "1" has only one character, a "0" is added to the beginning.
42+
// So "1" becomes "01".
43+
// This ensures the time format follows the 00:00:00 structure.

0 commit comments

Comments
 (0)