-
-
Notifications
You must be signed in to change notification settings - Fork 337
Sheffield | ITP-Jan-26 | Hayriye Saricicek | Sprint 2 | Module Structuing and Testing Data #1186
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 13 commits
32407f9
bfbcbe1
2b742f7
4c0e0eb
3f9d9b6
966c2c2
4dc8fb6
3200170
c3f25c2
f2897ab
849f01e
8be3b9d
d6e7596
af41f53
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,4 +1,4 @@ | ||
| node_modules | ||
| .DS_Store | ||
| .vscode | ||
| **/.DS_Store | ||
| .vscode/ | ||
| **/.DS_Store |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,31 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // =============> when the console.log outside the function is performed $(multiply(10, 32)) | ||
| // it will print the text The result of multiplying by 10 and 32 is | ||
| // but then will state undefined for the value | ||
|
|
||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> there is no return | ||
| // console.log(a*B) printed the result of multiplying a and b but because there is no return | ||
| // the answer was not returned to the function so the console.log outside the function did not | ||
| // receive the value and printed undefined instead | ||
|
|
||
|
|
||
| // 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)}`); | ||
|
|
||
| // I removed the first console.log because it didn't seem necessary to print out 320 | ||
| // if you need to print 320 as well as the final console-log statement then console.log(a * b) | ||
| // can be added back in but it would need to be before the return statement because if it is | ||
| // after the return statement it will never be reached and the value will not be printed out | ||
|
|
| 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 getPenceString(penceString){ | ||
|
|
||
| const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1); | ||
| //removes p | ||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| //puts zeros in front of number if less than 3 digits | ||
| const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2); | ||
| //removes last 2 digits to get pounds | ||
| const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); | ||
| //gets last 2 digits to get pence, if less than 2 digits adds zeros to end of string but shouldnt be less than 3 | ||
| //due to padStart above | ||
| return `£${pounds}.${pence}`; | ||
| // returns string with pounds and pence in correct format | ||
| } | ||
|
|
||
| console.log(getPenceString("399p")); | ||
| // tested with 123p 1200p 24589p 89p 9p and 0p | ||
|
Comment on lines
+8
to
+24
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. Indentation is off. Have you installed prettier VSCode extension and enabled formatting on save/paste on VSCode
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. I did install prettier and enabled formatting on save/paste. I may have done it after I had done this or it may not be working.
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. Understood. I cannot keep track of whom I had reminded of this. So I just nagged the person with this reminder whenever I spotted improperly formatted code. |
||
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.
What type of value do you expect your function to return? A number or a string?
Does your function return the type of value you expect?
Different types of values may appear identical in the console output, but they are represented and treated differently in the program. For example,
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.
Thank you for pointing this out. I have researched this now and understand that the answer would have been a number if I hadn't requested it to be to 1 decimal place. The tofixed(1) converts the number to a string. In this case it doesn't effect the outcome of the answer printed but I now understand that if I did need to do calculations on the answer then it would need to be converted back to a number in order to do this so it is good practice to convert back to a number.
I have amended the program to convert the string answer back to a number and print a number.
I know as good practice there are usually inbuilt checks in case people input incorrect data. If you want me to do this I can.
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.
As long as you are aware of the type of the data you are returning, its is enough.
The spec was not clear on this anyway, and returning the result as a number as its shorting coming too.