Skip to content

Commit 5abf1f5

Browse files
committed
Done coursework/sprint-2
1 parent 3372770 commit 5abf1f5

File tree

10 files changed

+91
-5
lines changed

10 files changed

+91
-5
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// The program will give an error.The error happens because `str`is written twice inside the function.
4+
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,14 @@ function capitalise(str) {
1012
}
1113

1214
// =============> write your explanation here
15+
// The function already has an input called `str`
16+
// Inside the function, the code tries to create another variable called `str`using `let`
17+
//JavaScript does not allow the same varible name to be created again in same place.
18+
//Because of this, the program throwns an error.
19+
1320
// =============> write your new code here
21+
22+
function captialise(str){
23+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
24+
return str;
25+
}

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

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

6+
// The program will give an error because of the `decimalNumber` is used outside the function.
7+
// Even though it is only created inside the function.
8+
69
// Try playing computer with the example to work out what is going on
710

811
function convertToPercentage(decimalNumber) {
@@ -16,5 +19,15 @@ console.log(decimalNumber);
1619

1720
// =============> write your explanation here
1821

22+
//The variable `decimalNumber`is only insdie the function.
23+
// At the end of the program, console.log( decimalNumber) tries to print it ,
24+
// however, JavaScript cannot find that varible outside the function.
25+
// Because of this, the program throws an error.
26+
1927
// Finally, correct the code to fix the problem
2028
// =============> write your new code here
29+
function convertTopercentage(decimalNumber) {
30+
const percentage = `${decimalNumber * 100}%`;
31+
return percentage;
32+
}
33+
console.log(convertToPercentage(0.5));

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11

22
// Predict and explain first BEFORE you run any code...
3+
// The program will give a SyntazError because a number (3) is used as peramter name in the function.
34

45
// this function should square any number but instead we're going to get an error
56

67
// =============> write your prediction of the error here
8+
// My perdiction is that program will not run and show a SyntaxError because 3 is not a valid variable name.
79

810
function square(3) {
911
return num * num;
1012
}
1113

1214
// =============> write the error message here
15+
// SyntaxError: Unexpected number
1316

1417
// =============> explain this error message here
18+
// Funtion parameters must be variable names.
19+
// You cannot use a number like 3 as name of parameter.
20+
//Javascript required a valid varible name inside the parentheses.
1521

1622
// Finally, correct the code to fix the problem
1723

1824
// =============> write your new code here
25+
function square(num) {
26+
return num * num;
27+
}
1928

20-
29+
console.log(square(3));

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

Lines changed: 12 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+
// The program will print 320 first.
5+
// Then it will " The result of mutiplying 10 and 32 is undefined".
6+
47

58
function multiply(a, b) {
69
console.log(a * b);
@@ -9,6 +12,15 @@ 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 function multiply is used console.log(a * b) to show the result
16+
// however, it does not resturn the value .
17+
// when the function is used inside the sentence with ${multiply(10, 32)}
18+
// JavaScript expects the function to give back a value.
19+
//Becuse the function does not return anything, the result becomes undefined.
1220

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
//My perdiction the code will show "undefined" becuse the function does not return the sum numbers.
34

45
function sum(a, b) {
56
return;
@@ -9,5 +10,12 @@ function sum(a, b) {
910
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1011

1112
// =============> write your explanation here
13+
// The return statemment is ematy and the "c=b" is written after return.
14+
//When JavaScript see return it stops the function, so a+b is never used.
15+
1216
// Finally, correct the code to fix the problem
1317
// =============> write your new code here
18+
function sum(a, b) {
19+
return a + b;
20+
}
21+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Predict and explain first...
2-
32
// Predict the output of the following code:
43
// =============> Write your prediction here
4+
//The code will always show 3 as the last digit because the function does not use the number inside getLastDigit().
55

66
const num = 103;
77

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

1616
// Now run the code and compare the output to your prediction
1717
// =============> write the output here
18+
// The last digit of 42 is 3
19+
// The last digit of 105 is 3
20+
// The last digit of 806 is 3
21+
1822
// Explain why the output is the way it is
1923
// =============> write your explanation here
24+
// The function does not take any input parameter, it always uses the global variavle num = 103.
25+
// Because of this the last digit returend is always 3, regardless of the number passed in the function call.
26+
2027
// Finally, correct the code to fix the problem
2128
// =============> write your new code here
29+
function getLastDigit(num) {
30+
return num.toString().slice(-1);
31+
}
32+
console.log(`The last digit of 42 is $(getlastDigita(42)}`);
33+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
34+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2235

2336
// This program should tell the user the last digit of each number.
2437
// Explain why getLastDigit is not working properly - correct the problem
38+
// The function was fixed by adding a parameter so it can return the last digit of the given number.

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

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
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 convertToUpperSnakeCase(str) {
19+
return str.toUpperCase().trim().split("").join("_");
20+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@
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(kilograms) {
9+
const pounds = kilograms * 2.20462;
10+
return Number(pounds.toFixed(2));
11+
}

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

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

1919
// a) When formatTimeDisplay is called how many times will pad be called?
2020
// =============> write your answer here
21+
// Pad wil be called 3 times because the return statement uses pad for hours, mintues and seconds.
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 because 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" because padStart makes the number two digits by adding a 0 at the start.
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
32-
35+
// The value is 1 because the last call to pad uses remainingSeconds, which is 1 when the input is 61.
3336
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
3437
// =============> write your answer here
38+
// The return value is "01" because padStart adds a 0 infornt of the single digit number.

0 commit comments

Comments
 (0)