File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11// Predict and explain first...
2- // =============> write your prediction here
2+ // ===========> I predict there will be an error when the function is called.
33
44// call the function capitalise with a string input
55// interpret the error message and figure out why an error is occurring
66
77function capitalise ( str ) {
8- let str = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
8+ str = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
99 return str ;
1010}
11-
12- // =============> write your explanation here
13- // =============> write your new code here
11+ capitalise ( "hello" ) ;
12+ // ===========> The error happens because str has already been declared as a function parameter.
13+ // ===========> Removed let because str is already declared as a function parameter.
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 predict there will be an error because decimalNumber is declared more than once.
55
66// Try playing computer with the example to work out what is going on
77
88function convertToPercentage ( decimalNumber ) {
9- const decimalNumber = 0.5 ;
109 const percentage = `${ decimalNumber * 100 } %` ;
1110
1211 return percentage ;
1312}
1413
15- console . log ( decimalNumber ) ;
14+ console . log ( convertToPercentage ( 0.5 ) ) ;
1615
17- // =============> write your explanation here
16+ // ===========> The error happens because decimalNumber is already declared as a function parameter.
1817
1918// Finally, correct the code to fix the problem
20- // =============> write your new code here
19+ // ===========> Removed the duplicate declaration and called the function with 0.5.
Original file line number Diff line number Diff line change 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
6- // =============> write your prediction of the error here
5+ // ===========> I predict there will be a syntax error because 3 cannot be used as a function parameter.
76
8- function square ( 3 ) {
9- return num * num ;
7+ function square ( num ) {
8+ return num * num ;
109}
1110
12- // =============> write the error message here
11+ // ===========> SyntaxError: Unexpected number
1312
14- // =============> explain this error message here
13+ // ===========> The error happens because a number cannot be used as a function parameter name.
1514
1615// Finally, correct the code to fix the problem
1716
18- // =============> write your new code here
19-
20-
17+ // ===========> Changed 3 to num so the function has a valid parameter name.
Original file line number Diff line number Diff line change 11// Predict and explain first...
22
3- // =============> write your prediction here
3+ // ===========> I predict the multiplication will print 320, but the final result will show undefined.
44
55function multiply ( a , b ) {
6- console . log ( a * b ) ;
6+ return a * b ;
77}
88
99console . log ( `The result of multiplying 10 and 32 is ${ multiply ( 10 , 32 ) } ` ) ;
1010
11- // =============> write your explanation here
11+ // ===========> The function prints the result instead of returning it, so the function returns undefined.
1212
1313// Finally, correct the code to fix the problem
14- // =============> write your new code here
14+ // ===========> Changed console.log to return so the function returns the multiplication result.
Original file line number Diff line number Diff line change 11// Predict and explain first...
2- // =============> write your prediction here
2+ // ===========> I predict the result will be undefined because the function returns before adding the numbers.
33
44function sum ( a , b ) {
5- return ;
6- a + b ;
5+ return a + b ;
76}
87
98console . log ( `The sum of 10 and 32 is ${ sum ( 10 , 32 ) } ` ) ;
109
11- // =============> write your explanation here
10+ // ===========> The function returns before a + b is calculated, so the result is undefined.
1211// Finally, correct the code to fix the problem
13- // =============> write your new code here
12+ // ===========> Changed the return statement so it returns 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+ // ===========> I predict all three results will be 3 because the function always uses num, which is 103.
55
6- const num = 103 ;
7-
8- function getLastDigit ( ) {
6+ function getLastDigit ( num ) {
97 return num . toString ( ) . slice ( - 1 ) ;
108}
119
@@ -15,10 +13,11 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1513
1614// Now run the code and compare the output to your prediction
1715// =============> write the output here
18- // Explain why the output is the way it is
16+ // ============> The output is 3 for all three numbers.
17+ // ============> The function ignores the numbers passed to it and always uses num, which is 103.
1918// =============> write your explanation here
2019// Finally, correct the code to fix the problem
21- // =============> write your new code here
20+ // ============> Added num as a parameter so the function uses the number passed to it.
2221
2322// This program should tell the user the last digit of each number.
2423// Explain why getLastDigit is not working properly - correct the problem
Original file line number Diff line number Diff line change 1515// It should return a string of 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
18+ const bmi = weight / ( height * height ) ;
19+ return bmi . 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 toUpperSnakeCase ( str ) {
18+ const upperCaseString = str . toUpperCase ( ) ;
19+ const snakeCaseString = upperCaseString . replaceAll ( " " , "_" ) ;
20+ return snakeCaseString ;
21+ }
22+ console . log ( toUpperSnakeCase ( "hello there" ) ) ;
23+ console . log ( toUpperSnakeCase ( "lord of the rings" ) ) ;
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+ function toPounds ( pence ) {
8+ return pence / 100 ;
9+ }
10+
11+ console . log ( toPounds ( 100 ) ) ;
12+ console . log ( toPounds ( 250 ) ) ;
13+ console . log ( toPounds ( 500 ) ) ;
Original file line number Diff line number Diff line change @@ -21,18 +21,18 @@ function formatTimeDisplay(seconds) {
2121// Questions
2222
2323// a) When formatTimeDisplay is called how many times will pad be called?
24- // =============> write your answer here
24+ // ============> 3 times
2525
2626// Call formatTimeDisplay with an input of 61, now answer the following:
2727
2828// b) What is the value assigned to num when pad is called for the first time?
29- // =============> write your answer here
29+ // ============> 0
3030
3131// c) What is the return value of pad when it is called for the first time?
32- // =============> write your answer here
32+ // ============> "00"
3333
34- // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35- // =============> write your answer here
34+ // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35+ // ============> 1, because remainingSeconds is 1.
3636
37- // e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38- // =============> write your answer here
37+ // e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38+ // ============> "01", because pad adds a 0 to make the value two characters long.
You can’t perform that action at this time.
0 commit comments