-
-
Notifications
You must be signed in to change notification settings - Fork 546
Manchester | 26-ITP-Sept | Fatima Rouchi | Sprint 3 | Coursework/sprint 3 #1634
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
c85f975
e071e69
6570ced
001939b
9650964
af75a19
12f14c1
cdac639
4089ba3
acc0e01
f80713c
86827f4
9f55f62
c566c35
ebea776
d6fd51e
dc6b184
60c1518
8ee83a9
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,25 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============>// I predict JavaScript will give a SyntaxError because str is declared twice in the function. | ||
|
|
||
| // 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; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| capitalise("hello");*/ | ||
| /* =============> The error happens because str is already the function's parameter. | ||
| let str tries to use the same name again, so JavaScript gives a SyntaxError. | ||
| The function stops before it can run. */ | ||
| // =============> write your new code here | ||
| function capitalise(str) { | ||
| if (str === "") { | ||
| return ""; | ||
| } | ||
|
|
||
| const result = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return result; | ||
| } | ||
|
|
||
| console.log(`Result: "${capitalise("")}"`); |
|
fatimarouchi marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,29 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // =============>// I predict a SyntaxError because decimalNumber is declared as a parameter and then declared again with const. | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| /*function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
| console.log(decimalNumber);*/ | ||
|
|
||
| // =============> write your explanation here | ||
| /* =============> decimalNumber is already the function parameter. | ||
| const decimalNumber tries to declare the same name again inside the function, | ||
| so JavaScript gives a SyntaxError. */ | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function convertToPercentage(decimalNumber) { | ||
| const percentage = `${(decimalNumber * 100).toFixed(1)}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(convertToPercentage(0.5)); | ||
|
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. Well done fixing the original error by removing the redeclared const decimalNumber, and your code runs correctly; just note that toFixed(1) changes the output from 50% to 50.0%, so either remove it to match the expected result or add a short comment explaining why you chose to include it. |
||
|
fatimarouchi marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,21 @@ | ||
|
|
||
| // 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 a SyntaxError because 3 is a number, but a function parameter needs to be a name. | ||
|
|
||
| function square(3) { | ||
| /*function square(3) { | ||
| return num * num; | ||
| } | ||
| */ | ||
| // =============> SyntaxError: Unexpected number | ||
|
|
||
| // =============> write the error message here | ||
|
|
||
| // =============> explain this error message here | ||
| // =============> The error happens because 3 is not a name. A parameter needs a name, like num. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
| console.log(square(3)); |
|
fatimarouchi marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,18 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // =============> I couldn't predict until I run the code | ||
|
|
||
| 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)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);*/ | ||
|
|
||
| /* =============> The Terminal gives an answer of 320 but also says "The result of multiplying 10 and 32 is undefined" | ||
| which makes me think that the function has no return so it gives back undefined.*/ | ||
| // 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)}`); |
|
fatimarouchi marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,19 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> I predict the code won't work because there's a ";" after 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 sum is undefined because the semicolon after return ends it too early, | ||
| the function returns before adding a and 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)}`); |
|
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. Spot on! Adding
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. If I call getLastDigit(-42), it returns "2", because the function converts the number to a string and gets its last character. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,43 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // =============> I predict the result will be wrong because the num is set set on 103. | ||
|
|
||
| const num = 103; | ||
| /*const num = 103; | ||
|
|
||
| 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 806 is ${getLastDigit(806)}`);*/ | ||
|
|
||
| // Now run the code and compare the output to your prediction | ||
| // =============> write the output here | ||
| /* =============> the output: | ||
| 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 | ||
| /* =============> The problem is that the function ignores the numbers passed to it, | ||
| it always uses num, which is 103, so it returns 3 every time.*/ | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| /*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)}`);*/ | ||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
| // In this case getLastDigit is not working because it's parameter is empty and not declared. | ||
| // Here's the right code: | ||
| 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)}`); |
|
fatimarouchi marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,5 +15,6 @@ | |
| // 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 (weight / (height * height)).toFixed(1); | ||
| } | ||
| console.log(calculateBMI(52, 1.63)); | ||
|
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. The exercise asks for the function to be implemented and checked with several inputs, ensuring the function works across multiple inputs. You can add a few more test checks to demonstrate this. |
||
|
fatimarouchi marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,8 @@ | |
| // 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(words) { | ||
| return words.toUpperCase().trim().split(/\s+/).join("_"); | ||
| } | ||
|
|
||
| console.log(toUpperSnakeCase("hello there")); | ||
|
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. Good work, your function correctly converts the string to UPPER_SNAKE_CASE, and using trim() with split(/\s+/) is a thoughtful way to handle extra spaces; you can add a few more test examples, such as a single word like "hello", and a string with extra spaces to show it works for a range of inputs |
||
|
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. Spot on! This is a clever way to handle string slicing and padding to format the currency correctly.
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. Without the p, it removes a digit by mistake. I can convert the input to a string and remove the p only when it’s there. |
|
fatimarouchi marked this conversation as resolved.
|
|
fatimarouchi marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,23 +3,49 @@ | |
| // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. | ||
|
|
||
| function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| const [hoursString, minutesString = "00"] = time.split(":"); | ||
| const hours = Number(hoursString); | ||
| const minutes = minutesString.padStart(2, "0"); | ||
|
|
||
| let suffix = "am"; | ||
| if (hours >= 12) { | ||
| suffix = "pm"; | ||
| } | ||
| return `${time} am`; | ||
| } | ||
|
|
||
| const currentOutput = formatAs12HourClock("08:00"); | ||
| const targetOutput = "08:00 am"; | ||
| console.assert( | ||
| currentOutput === targetOutput, | ||
| `current output: ${currentOutput}, target output: ${targetOutput}` | ||
| ); | ||
| let formattedHours = hours % 12; | ||
| if (formattedHours === 0) { | ||
| formattedHours = 12; | ||
| } | ||
|
|
||
| return `${formattedHours}:${minutes} ${suffix}`; | ||
| } | ||
|
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 work using hours % 12 with the reset to 12, which handles both midnight and noon correctly; one thing to fix is that formattedHours is a number, so 08:00 returns 8:00 am instead of 08:00 am. You look into fixing that. |
||
|
|
||
| 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 noonOutput = formatAs12HourClock("12:00"); | ||
| const noonTarget = "12:00 pm"; | ||
| console.assert( | ||
| noonOutput === noonTarget, | ||
| `current output: ${noonOutput}, target output: ${noonTarget}`, | ||
| ); | ||
|
|
||
| const midnightOutput = formatAs12HourClock("00:00"); | ||
| const midnightTarget = "12:00 am"; | ||
| console.assert( | ||
| midnightOutput === midnightTarget, | ||
| `current output: ${midnightOutput}, target output: ${midnightTarget}`, | ||
| ); | ||
|
|
||
| const singleDigitMinuteOutput = formatAs12HourClock("9:5"); | ||
| const singleDigitMinuteTarget = "9:05 am"; | ||
| console.assert( | ||
| singleDigitMinuteOutput === singleDigitMinuteTarget, | ||
| `current output: ${singleDigitMinuteOutput}, target output: ${singleDigitMinuteTarget}`, | ||
| ); | ||
|
|
||
| console.log("All formatAs12HourClock tests passed."); | ||
|
Comment on lines
+29
to
+51
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. Great job testing noon and midnight, which are the edge cases most likely to break this function. Two things to think about: what happens to your final success message if one of the assertions fails, and is "9:5" a valid 24-hour time whose expected output matches the format of your other tests? |
||
Uh oh!
There was an error while loading. Please reload this page.