Skip to content

Commit 65e1a8e

Browse files
end of Sprit-1
1 parent a8546b9 commit 65e1a8e

File tree

9 files changed

+176
-30
lines changed

9 files changed

+176
-30
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
//the function will turn the first given string into Uppercase.
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
67

78
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
9+
let strWithUpperCase = `${str[0].toUpperCase()}${str.slice(1)}`;
10+
return strWithUpperCase;
1011
}
1112

1213
// =============> write your explanation here
14+
// str[0].toUpperCase() ---> gets the first character and uses build-in method to convert a char to uppercase
15+
// str.slice(1) ----> will slice the array or sting "start from 1" to the rest of array or string
16+
// we get an error because we "re-declare" the str variable as parameter and as call back of expression
17+
1318
// =============> write your new code here
19+
20+
const myStr = "i forgot add this string";
21+
console.log(capitalise(myStr));

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5-
5+
// decimalNumber variable was declared twice. First as parameter and second as const in function body
6+
// use different variable name, either in parameter or the one in function body.
7+
// any way this function always return the same value because the variable is constant and define inside th function,
8+
// usually a function take a variable from function parameters.
9+
// console.log() will fail to print because no parameter define first.
610
// Try playing computer with the example to work out what is going on
711

812
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
13+
//-----> should be place as parameter and not as a constant ,,,,,const decimalNumber = 0.5;
1014
const percentage = `${decimalNumber * 100}%`;
1115

1216
return percentage;
1317
}
1418

15-
console.log(decimalNumber);
19+
decimalNumber = 0.27;
20+
21+
console.log(decimalNumber); //this is fine, just define the parameter it needs.
22+
console.log(convertToPercentage(decimalNumber));
1623

1724
// =============> write your explanation here
1825

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
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
6+
//the function will return square for every given number.
77

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

1212
// =============> write the error message here
13+
// ans: Unexpected number
1314

1415
// =============> explain this error message here
15-
16+
// ans: function need num to be defined, instead parameter was given 3 as literal value that does not point to any variable as parameter
1617
// Finally, correct the code to fix the problem
1718

1819
// =============> write your new code here
1920

21+
function square(num) {
22+
return num * num;
23+
}
24+
25+
mynum = 3; //define the num
2026

27+
console.log(square(mynum)); //verify the result

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

33
// =============> write your prediction here
4+
// function will return with result of multiplication of given parameters (i,e: a times b)
5+
// function multiply(a, b) {
6+
// console.log(a * b);
7+
// }
48

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
8-
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-
12+
// the function does not return any thing, so as result ${multiply(10, 32)} --> will return as "undefined"
1313
// Finally, correct the code to fix the problem
1414
// =============> write your new code here
15+
16+
function multiply(a, b) {
17+
return a * b;
18+
}
19+
20+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// sure this function wont work because, hang on.. I should predict and pretend that everything is fine
4+
// the function will return with summary of given parameters and console.log will verify it
35

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
6+
// function sum(a, b) {
7+
// return;
8+
// a + b;
9+
// }
810

9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
11+
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1012

1113
// =============> write your explanation here
14+
//function will execute line the code and escape from the function block whenever it "sees" return.. it will return with any expression put on it
15+
// in this function a+b which is expected as the result is placed after return line because return has ";" so i wont bother look the next line.
16+
// just remove the semicolon next to the return.. and put a+b instead, because some how "prettier" as trusty worthy formatter for this course will always add extra ; for every logical expression (somehow).. :) xixixixi
1217
// Finally, correct the code to fix the problem
1318
// =============> write your new code here
19+
function sum(a, b) {
20+
return a + b;
21+
}
22+
23+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,42 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// alright! from the sake of the function name, I can predict that it will successfully get the last digit
6+
// oh hang-on, where is the parameter???? this programmer must be tired or "coding while eating"... careful aye.. holiday is coming.
57

