-
-
Notifications
You must be signed in to change notification settings - Fork 241
London | 25-ITP-May | Houssam Lahlah | Sprint 2 | Coursework/sprint 2 #700
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 27 commits
2dd0a78
1bc5c74
b16d048
625c114
f5522e7
1cf3f70
b76c5a3
0748f7c
d957dd1
fcba7aa
c278bbb
335cc37
2f9d4da
bdc5ac9
16d6577
8796b5b
1b5f2cf
6573f60
01a1ced
eeff899
f39781a
bce599b
19f296f
f63026f
a9c2a21
5eb1ab8
c229426
81ae2ac
9b67749
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,9 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // I predict this program will throw a `SyntaxError` because | ||
| // the parameter `decimalNumber` is being redeclared inside the function using `const`. | ||
| // Also, the variable `decimalNumber` is being accessed outside the function where it's not defined. | ||
|
|
||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
|
|
@@ -15,6 +20,20 @@ function convertToPercentage(decimalNumber) { | |
| console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
| // The function already has a parameter named `decimalNumber`, | ||
| // but inside the function, it's trying to declare another | ||
| // constant with the same name: `const decimalNumber = 0.5;`. | ||
| // JavaScript doesn’t allow redeclaring a variable that already exists in the same scope. | ||
| // Also, `console.log(decimalNumber)` is outside the function and `decimalNumber` is not | ||
| // defined in the global scope, so that will cause a `ReferenceError`. | ||
|
|
||
|
|
||
| // 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.5)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,27 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
|
|
||
| // =============> write your prediction here | ||
| // I predict that this will print: "The sum of 10 and 32 is undefined" | ||
| // because the function has a return statement with no value. | ||
|
|
||
| // Explanation: | ||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| // =============> write your explanation here | ||
| // The `return;` statement ends the function early, so `a + b` is never reached. | ||
| // In JavaScript, any code after a bare `return;` is ignored (unreachable code). | ||
| // That's why the function returns `undefined`, not the actual sum. | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // 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)}`); | ||
| // Now the function returns the correct result: 42 | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,13 @@ | ||
| // 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. | ||
| function calculateBMI(weight, height) { | ||
| // Square the height (height in meters) | ||
| const heightSquared = height * height; | ||
|
|
||
| // You will need to implement a function that calculates the BMI of someone based off their weight and height | ||
| // Divide weight by height squared to get BMI | ||
| const bmi = weight / heightSquared; | ||
|
|
||
| // 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 | ||
| // Return the BMI rounded to 1 decimal place | ||
| return bmi.toFixed(1); // toFixed returns a string, which is fine unless you need it as a number | ||
| } | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| // Example usage: | ||
| console.log(calculateBMI(70, 1.73)); // Output: "23.4" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,15 @@ | ||
| // A set of words can be grouped together in different cases. | ||
| // This function converts any sentence into UPPER_SNAKE_CASE | ||
| function toUpperSnakeCase(input) { | ||
| // Step 1: Convert the string to uppercase | ||
| const upperCase = input.toUpperCase(); | ||
|
|
||
| // For example, "hello there" in snake case would be written "hello_there" | ||
| // UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces. | ||
| // Step 2: Replace all spaces with underscores | ||
| const snakeCase = upperCase.replace(/ /g, "_"); | ||
|
|
||
| // Implement a function that: | ||
| // Step 3: Return the result | ||
| return snakeCase; | ||
| } | ||
|
|
||
| // Given a string input like "hello there" | ||
| // When we call this function with the input string | ||
| // it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE" | ||
|
|
||
| // Another example: "lord of the rings" should be "LORD_OF_THE_RINGS" | ||
|
|
||
| // 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 | ||
| // Example usage: | ||
| console.log(toUpperSnakeCase("hello there")); // Output: "HELLO_THERE" | ||
| console.log(toUpperSnakeCase("lord of the rings")); // Output: "LORD_OF_THE_RINGS" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,22 @@ | ||
| // In Sprint-1, there is a program written in interpret/to-pounds.js | ||
| function toPounds(penceString) { | ||
| const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); | ||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
|
|
||
| // 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. | ||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| ); | ||
|
|
||
| // You should call this function a number of times to check it works for different inputs | ||
| const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); | ||
|
|
||
| return `£${pounds}.${pence}`; | ||
| } | ||
|
|
||
| // Test calls | ||
| console.log(toPounds("399p")); // Output: £3.99 | ||
| console.log(toPounds("5p")); // Output: £0.05 | ||
| console.log(toPounds("45p")); // Output: £0.45 | ||
| console.log(toPounds("999p")); // Output: £9.99 | ||
| console.log(toPounds("2p")); // Output: £0.02 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,46 @@ | ||
| // This is the latest solution to the problem from the prep. | ||
| // Make sure to do the prep before you do the coursework | ||
| // 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 minutes = time.slice(3, 5); | ||
|
|
||
| let rawHour; | ||
| let period; | ||
|
|
||
| if (hours === 0) { | ||
| rawHour = 12; | ||
| period = "am"; | ||
| } else if (hours === 12) { | ||
| rawHour = 12; | ||
| period = "pm"; | ||
| } else if (hours > 12) { | ||
| rawHour = hours - 12; | ||
| period = "pm"; | ||
| } else { | ||
| rawHour = hours; | ||
| period = "am"; | ||
| } | ||
| return `${time} am`; | ||
|
|
||
| const formattedHour = String(rawHour).padStart(2, "0"); | ||
|
|
||
| return `${formattedHour}:${minutes} ${period}`; | ||
| } | ||
|
|
||
| const currentOutput = formatAs12HourClock("08:00"); | ||
| const targetOutput = "08:00 am"; | ||
| console.assert( | ||
| currentOutput === 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}` | ||
| ); | ||
|
|
||
| // Tests: | ||
| const tests = [ | ||
| { input: "00:00", expected: "12:00 am" }, | ||
| { input: "08:00", expected: "08:00 am" }, | ||
| { input: "12:00", expected: "12:00 pm" }, | ||
| { input: "15:30", expected: "03:30 pm" }, | ||
| { input: "23:59", expected: "11:59 pm" }, | ||
| { input: "11:15", expected: "11:15 am" }, | ||
| ]; | ||
|
|
||
| tests.forEach(({ input, expected }) => { | ||
| const output = formatAs12HourClock(input); | ||
| console.assert( | ||
| output === expected, | ||
| `FAIL: input=${input}, output=${output}, expected=${expected}` | ||
| ); | ||
| }); | ||
|
|
||
|
|
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.
Renaming the variable allows you to fix the bug, great!
The pattern here is you are defining a variable and then immediately returning it. Can you think how to make this code even simpler?
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.
Hi LonMcGregor,
Thank you for your feedback.
I see what you mean — I don’t actually need the extra variable here.
I can simplify it by returning the string directly, Like this :
function capitalise(str) {
return
${str[0].toUpperCase()}${str.slice(1)};}
because this way the function is shorter and still clear.