File tree Expand file tree Collapse file tree 11 files changed +90
-29
lines changed
Expand file tree Collapse file tree 11 files changed +90
-29
lines changed Original file line number Diff line number Diff line change 11// Predict and explain first...
2- // =============> write your prediction here
2+ // =============> Capitalises the first letter of the str
33
44// call the function capitalise with a string input
55// interpret the error message and figure out why an error is occurring
@@ -9,5 +9,7 @@ function capitalise(str) {
99 return str ;
1010}
1111
12- // =============> write your explanation here
13- // =============> write your new code here
12+ capitalise ( "mohsen" ) ;
13+
14+ // =============> variable str is declared again inside function
15+ // =============> str = `${str[0].toUpperCase()}${str.slice(1)}`;
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+ // =============> Instead of calling the function, a variable is called
55
66// Try playing computer with the example to work out what is going on
77
@@ -14,7 +14,7 @@ function convertToPercentage(decimalNumber) {
1414
1515console . log ( decimalNumber ) ;
1616
17- // =============> write your explanation here
17+ // =============> Also inside the function, decimalNumber is called again
1818
1919// Finally, correct the code to fix the problem
20- // =============> write your new code here
20+ // =============> decimalNumber = 0.5;
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
6+ // =============> The variable num is not declared
77
88function square ( 3 ) {
99 return num * num ;
1010}
1111
12- // =============> write the error message here
12+ // =============>SyntaxError: Unexpected number
1313
14- // =============> explain this error message here
14+ // =============> There must a variable declared, not just simply passing a number as an argument
1515
1616// Finally, correct the code to fix the problem
1717
18- // =============> write your new code here
18+ // =============> function square(num) {
1919
2020
Original file line number Diff line number Diff line change 11// Predict and explain first...
22
3- // =============> write your prediction here
3+ // =============> the result of the multiply is printed inside function, and at the function call is will be undefined
44
55function multiply ( a , b ) {
66 console . log ( a * b ) ;
77}
88
99console . log ( `The result of multiplying 10 and 32 is ${ multiply ( 10 , 32 ) } ` ) ;
1010
11- // =============> write your explanation here
11+ // =============> because function is not returning any value
1212
1313// Finally, correct the code to fix the problem
14- // =============> write your new code here
14+ // =============> return a * b;
Original file line number Diff line number Diff line change 11// Predict and explain first...
2- // =============> write your prediction here
2+ // =============> undefined will be printed
33
44function sum ( a , b ) {
55 return ;
@@ -8,6 +8,6 @@ function sum(a, b) {
88
99console . log ( `The sum of 10 and 32 is ${ sum ( 10 , 32 ) } ` ) ;
1010
11- // =============> write your explanation here
11+ // =============> because the function returns nothing
1212// Finally, correct the code to fix the problem
13- // =============> write your new code here
13+ // =============> return a + b;
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
4+ // =============> the last digit of every function call will be 3
55
66const num = 103 ;
77
8- function getLastDigit ( ) {
8+ function getLastDigit ( num ) {
99 return num . toString ( ) . slice ( - 1 ) ;
1010}
1111
@@ -16,9 +16,10 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1616// Now run the code and compare the output to your prediction
1717// =============> write the output here
1818// Explain why the output is the way it is
19- // =============> write your explanation here
19+ // =============> function is using the num declared before it , not the number passed to it
2020// Finally, correct the code to fix the problem
21- // =============> write your new code here
21+ // =============> function getLastDigit(num) {
2222
2323// This program should tell the user the last digit of each number.
2424// Explain why getLastDigit is not working properly - correct the problem
25+ // Variable num used inside the function, should be passed as an argument
Original file line number Diff line number Diff line change 1515// It should return their Body Mass Index to 1 decimal place
1616
1717function 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+ const bmi = weight / height ** 2 ;
20+ return bmi . 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+ function makeUpperSnakeCase ( str ) {
18+ return str . toUpperCase ( ) . split ( " " ) . join ( "_" ) ;
19+ }
Original file line number Diff line number Diff line change 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+ const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
15+ const pounds = paddedPenceNumberString . substring (
16+ 0 ,
17+ paddedPenceNumberString . length - 2
18+ ) ;
19+
20+ const pence = paddedPenceNumberString
21+ . substring ( paddedPenceNumberString . length - 2 )
22+ . padEnd ( 2 , "0" ) ;
23+
24+ return `£${ pounds } .${ pence } ` ;
25+ }
Original file line number Diff line number Diff line change @@ -17,18 +17,20 @@ 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+ // =============> 3
21+ formatTimeDisplay ( 61 ) ;
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?
25- // =============> write your answer here
26+ // =============> 0
2627
2728// c) What is the return value of pad is called for the first time?
28- // =============> write your answer here
29+ // =============> It is called return value and will be '00'
2930
3031// 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
32+ // =============> The function is passed 61 which means 1 minute and 1 second so the value passed to pad for the last call is 1
3233
3334// 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
35+ // =============> 1 is passed to function as a number and stored in num then num is turned into a string and one '0' will be added
36+ // to the beginning of num so the returned value will be '01'
You can’t perform that action at this time.
0 commit comments