File tree Expand file tree Collapse file tree 10 files changed +75
-21
lines changed
Expand file tree Collapse file tree 10 files changed +75
-21
lines changed Original file line number Diff line number Diff line change 11// Predict and explain first...
2- // =============> write your prediction here
2+ // =============> Prediction: The function capitalise is possibly meant to capitalise the first letter in a string.
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,10 @@ function capitalise(str) {
99 return str ;
1010}
1111
12- // =============> write your explanation here
12+ // =============> =============> write your explanation here
13+ // The 'str' variable on the left hand side is the same used in the function name implementation which should not be.
1314// =============> write your new code here
15+ function capitalise ( str ) {
16+ let capFunction = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
17+ return capFunction ;
18+ }
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+ // =============> I suspect than an error will occur
55
66// Try playing computer with the example to work out what is going on
77
@@ -14,7 +14,10 @@ function convertToPercentage(decimalNumber) {
1414
1515console . log ( decimalNumber ) ;
1616
17- // =============> write your explanation here
18-
17+ // =============> write your explanation here. The functions takes a decimal number as input and converts it to percentage by multiplying by 100
1918// Finally, correct the code to fix the problem
2019// =============> write your new code here
20+ function convertToPercentage ( decimalNumber ) {
21+ const percentage = `${ decimalNumber * 100 } %` ;
22+ return percentage ;
23+ }
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+ // =============> write your prediction of the error here. The error is likely to be caused by square(3), '3' is supposed to be replaced by a string representing the number.
77
88function square ( 3 ) {
99 return num * num ;
1010}
1111
1212// =============> write the error message here
13-
13+ // function square(3) {
14+ // ^
15+ // SyntaxError: Unexpected number
1416// =============> explain this error message here
15-
17+ // The muber '3' should be used when calling the function and it should not be used as a parameter.
1618// Finally, correct the code to fix the problem
1719
1820// =============> write your new code here
19-
21+ function square ( num ) {
22+ return num * num ;
23+ }
2024
Original file line number Diff line number Diff line change 11// Predict and explain first...
22
3- // =============> write your prediction here
3+ // =============> write your prediction here. It may likely have issues as console.log() is within function implementation, instead of using'return'
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+ // =============> write your explanation here. Instead of having console.log() is within function implementation, it should be replaced with 'return'
1212
1313// Finally, correct the code to fix the problem
1414// =============> write your new code here
15+ function multiply ( a , b ) {
16+ return ( a * b ) ;
17+ }
Original file line number Diff line number Diff line change 11// Predict and explain first...
2- // =============> write your prediction here
2+ // =============> write your prediction here. An error will result becasue of the ';' between return and a + b
3+
34
45function sum ( a , b ) {
56 return ;
@@ -8,6 +9,9 @@ function sum(a, b) {
89
910console . log ( `The sum of 10 and 32 is ${ sum ( 10 , 32 ) } ` ) ;
1011
11- // =============> write your explanation here
12+ // =============> write your explanation here. The ideal statement should be 'return a + b; ' but this statement is separated by ';'
1213// Finally, correct the code to fix the problem
1314// =============> write your new code here
15+ function sum ( a , b ) {
16+ return a + b ;
17+ }
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+ // =============> Write your prediction here. It may run but give incorrect results because of const num & no parameter specified
5+
56
67const num = 103 ;
78
@@ -15,10 +16,19 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1516
1617// Now run the code and compare the output to your prediction
1718// =============> write the output here
19+ // The last digit of 42 is 3
20+ // The last digit of 105 is 3
21+ // The last digit of 806 is 3
1822// Explain why the output is the way it is
19- // =============> write your explanation here
23+ // =============> write your explanation here. num is a constant variable, it's value cannot change.
2024// Finally, correct the code to fix the problem
2125// =============> write your new code here
22-
26+ function getLastDigit ( ) {
27+ return num . toString ( ) . slice ( - 1 ) ;
28+ }
2329// This program should tell the user the last digit of each number.
2430// Explain why getLastDigit is not working properly - correct the problem
31+ // getLastDigit is not working properly because the value for num is fixed at '103'
32+ function getLastDigit ( num ) {
33+ return num . toString ( ) . slice ( - 1 ) ;
34+ }
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+ return Math . round ( ( weight ) / ( height ** 2 ) ) . toFixed ( 1 )
1920}
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 upCase ( strCar ) {
18+ let result = strCar . toUpperCase ( ) . replaceAll ( " " , "_" ) ;
19+ return result ;
20+ }
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 ( money ) {
9+ const moneyWithoutTrailingP = money . substring (
10+ 0 ,
11+ money . length - 1
12+ ) ;
13+ const paddedPenceNumberString = moneyWithoutTrailingP . padStart ( 3 , "0" ) ;
14+ const pounds = paddedPenceNumberString . substring (
15+ 0 ,
16+ paddedPenceNumberString . length - 2
17+ ) ;
18+ const pence = paddedPenceNumberString
19+ . substring ( paddedPenceNumberString . length - 2 )
20+ . padEnd ( 2 , "0" ) ;
21+ result = pounds + "." + pence ;
22+ res = `£${ pounds } .${ pence } ` ;
23+ return res ;
24+ }
25+ console . log ( toPounds ( "399p" ) )
26+ console . log ( toPounds ( "1099p" ) )
Original file line number Diff line number Diff line change @@ -17,18 +17,18 @@ 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+ // =============> write your answer here.pad will be called 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+ // =============> write your answer here. the value assigned to num when pad is called for the first time is 00
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. the return value of pad is called for the first time is '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. the value assigned to num when pad is called for the last time is 01
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. the return value assigned to num when pad is called for the last time in this program is '01'
You can’t perform that action at this time.
0 commit comments