File tree Expand file tree Collapse file tree 10 files changed +110
-33
lines changed Expand file tree Collapse file tree 10 files changed +110
-33
lines changed Original file line number Diff line number Diff line change 11// Predict and explain first...
2- // =============> write your prediction here
2+ // =============> my explanation: I see here declaration of str variable, which already exist as an argument. It will cause an error.
33
44// call the function capitalise with a string input
55// interpret the error message and figure out why an error is occurring
6-
6+ //===============>SyntaxError: Identifier 'str' has already been declared, its occured while execution of code.
7+ /*
78function capitalise(str) {
89 let str = `${str[0].toUpperCase()}${str.slice(1)}`;
910 return str;
1011}
11-
12- // =============> write your explanation here
12+ */
13+ // =============> write your explanation here. My explanation: We have to change the name of variable and return it in order to fix an error.
1314// =============> write your new code here
15+ function capitalise ( str ) {
16+ let strChanged = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
17+ return strChanged ;
18+ }
19+ console . log ( capitalise ( "some string" ) ) ;
Original file line number Diff line number Diff line change 11// Predict and explain first...
22
33// Why will an error occur when this program runs?
4- // =============> write your prediction here
4+ // =============> write your prediction here. My prediction: Value decimalNumber is a parameter to the convertToPercentage. We don't need to declare it at line 10.
5+ // =============> Also at line 16, we have to call function coverToPercentage with an argument.
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}
1415
1516console.log(decimalNumber);
16-
17- // =============> write your explanation here
17+ */
18+ // =============> write your explanation here. My explanation: We need to modify this function. I'm going to remove line where decimalNumber is declared and execute the function properly.
1819
1920// Finally, correct the code to fix the problem
2021// =============> write your new code here
22+
23+ function convertToPercentage ( decimalNumber ) {
24+ const percentage = `${ decimalNumber * 100 } %` ;
25+ return percentage ;
26+ }
27+
28+ console . log ( convertToPercentage ( 0.1 ) ) ;
Original file line number Diff line number Diff line change 33
44// this function should square any number but instead we're going to get an error
55
6- // =============> write your prediction of the error here
7-
6+ // =============> write your prediction of the error here My prediction: There is an error with parameter.
7+ /*
88function square(3) {
99 return num * num;
1010}
11+ console.log(square)
12+ */
13+ // =============> write the error message here: SyntaxError: Unexpected number
1114
12- // =============> write the error message here
13-
14- // =============> explain this error message here
15+ // =============> explain this error message here: An error because of invalid parameter.
1516
1617// Finally, correct the code to fix the problem
1718
1819// =============> write your new code here
20+ function square ( num ) {
21+ return num * num ;
22+ }
23+ console . log ( square ( 3 ) )
1924
2025
Original file line number Diff line number Diff line change 11// Predict and explain first...
22
3- // =============> write your prediction here
4-
3+ // =============> write your prediction here: This function return nothing.
4+ /*
55function multiply(a, b) {
66 console.log(a * b);
77}
88
99console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
10-
11- // =============> write your explanation here
10+ */
11+ // =============> write your explanation here: We need to modify this function.
12+ // =============> Create a new variable where we have to do the following calculation, after that return this new variable.
1213
1314// Finally, correct the code to fix the problem
1415// =============> write your new code here
16+ function multiply ( a , b ) {
17+ resultOfMultiply = a * b ;
18+ return resultOfMultiply
19+ }
20+
21+ console . log ( `The result of multiplying 10 and 32 is ${ multiply ( 10 , 32 ) } ` ) ;
Original file line number Diff line number Diff line change 11// Predict and explain first...
2- // =============> write your prediction here
3-
2+ // =============> write your prediction here: wrong calculation and return nothing.
3+ /*
44function sum(a, b) {
55 return;
66 a + b;
77}
88
99console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10-
11- // =============> write your explanation here
10+ */
11+ // =============> write your explanation here: We need to create a variable where we have to store our calculation and return it.
1212// Finally, correct the code to fix the problem
13- // =============> write your new code here
13+ // =============> write your new code
14+ function sum ( a , b ) {
15+ resultOfAdding = a + b ;
16+ return resultOfAdding
17+ }
18+
19+ console . log ( `The sum of 10 and 32 is ${ sum ( 10 , 32 ) } ` ) ;
Original file line number Diff line number Diff line change 11// Predict and explain first...
22
33// Predict the output of the following code:
4- // =============> Write your prediction here
5-
4+ // =============> Write your prediction here: Function getLastDigit return num.
5+ /*
66const num = 103;
77
88function getLastDigit() {
99 return num.toString().slice(-1);
10+
1011}
1112
1213console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1314console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1415console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15-
16+ */
1617// Now run the code and compare the output to your prediction
17- // =============> write the output here
18+ // =============> write the output here: output is last digit of variable num.
1819// Explain why the output is the way it is
19- // =============> write your explanation here
20+ // =============> write your explanation here: Because num is global and function's parameter is absent .
2021// Finally, correct the code to fix the problem
2122// =============> write your new code here
23+ const num = 103 ;
24+
25+ function getLastDigit ( num ) {
26+ let digit = num . toString ( ) . slice ( - 1 ) ;
27+ return digit
28+ }
29+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
30+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
31+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
2232
2333// This program should tell the user the last digit of each number.
2434// Explain why getLastDigit is not working properly - correct the problem
Original file line number Diff line number Diff line change 1616
1717function calculateBMI ( weight , height ) {
1818 // return the BMI of someone based off their weight and height
19- }
19+ resultBmi = weight / Math . pow ( height , 2 ) ;
20+ return resultBmi . toFixed ( 1 ) ;
21+ }
22+ console . log ( calculateBMI ( 70 , 1.73 ) ) ;
Original file line number Diff line number Diff line change 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 ( text ) {
19+ let textUpper = text . toUpperCase ( ) ;
20+ return textUpper . replace ( " " , "_" ) ;
21+ }
22+ console . log ( upperSnakeCase ( "hello here" ) ) ;
Original file line number Diff line number Diff line change 1+ /*
12// In Sprint-1, there is a program written in interpret/to-pounds.js
23
34// You will need to take this code and turn it into a reusable block of code.
45// You will need to declare a function called toPounds with an appropriately named parameter.
56
67// You should call this function a number of times to check it works for different inputs
8+ */
9+
10+ function toPounds ( penceString ) {
11+ let penceStringWithoutTrailingP ;
12+ if ( penceString . charAt ( penceString . length - 1 ) == "p" ) {
13+ penceStringWithoutTrailingP = penceString . substring ( 0 , penceString . length - 1 ) ;
14+ } else {
15+ penceStringWithoutTrailingP = penceString ;
16+ }
17+ const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
18+ const pounds = paddedPenceNumberString . substring ( 0 , paddedPenceNumberString . length - 2 ) ;
19+
20+ const pence = paddedPenceNumberString . substring ( paddedPenceNumberString . length - 2 ) . padEnd ( 2 , "0" ) ;
21+
22+ console . log ( `£${ pounds } .${ pence } ` ) ;
23+ }
24+ toPounds ( "200" ) ;
25+ toPounds ( "200p" ) ;
26+ toPounds ( "20" ) ;
27+ toPounds ( "2" ) ;
Original file line number Diff line number Diff line change @@ -17,18 +17,23 @@ function formatTimeDisplay(seconds) {
1717// Questions
1818
1919// a) When formatTimeDisplay is called how many times will pad be called?
20- // =============> write your answer here
20+ // =============> // three times
2121
2222// Call formatTimeDisplay with an input of 61, now answer the following:
2323
2424// b) What is the value assigned to num when pad is called for the first time?
25- // =============> write your answer here
25+ // =============> 61%60=1/61-1/60=1/60%60=0, the value assigned to num when pad called for the first time is 0.
2626
2727// c) What is the return value of pad is called for the first time?
28- // =============> write your answer here
28+ // =============> write your answer here: "00".
2929
3030// 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
31+ // =============> write your answer here: remainingSeconds 61%60=1.
3232
3333// 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
34+ // =============> write your answer here: "01" because of .padStart(2, "0")
35+ console . log ( pad ( 0 ) ) ;
36+ console . log ( pad ( 1 ) ) ;
37+ console . log ( pad ( 123 ) ) ;
38+ console . log ( formatTimeDisplay ( 61 ) ) ;
39+
You can’t perform that action at this time.
0 commit comments