-
-
Notifications
You must be signed in to change notification settings - Fork 546
London | 25-ITP-SEP | Shirin Panahian | sprint 3 | sprint 3 Acoursework #1596
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
2fe022a
ec7939c
e54ba0f
567f5ad
f3b91eb
f6a29d4
c80dd5d
b138a74
528fd79
ae508fc
31e4589
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,21 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // function parameter and variable inside the function have same name. | ||
|
|
||
| // call the function capitalise with a string input | ||
| console.log(capitalise("message")); | ||
| // interpret the error message and figure out why an error is occurring | ||
|
|
||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
| /*Identifier 'str' has already been declared . | ||
| This mean that 'str' has already been declared as a function parameter, | ||
| so we cannot declare another variable with the same name using 'let'. | ||
| */ | ||
|
|
||
| // =============> write your explanation here | ||
| // The error occurs because 'str' is already declare as a function parameter. | ||
| //The 'let str' inside the function tries to declare 'str' again in the same scope. | ||
| //I changed the name of new variable to 'result' to fix the error. | ||
|
|
||
| // =============> write your new code here | ||
| function capitalise(str) { | ||
| let result = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return result; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,27 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
| // when we run the code, I predict that we will got the error in function argument | ||
| //because it is not a variable | ||
|
|
||
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // =============> write your prediction of the error here | ||
| // | ||
|
|
||
| function square(3) { | ||
| /*function square(3) { | ||
| return num * num; | ||
| } | ||
| }*/ | ||
|
|
||
| // =============> write the error message here | ||
| // SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // The error occurs because 3 is a number, not a valid variable name. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| const num = 3; | ||
| function square(num) { | ||
| return num * num; | ||
| } | ||
| console.log(square(num)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,24 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| //when we run the code, I predict that the function just execute but doesn't return any thing. | ||
| // function just print the result of multiply | ||
| // I am not sure what going to be show in the second console.log | ||
|
|
||
| function multiply(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 | ||
| // After running the code, the multiply function display the result (320) | ||
| // but it does not return result back to the code | ||
| // therefore, multiply(10, 32) evaluate 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,21 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> I think the position of return inside the function is wrong | ||
| //probably it is show nothing | ||
|
|
||
| function sum(a, b) { | ||
| /*function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); */ | ||
|
|
||
| // =============> write your explanation here | ||
| // After running code it is show "The sum of 10 and 32 is undefined" | ||
| // because return has no value so it return undefined. | ||
|
|
||
| // 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 |
|---|---|---|
| @@ -1,24 +1,39 @@ | ||
| // Predict and explain first... | ||
| // In my idea we can not change value of num as it is defined by const | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| //perhaps we will get value definition error | ||
|
|
||
| const num = 103; | ||
| /*const num = 103; | ||
|
|
||
| function getLastDigit() { | ||
| return num.toString().slice(-1); | ||
| } | ||
|
|
||
| console.log(`The last digit of 42 is ${getLastDigit(42)}`); | ||
| console.log(`The last digit of 105 is ${getLastDigit(105)}`); | ||
| console.log(`The last digit of 806 is ${getLastDigit(806)}`); | ||
| console.log(`The last digit of 806 is ${getLastDigit(806)}`);*/ | ||
|
|
||
| // Now run the code and compare the output to your prediction | ||
| // =============> write the output here | ||
| // The last digit of 42 is 3 | ||
| //The last digit of 105 is 3 | ||
| //The last digit of 806 is 3 | ||
|
|
||
| // Explain why the output is the way it is | ||
| // =============> write your explanation here | ||
| // I was wrong in all the function call, the result is the same because the function always uses the global num. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| let num = 103; | ||
|
|
||
| function getLastDigit(num) { | ||
| return num.toString().slice(-1); | ||
| } | ||
|
|
||
| console.log(`The last digit of 42 is ${getLastDigit(42)}`); | ||
| console.log(`The last digit of 105 is ${getLastDigit(105)}`); | ||
| console.log(`The last digit of 806 is ${getLastDigit(806)}`); | ||
|
|
||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,10 @@ | |
| // 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 toUpperSnakeCase(text) { | ||
| const newText = text.replace(" ", "_"); | ||
|
Contributor
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. Try |
||
| return newText.toUpperCase(); | ||
| } | ||
| console.log( | ||
| `The upper snake case of "hello there" is ${toUpperSnakeCase("hello there")}`, | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,23 @@ | |
| // You will need to declare a function called toPounds with an appropriately named parameter. | ||
|
|
||
| // You should call this function a number of times to check it works for different inputs | ||
| function toPound(penceString) { | ||
|
Contributor
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. Line 4 asks for a function called |
||
| const penceStringWithoutTrailingP = penceString.substring( | ||
| 0, | ||
| penceString.length - 1, | ||
| ); | ||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2, | ||
| ); | ||
| const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); | ||
| return `£${pounds}.${pence}`; | ||
| } | ||
| console.log(toPound("399p")); | ||
| console.log(toPound("2p")); | ||
| console.log(toPound("4500p")); | ||
| console.log(toPound("95p")); | ||
| console.log(toPound("3675p")); | ||
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.
Is
numon line 28 used now? Your function uses its ownnumparameter. If line 28 is not needed, remove it.