-
-
Notifications
You must be signed in to change notification settings - Fork 546
London | 26-ITP-Sep | Hanna Bohlin | Sprint 3 | Sprint 3 Coursework #1556
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
cd53ceb
0da90b9
73c7f27
cf6d301
627533f
c3dada4
46ff75c
bc36994
303a81a
5acd69c
fb5a7e3
76c016e
005834e
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,28 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| //Answer: I predict that there will be a Reference Error, because str get's re-declared with let | ||
| //inside the function even though it already is declared as it is the parameter. Perhaps it would | ||
| // be solved by removing the "let". | ||
|
|
||
| // 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; | ||
| } | ||
|
|
||
| console.log(capitalise("hellllo"); | ||
| */ | ||
|
|
||
| // =============> write your explanation here | ||
| // Answer: I got a Syntax Error, Identifier 'str' has already been declared. So 'str' needs to not | ||
| // be declared again. I will try to write the code without the let | ||
| // =============> write your new code here | ||
|
|
||
| function capitalise(str) { | ||
| str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
| console.log(capitalise("hellllo")); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,38 @@ | ||
|
|
||
| // 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 | ||
| // Answer: I predict that we will get a Reference Error: num is not defined. Or perhaps an error about the 3, as | ||
| // it is a number and therefore not a valid parameter name. I believe perhaps parameter names are like variable | ||
| // names and can't start with a number. | ||
|
|
||
| /* | ||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| */ | ||
|
|
||
| // =============> write the error message here | ||
| // Answer: SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // Answer: Yes we got an error message about the number 3. We wouldn't get a message about num not being defined | ||
| // since the function isn't called in the code, so the inside of the function can't produce an error. | ||
| // I will first try to update the 3 to n3, to see if it is a valid parameter name and see if the error goes away, | ||
| // just as an experiment. | ||
|
|
||
| // Answer: The error did go away. However it didn't solve our problem as we still want to receive 3 squared when | ||
| // calling the function. I will change n3 to the proper parameter name, num. So it can be referenced inside the | ||
| // function body. Then I will make a function call and pass in 3 there, as an argument to the parameter num. | ||
| // Then I will log the result to see if it worked. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
|
|
||
| console.log(square(3)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,29 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // Answer: I predict the console will log first 320, and then on a new line "The result of multiplying 10 and 32 is ${NaN}" | ||
| // This is because the function logs the result in it's body, so it will log it first as it is run, but | ||
| // because it's not explicitly returning anything, it will just return NaN into the string literal logged at the end. | ||
|
|
||
| /* | ||
| 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 | ||
| // Answer: In reality, I was almost correct, but the function returned undefined, not NaN, so it logged | ||
| // 320, and then The result of multiplying 10 and 32 is undefined. Oh and also of course the string | ||
| // interpolation brackets weren't included in the logged string like I had predicted. | ||
| // I will fix the problem by returning a * b in the function body, instead of logging it. | ||
|
|
||
| // 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,30 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // Answer: I predict that what happens is `The sum of 10 and 32 is undefined` gets logged to the console. | ||
| // That is because even though the function sum has a return statement, it doesn't return anything. There | ||
| // is a value that is meant to be returned below the return statement, but because the function already returned | ||
| // it will never reach that line in execution. That is why it is greyed out. | ||
|
|
||
| /* | ||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| */ | ||
|
|
||
| // =============> write your explanation here | ||
| // Answer: Running the code logged "The sum of 10 and 32 is undefined" like I thought. I will fix the problem | ||
| // by moving a + b; in the function body to be on the same line as return, so it gets returned instead of 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)}`); | ||
|
|
||
| // Answer: now "The sum of 10 and 32 is 42" is logged. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,5 +15,15 @@ | |
| // It should return a string of their Body Mass Index to 1 decimal place | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| return Number.parseFloat(weight / (height * height)).toFixed(1); | ||
| } | ||
|
|
||
| // Tests | ||
| console.log(calculateBMI(55, 1.63)); | ||
| console.log(calculateBMI(120, 1.73)); | ||
| console.log(calculateBMI(80, 1.69)); | ||
| console.log(calculateBMI(51, 1.55)); | ||
|
|
||
| // Please could I have a little feedback about if I have refactored the function return too much? | ||
|
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. One line is totally fine here! As a rule of thumb, you can keep it in one line as long as it is easy to understand. |
||
| // And if so, what is a good guide on how many operations to perform in one line..? I can also | ||
| // ask this in class if you prefer. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,12 @@ | |
| // 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(str) { | ||
| return str.toUpperCase().split(" ").join("_"); | ||
|
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. Nice! Very clean solution and great function name. |
||
| } | ||
|
|
||
| //tests | ||
| console.log(toUpperSnakeCase("i want to scream")); | ||
| console.log(toUpperSnakeCase("lord of the rings")); | ||
| console.log(toUpperSnakeCase("This is a loud file name")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,32 @@ | |
| // 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"); | ||
|
|
||
| return [pounds, pence]; | ||
| } | ||
|
|
||
| //tests | ||
| let [pounds, pence] = toPounds("399p"); | ||
| console.log(`£${pounds}.${pence}`); | ||
|
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. Can you think of a way to avoid repeating £${pounds}.${pence} after every call?" |
||
| [pounds, pence] = toPounds("3995p"); | ||
| console.log(`£${pounds}.${pence}`); | ||
| [pounds, pence] = toPounds("42895p"); | ||
| console.log(`£${pounds}.${pence}`); | ||
| [pounds, pence] = toPounds("2p"); | ||
| console.log(`£${pounds}.${pence}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,9 @@ | |
| function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| return `${hours - 12 < 10 ? "0" : ""}${hours - 12}:${time.slice(-2)} pm`; | ||
|
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. Can you think of any other edge cases that could surface here? |
||
| } else if (hours === 12) { | ||
| return `${time} pm`; | ||
| } | ||
| return `${time} am`; | ||
| } | ||
|
|
@@ -14,12 +16,40 @@ const currentOutput = formatAs12HourClock("08:00"); | |
| const targetOutput = "08:00 am"; | ||
| console.assert( | ||
| currentOutput === targetOutput, | ||
| `current output: ${currentOutput}, target output: ${targetOutput}` | ||
| `current output: ${currentOutput}, target output: ${targetOutput}`, | ||
| ); | ||
|
|
||
| const currentOutput2 = formatAs12HourClock("23:00"); | ||
| const targetOutput2 = "11:00 pm"; | ||
| console.assert( | ||
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}`, | ||
| ); | ||
|
|
||
| const currentOutput3 = formatAs12HourClock("12:00"); | ||
| const targetOutput3 = "12:00 pm"; | ||
| console.assert( | ||
| currentOutput3 === targetOutput3, | ||
| `current output: ${currentOutput3}, target output: ${targetOutput3}`, | ||
| ); | ||
|
|
||
| const currentOutput4 = formatAs12HourClock("15:45"); | ||
| const targetOutput4 = "03:45 pm"; | ||
| console.assert( | ||
| currentOutput4 === targetOutput4, | ||
| `current output: ${currentOutput4}, target output: ${targetOutput4}`, | ||
| ); | ||
|
|
||
| const currentOutput5 = formatAs12HourClock("08:25"); | ||
| const targetOutput5 = "08:25 am"; | ||
| console.assert( | ||
| currentOutput5 === targetOutput5, | ||
| `current output: ${currentOutput5}, target output: ${targetOutput5}`, | ||
| ); | ||
|
|
||
| const currentOutput6 = formatAs12HourClock("12:17"); | ||
| const targetOutput6 = "12:17 pm"; | ||
| console.assert( | ||
| currentOutput6 === targetOutput6, | ||
| `current output: ${currentOutput6}, target output: ${targetOutput6}`, | ||
| ); | ||
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.
Could be worth having another look at the Number.parseFloat docs. What type of value does it expect, and what are we passing it here?