-
-
Notifications
You must be signed in to change notification settings - Fork 398
London | 26-ITP-May | Ebrahim Moqbel | Sprint 2 | course work #1582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
51a060d
795be43
2b4173b
c77e1c3
e829acf
14fee9a
d424070
2eaa851
1c37d58
4d8de06
69791bc
97abaaa
3fcb050
2efa343
4435e7f
c234195
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,32 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| /* | ||
| I predict the function will throw a SyntaxError as the variable str has already been declared in the function parameter | ||
| function capitalise will capitalise the first letter with index 0 | ||
| and will append the sliced string from index 1 which is the second letter | ||
| */ | ||
|
|
||
| // call the function capitalise with a string input | ||
| // interpret the error message and figure out why an error is occurring | ||
| //call the function capitalise with a string input | ||
|
|
||
| // interpret the error message and figure out why an error is occurring : | ||
|
|
||
| /* syntax Error :identifier the variabel has already been declared. | ||
| As we can see the str variable has already been declared in the prameter of the function instead we just return the value */ | ||
|
|
||
| // function capitalise(str) { | ||
| // let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // console.log(str); | ||
| // return str; | ||
| // } | ||
|
|
||
|
|
||
| // =============> write your explanation here | ||
| /* syntax Error :identifier the variabel has already been declared. | ||
| As we can see the str variable has already been declared in the prameter of the function instead we just return the value */ | ||
|
|
||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
| return str[0].toUpperCase()+str.slice(1); | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| } | ||
| console.log(capitalise("ebrahim")); | ||
| console.log(capitalise('salomi')); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,35 @@ | ||
| // Predict and explain first... | ||
|
|
||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
|
|
||
| //1- The function is trying to declare the decimalNumber twice in the perameter and later as a new variable | ||
| //2-console.log() is printing a variable in the local scope where it can't be accessed as it is inside the function | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| // function convertToPercentage(decimalNumber) { | ||
| // const decimalNumber = 0.5; | ||
| // const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| // return percentage; | ||
| // } | ||
|
|
||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
| /* The variable decimal Number has already been declared in the parameter | ||
| and already is a local variable which can't be declared again causing a ReferenceError | ||
| As the decimalNumber inside the function we can't print it using the console.log() as it has no power to the local scope | ||
| instead we can delete the second declaration and just leave the parameter as an input for the user | ||
| Finally, correct the code to fix the problem */ | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function convertToPercentage(decimalNumber) { | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| return percentage; | ||
| } | ||
|
|
||
| console.log(convertToPercentage(0.5)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,17 +4,26 @@ | |
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // =============> write your prediction of the error here | ||
| //I predict the funciton will throw a SyntaxEror as the perametters sould be valid variables like names or identifiers with upholding to the variable naming convention and in this case a number found | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| // function square(3) { | ||
| // return num * num; | ||
| // } | ||
|
|
||
| // =============> write the error message here | ||
| //the Error is a syntax error: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| /* The function was givin a number as its perameter where in JS only allows varaible names and usnig a number causes a SyntaxError | ||
| Also, the function was returning num*num where num is undefined causing a ReferenceError | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here. |
||
| */ | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
|
|
||
| console.log(square(25)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,23 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // This function would multiply the two arguments a and b whatever that is | ||
| // when we log the output we will get the string and the output of the function but because we are not returning an output we might run into an error | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
| // function multiply(a, b) { | ||
| // console.log(a * b); | ||
| // } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| // console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| //The console.log() inside the funtion will print a*b but when we call the function will return undefined | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function multiply(a, b) { | ||
| return a * b; | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,23 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // The function suppose to return the sum of the two variables but we will encounter an error as the code after return is unreachable. | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| // function sum(a, b) { | ||
| // return; | ||
| // a + b; | ||
| // } | ||
| // console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // As we return nothing by closing the line with a semicolon hence a + b is not returned where we tell the computer to exit and go back to global scope and the computer will not be able to read the lines after that | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| function sum(a, b) { | ||
| return a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,9 @@ | |
| // You will need to come up with an appropriate name for the function | ||
| // Use the MDN string documentation to help you find a solution | ||
| // This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase | ||
| function toUpperSnakeFormat(string){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename to match the vocabulary brief in the link, i.e There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coming back to this one. The function is still called |
||
| return string.toUpperCase().replaceAll(" ","_") | ||
|
|
||
| } | ||
| console.log(toUpperSnakeFormat("hello there")) | ||
| console.log(toUpperSnakeFormat("lord of the rings")) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,24 +15,28 @@ function formatTimeDisplay(seconds) { | |
| return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; | ||
| } | ||
|
|
||
| console.log(formatTimeDisplay(61)) | ||
| // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit | ||
| // to help you answer these questions | ||
|
|
||
| // Questions | ||
|
|
||
| // a) When formatTimeDisplay is called how many times will pad be called? | ||
| // =============> write your answer here | ||
| // =============> 3 times | ||
|
|
||
| // Call formatTimeDisplay with an input of 61, now answer the following: | ||
|
|
||
| // b) What is the value assigned to num when pad is called for the first time? | ||
| // =============> write your answer here | ||
| // =============> the first time pad was called with the totalHours | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Answer with the value, not the variable name. Run |
||
|
|
||
| // c) What is the return value of pad is called for the first time? | ||
| // =============> write your answer here | ||
| // =============> "00" | ||
|
|
||
|
|
||
| // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer | ||
| // =============> write your answer here | ||
| // =============> "1" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check where |
||
| // the last time, pad() is called with the remaining seconds which is value "1" | ||
|
|
||
| // e) What is the return value of pad when it is called for the last time in this program? Explain your answer | ||
| // =============> write your answer here | ||
| // =============> "01" | ||
| // the return value is 1 put as we are using pad wouldbe adding leading "0" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try running the original code. That's a SyntaxError, not a ReferenceError, and it stops the file before anything runs, so the
console.logproblem you predicted never happens. Update your explanation to match.