Skip to content

Commit 331a9b4

Browse files
author
Enice-Codes
committed
complete bmi.js ,cases.js and to-pounds.js
1 parent c8de3b5 commit 331a9b4

10 files changed

Lines changed: 157 additions & 4 deletions

File tree

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

Lines changed: 8 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+
// I predict that the code will throw an error because the variable `str` is being declared twice within the same scope, the first in the function perimeter.
4+
// and the second declaration is inside the function body.
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,10 @@ function capitalise(str) {
1012
}
1113

1214
// =============> write your explanation here
15+
// an error will occur because the variable`str` is being declared in the same scope,which is not allowed in javascript.
16+
1317
// =============> write your new code here
18+
function capitalise(str){
19+
let newstr = `${newstr[0].toUppercase()}${newstr.slice(1)}`;
20+
return str;
21+
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// I predict that the code will throw an error because the variable `decimalNumber` being declared twice within the same scope,in the function perimeter.
6+
// and the second declaration is inside the function body.
7+
58

69
// Try playing computer with the example to work out what is going on
710

@@ -15,6 +18,22 @@ function convertToPercentage(decimalNumber) {
1518
console.log(decimalNumber);
1619

1720
// =============> write your explanation here
21+
// in javascript its not allowed to declare variables twice in the same scope because it creates an error such as `decimalnumber`.
1822

1923
// Finally, correct the code to fix the problem
2024
// =============> write your new code here
25+
26+
function convertToPercentage(decimalNumber) {
27+
const percentage = `${ decimalNumber * 100}%`;
28+
return percentage;
29+
}
30+
console.log(convertToPercentage(0.5));;
31+
32+
33+
function convertToPercentage(decimalNumber) {
34+
const percentage = `${decimalNumber * 100}%`;
35+
return percentage;
36+
}
37+
38+
console.log(convertToPercentage(0.5));
39+

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11

22
// Predict and explain first BEFORE you run any code...
3+
// I predict that the error will be a syntax error because the parameter name in the function definition is a number (3) instead variable name.
34

45
// this function should square any number but instead we're going to get an error
5-
66
// =============> write your prediction of the error here
7+
// I predict that there will be an error when running the code because the return variable has a *.
78

89
function square(3) {
910
return num * num;
1011
}
1112

1213
// =============> write the error message here
14+
//uncaught syntax error :unexpected number .
1315

1416
// =============> explain this error message here
17+
// Javascript expects a parameter name inside the function parentheses.
18+
// In the code `function square (3)`, the number 3 is used as a parameter,
19+
// but parameter names must be valid identifiers (such as num,x or value ).
20+
// Because of this , javaScript throws the error "unexpected number".
21+
1522

1623
// Finally, correct the code to fix the problem
1724

18-
// =============> write your new code here
25+
function square (number){
26+
return num * num ;
27+
}
28+
console.log (square(3));
1929

30+
// =============> write your new code here
2031

32+
function square (number){
33+
return 3 * 3;
34+
}
35+
console.log (square()); //9

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

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

5+
// I predict there will be an error ,the outcome will however br unexpected because the multiply function uses console,log() instead of return.
6+
// ${multiply(10,32)} evaluates to undefined because the function does not return a value.
7+
58
function multiply(a, b) {
69
console.log(a * b);
710
}
@@ -10,5 +13,20 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1013

1114
// =============> write your explanation here
1215

16+
//the multiply function uses console,log() instead of return.
17+
// ${multiply(10,32)} evaluates to undefined because the function does not return a value.
18+
1319
// Finally, correct the code to fix the problem
20+
21+
function multiply(a,b){
22+
return (a*b);
23+
}
24+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
25+
1426
// =============> write your new code here
27+
28+
function multiply (a,b){
29+
return (a*b);
30+
}
31+
32+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// I predict that there will be ann error because the return variable does not have values recieved by sum function .there are values that has not variable
34

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

1112
// =============> write your explanation here
12-
// Finally, correct the code to fix the problem
13+
14+
// Finally, Correct the code to fix the problem. The function receives the values 10 and 32 as parameters a and b.
15+
// However, because return is followed by a new line, JavaScript treats it as:
16+
17+
// This causes the function to return undefined immediately.The expression a + b is never execute.
18+
// As a result , the template literal prints:
19+
"The sum of 10 and 32 is undefined".
20+
1321
// =============> write your new code here
22+
23+
function sum (a,b){
24+
return a + b;
25+
26+
}
27+
28+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
// Predict and explain first...
2+
// I predict that all 3 lines will print as last digit of the function does not use the valuable to passed into.
3+
// instead , it always uses the global variable which is 103.
24

35
// Predict the output of the following code:
46
// =============> Write your prediction here
7+
// The function getLastDigit() does not have a parameter , so it cannot use the numbers passed to it when it is called.
8+
// when getLastDigit (42), getLastDigit (105),and getLastDigit(806)are executed ,the arguments are ignored because thr function the function does not accept
9+
// any parameters instead,the function always uses the global variable num, which is 103.
10+
// 103 converted to a string is "103",and slice(-1) returns the last character ,which is "3".
11+
// therefore, the function always returns 3 regardless of the number passed in.
512

613
const num = 103;
714

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

1623
// Now run the code and compare the output to your prediction
1724
// =============> write the output here
25+
26+
// The resource https://chatgpt.com/cdn/assets/root-lmbi4tq1.css# was preloaded using link preload but not used within a few seconds from the window's load event.
27+
// Please make sure it has an appropriate `as` value and it is preloaded intentionally.
28+
(index):1
29+
1830
// Explain why the output is the way it is
1931
// =============> write your explanation here
32+
33+
// his message is a browser warning, not a JavaScript error.
34+
// The browser downloaded (preloaded) the CSS file to make the page load faster,
35+
// but the stylesheet was not used immediately after the page loaded.
36+
// Because the preloaded resource was not used within a few seconds,
37+
// Chrome displays a warning to indicate that the preload may be unnecessary
38+
// or that the resource might have the wrong "as" attribute.
39+
// The page can still work correctly even though this warning appears.
40+
2041
// Finally, correct the code to fix the problem
2142
// =============> write your new code here
2243

44+
function getLastDigit(num) {
45+
return num.toString().slice(-1);
46+
}
47+
48+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
49+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
50+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
51+
52+
2353
// This program should tell the user the last digit of each number.
2454
// Explain why getLastDigit is not working properly - correct the problem
55+
// The original version of getLastDigit was not working properly because it did not accept a parameter.
56+
// It always used a global variable instead of the number passed into the function.
57+
// by adding the parameter num,the function now recieves the number that is passed in when it is called.The function converts that number to a string
58+
// and uses slice (-1) to return the last digit.as a result ,getLastDigit(105) returns 5,and getLastDigital(806) returns 6.
59+
60+

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

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

1717
function calculateBMI(weight, height) {
1818
// return the BMI of someone based off their weight and height
19-
}
19+
Const bmi = weight/ (height * height);
20+
return Number(bmi.tofixed(1));
21+
22+
}
23+
console.log( calculateBMI(70.1.73)); //23.4

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces.
55

66
// Implement a function that:
7+
function toupperSneakCase (str){
8+
return str.toUpperCase() .replaceAll("","_");
9+
}
10+
console.log(toupperSneakCase("hello there"));
11+
//HELLO_THERE
12+
13+
console.log(toupperSneakCase("lord of the rings"));
14+
//LORD_OF_THE_RINGS
715

816
// Given a string input like "hello there"
917
// When we call this function with the input string
@@ -14,3 +22,4 @@
1422
// You will need to come up with an appropriate name for the function
1523
// Use the MDN string documentation to help you find a solution
1624
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
25+

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,27 @@
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+
const penceStringWithoutTrailingP = penceString.substring(
10+
0,
11+
penceString.length - 1
12+
);
13+
}
14+
15+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3,"0");
16+
const pounds = paddedPenceNumberString.substring (0,
17+
paddedPenceNumberString.length - 2
18+
);
19+
const pence = paddedPenceNumberString
20+
.substring(paddedPenceNumberString.length - 2)
21+
.padEnd(2,"0");
22+
23+
return${pounds}.${pence}`;
24+
25+
// Test the Function with different inputs
26+
console.log(topounds ("399p")); // £3.99
27+
console.log(topounds("45p")); //£ 0.45
28+
console.log(topounds ("7p"));//£0.07
29+
console.log (topounds("120p")); // £1.20
30+

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,22 @@ function formatTimeDisplay(seconds) {
2222

2323
// a) When formatTimeDisplay is called how many times will pad be called?
2424
// =============> write your answer here
25+
// 3 times
2526

2627
// Call formatTimeDisplay with an input of 61, now answer the following:
2728

2829
// b) What is the value assigned to num when pad is called for the first time?
2930
// =============> write your answer here
31+
//0
3032

3133
// c) What is the return value of pad is called for the first time?
3234
// =============> write your answer here
35+
//"00"
3336

3437
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3538
// =============> write your answer here
39+
//1
3640

3741
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
3842
// =============> write your answer here
43+
//"01"SSS

0 commit comments

Comments
 (0)