-
-
Couldn't load subscription status.
- Fork 239
West Midlands | 25-Sept-ITP | Georgina Rogers | Sprint 2 | Structing and Testing Data #782
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
d17d9f0
8d3eb4a
1564427
516822d
95b56ec
bc05c26
23888b9
b6e2da1
97e6d94
f6b823d
c232401
7f82d29
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,20 +1,29 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // constant decimalNumber is being declared twice within the same scope, which will cause a syntax error. | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| //function convertToPercentage(decimalNumber) { | ||
| //const decimalNumber = 0.5; | ||
| //const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
| //return percentage; | ||
| //} | ||
|
|
||
| console.log(decimalNumber); | ||
| //console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
| // SyntaxError: Identifier 'decimalNumber' has already been declared | ||
| // decimalNumber is declared as a parameter of the function and then again inside the function using const. | ||
| // In JavaScript, you cannot declare the same variable name in the same scope using let or const. | ||
| // Function input is ignored if a variable with the same name is declared inside the function body. | ||
| // const decimalNumber = 0.5 will always output 50% regardless of the input value. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function convertToPercentage(decimalNumber) { | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| return percentage; | ||
| } | ||
| console.log(convertToPercentage(0.75)); // Example call to test the function |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,23 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // console.log is being used inside the multiply function, which does not return any value. | ||
| // This will result in undefined being printed in the template literal. | ||
|
|
||
| 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 | ||
| // Output in the terminal was: "320 | ||
| // The result of multiplying 10 and 32 is undefined" | ||
| // Console.log within the function prints the product of the calculation, but the function does not return the value to the global scope. | ||
| // Therefore within the template literal, the meaning of the multiply function call is 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)}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,20 @@ | ||
| // Below are the steps for how BMI is calculated | ||
|
|
||
| // The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared. | ||
|
|
||
| // For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by: | ||
|
|
||
| // squaring your height: 1.73 x 1.73 = 2.99 | ||
| // dividing 70 by 2.99 = 23.41 | ||
| // Your result will be displayed to 1 decimal place, for example 23.4. | ||
|
|
||
| // You will need to implement a function that calculates the BMI of someone based off their weight and height | ||
|
|
||
| // Given someone's weight in kg and height in metres | ||
| // Then when we call this function with the weight and height | ||
| // It should return their Body Mass Index to 1 decimal place | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| var bmi = (weight / (height * height)); | ||
| return bmi.toFixed(1); | ||
|
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. What type of value do you expect the function to return? A number or a string? |
||
| } | ||
| console.log(calculateBMI(70, 1.73)); | ||
| console.log(calculateBMI(95, 1.82)); | ||
| console.log(calculateBMI(52, 1.6)); | ||
| console.log(calculateBMI(110, 1.75)); | ||
| console.log(calculateBMI(65, 1.9)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,34 @@ | ||
| // In Sprint-1, there is a program written in interpret/to-pounds.js | ||
|
|
||
| // You will need to take this code and turn it into a reusable block of code. | ||
| // 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) { | ||
| penceString = String(penceString);// ensures input is treated as a string even if number is inputted | ||
| // const penceStringWithoutTrailingP = penceString.substring( | ||
| let penceStringWithoutTrailingP = penceString; | ||
| if (penceString.endsWith("p")) { | ||
| penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); | ||
| } // This prevents the last characters from being sliced off if there is no p. | ||
|
|
||
| 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}`; | ||
| } | ||
|
Comment on lines
+6
to
+25
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 improve the indentation of the code in this function? Note: In VSCode, we can install the prettier extension and then use the "Format document" function" to automatically indent code. |
||
| console.log(toPounds("481p")); | ||
| console.log(toPounds("50p")); | ||
| console.log(toPounds("5p")); | ||
| console.log(toPounds("0p")); | ||
| console.log(toPounds("12345p")); | ||
| console.log(toPounds("9")); // edge case: missing 'p' at the end, returns 0 rather than error because last character is sliced off | ||
| // Can be fixed by changing line 8 | ||
| console.log(toPounds("abc")); // Works but returns nonsense value | ||
| console.log(toPounds(123)); // Works with number input because of line 7 converting to string | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,17 @@ | |
| // 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) { | ||
| if (!/^\d{2}:\d{2}$/.test(time)) { //If input does not match HH:MM format, returns string "Invalid time format" | ||
| return "Invalid time format"; | ||
| } | ||
|
|
||
| const hours = Number(time.slice(0, 2)); | ||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| } | ||
| const minutes = time.slice(3); | ||
|
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. Could also consider use |
||
| if (hours === 0) return `12:${minutes} am`; | ||
| if (hours === 12) return `12:${minutes} pm`; | ||
| if (hours === 24) return `12:${minutes} am`; | ||
| if (hours > 12) return `${hours - 12}:00 pm`; | ||
|
|
||
| return `${time} am`; | ||
|
Comment on lines
+15
to
17
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 return values of these two function calls are not formatted consistently. Can you improve the consistency? |
||
| } | ||
|
|
||
|
|
@@ -23,3 +30,12 @@ console.assert( | |
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| ); | ||
|
|
||
| console.log(formatAs12HourClock("12:00")); // | ||
| console.log(formatAs12HourClock("00:00")); // | ||
| console.log(formatAs12HourClock("15:30")); // | ||
| console.log(formatAs12HourClock("11:45")); // | ||
| console.log(formatAs12HourClock("24:00")); // | ||
| console.log(formatAs12HourClock("ab:cd")); // | ||
| console.log(formatAs12HourClock("9:00")); // | ||
| console.log(formatAs12HourClock("09:0")); | ||
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.
Avoid using
varto declare variables. Useletinstead.