6-
const num = 103;
8+
// const num = 103;
79

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

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)}`);
14+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
15+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
16+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1517

1618
// Now run the code and compare the output to your prediction
1719
// =============> write the output here
20+
// nope.. It runs as predicted :) xixixiixi... that way we are in this "debugging" session.
21+
// it runs but no syntax error but the output are not correct.
22+
1823
// Explain why the output is the way it is
1924
// =============> write your explanation here
25+
// the function does not get any "define parameter", like nothing, so it tries to the "num" in global variable
26+
// that is given just before the function that is const num = 103;
27+
// so, every time that function is called, it will use num 103 as parameter
28+
2029
// Finally, correct the code to fix the problem
2130
// =============> write your new code here
31+
const num = 103;
32+
33+
function getLastDigit(num) {
34+
return num.toString().slice(-1);
35+
}
36+
37+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
38+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
39+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2240

2341
// This program should tell the user the last digit of each number.
2442
// Explain why getLastDigit is not working properly - correct the problem
43+
// no syntax error just logic error :) because, sometime we need to manipulate the function just like that. BTW that is not save anyway.

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,17 @@
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+
// squaring your height: 1.73 x 1.73 = 2.99
19+
let heightSquared = height * height;
20+
// dividing 70 by 2.99 = 23.41
21+
let weightMod = weight / heightSquared;
22+
// Your result will be displayed to 1 decimal place, for example 23.4.
23+
let BMI = weightMod.toFixed(1);
24+
// return the BMI of someone based off their weight and height
25+
return BMI;
26+
}
27+
28+
let myWeight = 70;
29+
let myHeight = 1.73;
30+
31+
console.log(`my BMI is ${calculateBMI(myWeight, myHeight)}`);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,17 @@
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+
let myStr = "hello world I am sleepy";
19+
20+
// let myStrUpperCase = myStr.toUpperCase();
21+
22+
// let arrWord = myStrUpperCase.split(" ");
23+
24+
// let myStrUpperCaseWithSnakeCase = arrWord.join("_");
25+
26+
//console.log(`${arrWord}`);
27+
28+
let combine = myStr.toUpperCase().split(" ").join("_");
29+
30+
console.log(`${combine}`);

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,66 @@
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+
//=============================================================
9+
// const penceString = "9p"; //init variable
10+
11+
// const penceStringWithoutTrailingP = penceString.substring(
12+
// 0,
13+
// penceString.length - 1
14+
// ); //remove p character at the end of the variable
15+
// console.log(penceStringWithoutTrailingP);
16+
17+
// const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); //
18+
// console.log(paddedPenceNumberString); //give format of 3 zeroes if the number is less than 3 digits.
19+
20+
// const pounds = paddedPenceNumberString.substring(
21+
// 0,
22+
// paddedPenceNumberString.length - 2
23+
// ); //get ponds value conversion changes for given pence
24+
25+
// console.log(pounds);
26+
27+
// const pence = paddedPenceNumberString
28+
// .substring(paddedPenceNumberString.length - 2)
29+
// .padEnd(2, "0"); //get the reminds after conversion to pounds
30+
31+
// console.log(`£${pounds}.${pence}`);
32+
33+
//------------------------------------------------------------------------------------------
34+
35+
function pensToPounds(penceString) {
36+
const penceStringWithoutTrailingP = penceString.substring(
37+
0,
38+
penceString.length - 1
39+
); //remove p character at the end of the variable
40+
//console.log(penceStringWithoutTrailingP);
41+
42+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); //
43+
//console.log(paddedPenceNumberString); //give format of 3 zeroes if the number is less than 3 digits.
44+
45+
const pounds = paddedPenceNumberString.substring(
46+
0,
47+
paddedPenceNumberString.length - 2
48+
); //get ponds value conversion changes for given pence
49+
50+
//console.log(pounds);
51+
52+
const pence = paddedPenceNumberString
53+
.substring(paddedPenceNumberString.length - 2)
54+
.padEnd(2, "0"); //get the reminds after conversion to pounds
55+
56+
return ${pounds}.${pence}`;
57+
}
58+
59+
//test----1
60+
let moneyA = "90p";
61+
console.log(`${pensToPounds(moneyA)}`);
62+
63+
//test----2
64+
moneyA = "710p";
65+
console.log(`${pensToPounds(moneyA)}`);
66+
67+
//test----3
68+
moneyA = "1210p";
69+
console.log(`${pensToPounds(moneyA)}`);

0 commit comments

Comments
 (0)