-
-
Notifications
You must be signed in to change notification settings - Fork 546
London | 26-ITP-Sep | Rhoda Ajiroba | Sprint 3 | Course work #1629
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
Open
codebyra
wants to merge
2
commits into
CodeYourFuture:main
Choose a base branch
from
codebyra:coursework/sprint-3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,13 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // ===========> I predict there will be an error when the function is called. | ||
|
|
||
| // call the function capitalise with a string input | ||
| // interpret the error message and figure out why an error is occurring | ||
|
|
||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| capitalise("hello"); | ||
| // ===========> The error happens because str has already been declared as a function parameter. | ||
| // ===========> Removed let because str is already declared as a function parameter. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,19 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // ===========> I predict there will be an error because decimalNumber is declared more than once. | ||
|
|
||
| // 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(0.5)); | ||
|
|
||
| // =============> write your explanation here | ||
| // ===========> The error happens because decimalNumber is already declared as a function parameter. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // ===========> Removed the duplicate declaration and called the function with 0.5. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,17 @@ | ||
|
|
||
| // 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 predict there will be a syntax error because 3 cannot be used as a function parameter. | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| function square(num) { | ||
| return num * num; | ||
| } | ||
|
|
||
| // =============> write the error message here | ||
| // ===========> SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // ===========> The error happens because a number cannot be used as a function parameter name. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| // ===========> Changed 3 to num so the function has a valid parameter name. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,14 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // ===========> I predict the multiplication will print 320, but the final result will show undefined. | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| return a * b; | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // ===========> The function prints the result instead of returning it, so the function returns undefined. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // ===========> Changed console.log to return so the function returns the multiplication result. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,12 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // ===========> I predict the result will be undefined because the function returns before adding the numbers. | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| return a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // ===========> The function returns before a + b is calculated, so the result is undefined. | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // ===========> Changed the return statement so it returns a + b. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Your function works for numbers, but it is not the program this exercise asks for.
The exercise wants you to take the program in
Sprint-2/3-mandatory-interpret/3-to-pounds.jsand turn it into a function. That program:"399p""£3.99"Your function takes a number, like
250, and gives back a number,2.5. A price should show as£2.50.Please open that Sprint 2 file. Put its code inside your
toPoundsfunction, and use the parameter in place of"399p". Then call the function with a few different inputs, for example"399p","5p"and"2500p".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.
Your function works now. A string goes in, and a string comes out. Good.
But this exercise had a different aim. The Sprint 2 code in
Sprint-2/3-mandatory-interpret/3-to-pounds.jsalready changes pence to pounds, and it works. But it only works for one price, because"399p"is fixed on line 1. To change"5p"or"2500p", you would need to copy all the lines again.A function solves this. When we need the same lines many times, we put them in a function. The part that changes each time becomes the parameter. Here, the part that changes is the pence amount. The steps stay the same.
So you did not need to write new steps. Your three lines do the same jobs as the Sprint 2 lines:
pNumber(pence.replace("p", ""))penceString.substring(...)penceNumber / 100padStart,substring,padEnd`£${pounds.toFixed(2)}``£${pounds}.${pence}`You only needed to change two Sprint 2 lines:
const penceString = "399p";, goes away. The parameter takes its place. If you call the parameterpenceString, lines 3 to 16 work with no changes at all.console.log. In a function, change it toreturn.The function looks like this:
Now the same lines work for every price.
toPounds("399p"),toPounds("5p")andtoPounds("2500p")all use them. Only the value you pass in changes.The rule: if you need the same lines many times, put them in a function. Make the part that changes a parameter.