-
-
Notifications
You must be signed in to change notification settings - Fork 546
London | 26-Sep-ITP | Frumentius Tesfay | Sprint 3 | Coursework #1589
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
d83f34c
dc3752d
53921fe
62255c0
d423953
47e5c24
6189cca
d8b60b7
20b12f0
706a763
7d4385b
107eaf2
9a02929
cf22a51
d9e2cde
365fc3a
0c6b704
da302ee
bc5bd0e
9723adb
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 | ||
| // I believe this will cause an error because str is declared twice | ||
|
|
||
| // call the function capitalise with a string input | ||
| // interpret the error message and figure out why an error is occurring | ||
|
|
||
| function capitalise(str) { | ||
| /* function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
| */ | ||
|
|
||
| // The error happens because str is declared twice, once as a parameter & again with let | ||
| // If I give the new variable a different name, I hope it should work :) | ||
|
|
||
| function capitalise(str) { | ||
| let capitalisedString = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return capitalisedString; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| console.log(capitalise("frumentius")); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,32 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // Because decimalNumber is declared twice, once as a parameter and again with const | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| /* | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
| console.log(convertToPercentage(decimalNumber)); */ | ||
|
|
||
| // =============> write your explanation here | ||
| // decimalNumber is already a parameter, so declaring it again with const causes an error | ||
| // decimalNumber has already been declared as a parameter in the same function scope | ||
| // console.log(decimalNumber) is wrong because decimalNumber is not defined outside the function, | ||
| // so we need to call the function with a value | ||
|
|
||
| // 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.7)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,30 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
|
|
||
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // =============> write your prediction of the error here | ||
| // I think using 3 will cause an error as parameter | ||
| // A parameter must be a valid variable name, not a number | ||
|
|
||
| /* function square(3){ | ||
| return num * num; | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| */ | ||
|
|
||
| // =============> write the error message here | ||
| // SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // The parser sees the number 3 where it expects a valid parameter name, | ||
| // so it cannot understand the structure of the function | ||
| // It means we cannot use a number as a parameter name | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
| console.log(square(7)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,24 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // I believe the function will calculate 10 x 32 | ||
|
|
||
| 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 calling multiply(10, 32), the result is undefined because there is no return statement | ||
| // It can still print 320 because console.log() prints the result inside the function | ||
|
|
||
| // 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,22 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // I guess the function will return error or undefined because the return; | ||
|
|
||
| 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 | ||
| // The problem is the semicolon immediately after return that prevent to reach out to a + b | ||
|
|
||
| // 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 toUpperSnakeCase(input) { | ||
| return input.replaceAll(" ", "_").toUpperCase(); | ||
|
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. apart from uppercase, is this function perhaps adding something that does not exist in the input ?
Author
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. Yeah, I noticed that it also replaces the spaces with underscores besides converting to uppercase. |
||
| } | ||
|
|
||
| console.log(toUpperSnakeCase("alpha and omega")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,21 @@ | |
| // 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 toPounds(amount) { | ||
| const amountWithoutTrailingP = amount.substring(0, amount.length - 1); | ||
|
|
||
| const paddedPenceNumberString = amountWithoutTrailingP.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(toPounds("1250p")); | ||
|
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 6 asks you to call the function a number of times, with different inputs. Line 24 calls it once. Try a few more, for example a short one and a long one. |
||
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 there a possibility that the logs are being duplicated ?