Skip to content

Commit 66634e1

Browse files
committed
Sprint-2 coursework: all folders reviewed, predictions, explanations, debugged
1 parent 3372770 commit 66634e1

File tree

11 files changed

+139
-43
lines changed

11 files changed

+139
-43
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> write your prediction here
3+
// `My prediction` => The capitalise function tries to capitalize the first character of the string and concat it with the rest. This function may not run as it has error.
34

45
// call the function capitalise with a string input
6+
// `I called the capitalise function and found out there is a syntax error, we have already declared our 'str' variable in our function parameter, so its not a good idea to declare the same variable name as it ll throw an error.`
7+
58
// interpret the error message and figure out why an error is occurring
9+
// `The error occurs because we have declared the same (str) variable two times`. We can not declare two variables with the same name.
610

7-
function capitalise(str) {
11+
/*function capitalise(str) {
812
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
913
return str;
1014
}
15+
console.log(capitalise("Roman"));*/
1116

1217
// =============> write your explanation here
18+
// `to make this function run we should make we should declare two different variables in our function
19+
// .one as a parameter of the func and assign it with the above value. the second option is to return directly without creating a variable.`
1320
// =============> write your new code here
21+
function capitalized(str2){
22+
let capitalizedStr = `${str2[0].toUpperCase()}${str2.slice(1)}`;
23+
return capitalizedStr;
24+
}
25+
console.log(capitalized("hi dear"));

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
// Predict and explain first...
2+
// =====> This function tries to convert a decimal number to percentage, but I think it will not run as we have declared same variable name two times.
23

34
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
5+
// =============> It will throw an error as we have declared decimalNumber variable in our function parameter and again we declared this decimalNumber variable inside our function.
56

67
// Try playing computer with the example to work out what is going on
78

8-
function convertToPercentage(decimalNumber) {
9+
/*function convertToPercentage(decimalNumber) {
910
const decimalNumber = 0.5;
1011
const percentage = `${decimalNumber * 100}%`;
1112
1213
return percentage;
1314
}
15+
console.log(decimalNumber);*/
1416

15-
console.log(decimalNumber);
16-
17-
// =============> write your explanation here
17+
// =============> I tried to run this, it throw an error like: 'decimalNumber' has already been declared. to fix this we should remove the decimalNumber variable which we have inside our function.
1818

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

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11

22
// Predict and explain first BEFORE you run any code...
3+
// ====> As my understanding this function will throw and error as we have have not called the function with the argument 'num' .
34

4-
// this function should square any number but instead we're going to get an error
5+
// this function should square any number but instead we're going to get an error.
6+
// ====> it will not square any number but 3 when we call it with the argument 'num' inside.
57

6-
// =============> write your prediction of the error here
78

8-
function square(3) {
9+
/*function square(3) {
910
return num * num;
10-
}
11+
}*/
1112

1213
// =============> write the error message here
14+
// the error message is: SyntaxError: Unexpected number.
1315

1416
// =============> explain this error message here
17+
// after running I understood that we can not put a value in function parameter but a variable name. to make it short, in JavaScript we put variable in parameter and value as argument when calling the function.
1518

1619
// Finally, correct the code to fix the problem
1720

1821
// =============> write your new code here
22+
function square(num) {
23+
return num * num;
24+
}
25+
console.log(square(9));
1926

2027

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

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

3-
// =============> write your prediction here
3+
// =============> This function will not execute any operation, as it does not return anything.
44

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
5+
// function multiply(a, b) {
6+
// console.log(a * b);
7+
// }
88

9-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
9+
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1010

1111
// =============> write your explanation here
12+
// After running the code, I found that the function works partially, it prints lines 6 and as we called the function on line 9. but line 9 has an error to be fixed. As it does not print what we need.
13+
// also : In JavaScript, if a function doesn’t explicitly return something, it automatically returns. this is why we have got the first input ok and the second input Undefined.
1214

1315
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
16+
// =============> write your new code here;
17+
function multiply(a, b) {
18+
return (a * b);
19+
}
20+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> This function will not return the sum of a + b as the operation is located on the next line or return. so we ll have a syntax error.
33

4-
function sum(a, b) {
4+
/*function sum(a, b) {
55
return;
66
a + b;
77
}
8+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);*/
89

9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10+
// =============> after running the code I got undefined on the output as our function does not return anything. because after return keyword our function do not operate the codes.
1011

11-
// =============> write your explanation here
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+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
// Predict and explain first...
22

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> In my knowledge only the output from line 6 will be printed correctly. As for line 12, 13, and 14 it will throw error or will print undefined as we don't declare any variable for them but only value.
55

6-
const num = 103;
6+
// const num = 103;
77

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
8+
// function getLastDigit() {
9+
// return num.toString().slice(-1);
10+
// }
1111

12-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
12+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14+
// 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 output after running the code is: ---The last digit of 42 is 3 --- The last digit of 105 is 3 --- The last digit of 806 is 3;
18+
// my prediction was half correct as it worked only with line 6 but it did not throw error or undefined. here not syntax error but logical error.
19+
1820
// Explain why the output is the way it is
19-
// =============> write your explanation here
21+
// =============> Its because JavaScript only calls the function 3 times, it does not read or ignore the value inside as they are not declared.
22+
2023
// Finally, correct the code to fix the problem
2124
// =============> write your new code here
25+
function getLastDigit(num) {
26+
return num.toString().slice(-1);
27+
}
28+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
29+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
30+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2231

2332
// This program should tell the user the last digit of each number.
2433
// Explain why getLastDigit is not working properly - correct the problem
34+
// ===> it was not working with line 12, 13, and 14 because we had not declared the variable inside the function parameter, to fix the issue is to declare the num variable inside the parameter of the function. and remove the const variable of num above the function or line 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 heightSquare = height * height;
19+
let bmi = weight / heightSquare;
20+
console.log(`Your BMI is ${bmi.toFixed(1)}`)
21+
return bmi;
22+
}
23+
calculateBMI(65, 1.73); // ==> 21.7

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE"
1111

1212
// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"
13+
function upperSnakeCase(str){
14+
let snakeCase = str.replaceAll(" ", "_").toUpperCase();
15+
console.log(snakeCase);
16+
return snakeCase;
17+
}
18+
upperSnakeCase("the vampire dairies"); // "THE_VAMPIRE_DAIRIES"
1319

1420
// You will need to come up with an appropriate name for the function
1521
// Use the MDN string documentation to help you find a solution

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,20 @@
22

33
// You will need to take this code and turn it into a reusable block of code.
44
// You will need to declare a function called toPounds with an appropriately named parameter.
5-
65
// You should call this function a number of times to check it works for different inputs
6+
function toPounds(penceString) {
7+
const penceStringWithoutTrailingP =
8+
penceString.substring(0, penceString.length - 1);
9+
10+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
11+
const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
12+
13+
const pence = paddedPenceNumberString
14+
.substring(paddedPenceNumberString.length - 2)
15+
.padEnd(2, "0");
16+
17+
console.log(${pounds}.${pence}`);
18+
}
19+
toPounds("399p"); // ===> £3.99
20+
toPounds("488p"); // ===> £4.88
21+
toPounds("999p"); // ===> £9.99
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
function pad(num) {
23
return num.toString().padStart(2, "0");
34
}
@@ -7,28 +8,29 @@ function formatTimeDisplay(seconds) {
78
const totalMinutes = (seconds - remainingSeconds) / 60;
89
const remainingMinutes = totalMinutes % 60;
910
const totalHours = (totalMinutes - remainingMinutes) / 60;
10-
11+
console.log(`${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`);
1112
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1213
}
14+
formatTimeDisplay(61); // ===> 00:01:01
1315

1416
// You will need to play computer with this example - use the Python Visualiser 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?
20-
// =============> write your answer here
22+
// =============> pad function will be called `3` times.
2123

2224
// 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
27+
// =============> the num value is '0' when pad is called for the first time.
2628

2729
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here
30+
// =============> the return value of num is '00' when pad is called for the first time.
2931

3032
// 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
33+
// =============> the value of num is '1' when pad is called for the last time. to add more remainingSeconds = 61 % 60 = 1. this is why we have 1 for num value.
3234

3335
// 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
36+
// =============> the return value of num is '01' when pad is called last time. in short, remainingSeconds = 61 % 60 = 1 as num equals 1 then we have 0 from padStart(2, "0"); so the result will be "01".

0 commit comments

Comments
 (0)