-
-
Notifications
You must be signed in to change notification settings - Fork 546
London | 26-ITP-Sep | Mahir Shah | Sprint 3 | Sprint 3 Coursework #1625
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 15 commits
38c5f48
b862d6d
d311b62
d9257c2
8dcfe07
9ff0a0b
ebffeae
818cc32
b0260ef
f54452f
38825cb
ab74a01
d27c7d8
508e466
92cdff7
ad83dc0
d643752
9b453d8
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,22 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // the function has a parameter str, but then tries to make a new | ||
| // variable also called str. so maybe cause error because str already exists | ||
|
|
||
| // 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)}`; | ||
| return str; | ||
| } | ||
| //function capitalise(str) { | ||
| // let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // return str; | ||
| //} | ||
|
|
||
| // =============> write your explanation here | ||
| // SyntaxError: Identifier 'str' has already been declared | ||
| // Prediction was correct, str already exists | ||
| // =============> write your new code here | ||
|
|
||
| function capitalise(str) { | ||
| const capitaliseStr = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return capitaliseStr; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,26 @@ | ||
|
|
||
| // 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 | ||
| // In the function, 3 hasn't been assigned to num, so num is NaN and should return NaN. | ||
| //Also when defining a function, the parameter should have the variable name, not a value | ||
| //to pass in, unless you want to default value such as num = 3, which assigns 3 to num if | ||
| // no value is passed in | ||
|
|
||
| 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 | ||
|
|
||
| //syntaxError means there is a mistake in the way the code is written, so javascript can't | ||
| // interpret the code. in this case there is a number here when there shouldn't be. | ||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,25 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| //The multiply function doesn't return anything, only prints to console so the function call will have value undefined. | ||
| //So when line 12 is executed, first it will print "The result of multiplying 10 and 32 is undefined" | ||
| //Then on a new line it prints 320 | ||
|
|
||
| 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 | ||
|
|
||
| //It first printed 320, then printed "The result of multiplying 10 and 32 is undefined" on the next line | ||
| //I think this is because it goes through the line 12, executes the multiply function call first, then finishes the | ||
| //console.log function call. | ||
| // 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 | ||
| //when the return line is executed, the function exits. a + b is a separate line because it | ||
| //is after the semicolon, so a + b is never reached and not returned. So line 11 will print | ||
| //The The sum of 10 and 32 is undefined | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
| //function sum(a, b) { | ||
| // return; | ||
| // a + b; | ||
| //} | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| //It did as predicted, for the reason I expected | ||
| // 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,41 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| //The last digit of 42 is 3 | ||
| //The last digit of 105 is 3 | ||
| //The last digit of 806 is 3 | ||
| // =============> Write your prediction here | ||
| //num is defined outside of the function, so has scope in the function getLastDigit. | ||
| //the function doesn't take any arguments and uses num, so will always return 3, no matter what | ||
| //is passed to the function because it will use num, which is set to 103 | ||
|
|
||
| const num = 103; | ||
| //const num = 103; | ||
|
|
||
| function getLastDigit() { | ||
| return num.toString().slice(-1); | ||
| } | ||
| //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 42 is ${getLastDigit(42)}`); | ||
| //console.log(`The last digit of 105 is ${getLastDigit(105)}`); | ||
| //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 | ||
| // As I predicted, for the reason above | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| 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 |
|---|---|---|
|
|
@@ -4,3 +4,28 @@ | |
| // 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(penceString) { | ||
| 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"); | ||
|
|
||
| console.log(`£${pounds}.${pence}`); | ||
|
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. Your function prints the price, but it does not give it back. What does You explained this yourself in Say another part of a program needs the price. For example, it wants to add it to a sentence. Can it get the price from
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. Not currently. I have changed the console.log to return
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. Your function returns the price now, which is right. And you call it a few times with different values. But how do you know it works each time? Line 6 says to call it "to check it works for different inputs". There are a few ways to do this:
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. None of the five |
||
| } | ||
|
|
||
| toPounds("10p"); | ||
| toPounds("100p"); | ||
| toPounds("232134p"); | ||
| toPounds("238p"); | ||
| toPounds("5234p"); | ||
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.
why was the replaceAll used ? does this print exactly what is expected or does it add some additional strings which were not part of input string ?
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.
replaceAll goes through a string and removes all cases of the first input, and replaces it with the second input. It prints what is expected, and the only thing added are _ in places of " ". If there's any unexpected whitespace " " after a string, they would replaced with _
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.
Clear answer, thanks.