-
-
Notifications
You must be signed in to change notification settings - Fork 382
Glasgow| May-26-ITP | Yat Long Chan | Sprint 2 | Coursework #1361
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
1df05b5
4774028
7a9559b
fedf840
93486c9
608b21e
2710dcd
430f087
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 | ||
|
|
||
| // 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)}`; | ||
| let str = '${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| It said syntaxError because identifier 'str' has already been declared | ||
| because we should use backtick`` to wrap the template literal.The first one of the bracket is a | ||
| single quote, not a backtick. | ||
|
|
||
| Also, since str has already been declared as a parameter, we should not use it to define as variable. | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| function capitalise(str) { | ||
|
|
||
| let result = `${str[0].toUpperCase() + str.slice(1)}`; | ||
| return result; | ||
| } | ||
|
|
||
| console.log(capitalise("str")); | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,22 @@ | ||
|
|
||
| // 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 | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
|
|
||
| // =============> write the error message here | ||
|
|
||
| // =============> explain this error message here | ||
| It shows the syntax error which is an unexpected number; | ||
| because we should not put 3 insides the variable, because it will lead to variable: num not defined | ||
| insides the function, so the 3 doesn't work insides the function. | ||
|
|
||
| We need to put num instead of 3 insides the(); | ||
| That way when we call the function by using console.log.The 3 will go into the num variable. | ||
|
|
||
|
|
||
| // Finally, correct the code to fix the problem | ||
| function square(num) { | ||
| return num * num; | ||
| } | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,20 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| The problem is unexpected identifier "problem" | ||
| The problem is the result doesn't print the mulitplication of the number, | ||
| because we should not call the function inside the function; | ||
|
|
||
|
|
||
| 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 | ||
| Instead, we should define the expression a * b | ||
| and return the expression. | ||
|
|
||
| / function multiply(a, b) { | ||
| return (a * b); | ||
| } | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| Unexpected identifier "problem" | ||
| variable hasn't been defined so the variable cannot do function. | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
|
|
@@ -8,6 +8,14 @@ function sum(a, b) { | |
|
|
||
| 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 | ||
|
|
||
| because return should be followed by a + b; | ||
| the final result doesn't return the value. | ||
|
|
||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
|
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. When you run this code, does it work correctly? |
||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(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. | ||
| function calculateBMI(weight, height) { | ||
|
|
||
| let newheight = height / 100; | ||
| let num = weight / (newheight ** 2); | ||
| let idea = num.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. Is "idea" a good variable name here? |
||
|
|
||
| return idea; | ||
| } | ||
|
|
||
|
|
||
| console.log(calculateBMI(58, 178)); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| // 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 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,16 @@ | |
| // UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces. | ||
|
|
||
| // Implement a function that: | ||
| function convertToUpperCase(text) { | ||
| const result = text.toUpperCase(); | ||
|
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. it is upper cased now, how can you make it "snake case"? |
||
| return result; | ||
| } | ||
|
|
||
| console.log(convertToUpperCase("hello")); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // Given a string input like "hello there" | ||
| // When we call this function with the input string | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,36 @@ | |
|
|
||
| function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| const minutes = time.slice(3,6); | ||
|
|
||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
|
|
||
| const convertedHour = hours - 12 | ||
| const paddedHour = convertedHour.toString().padStart(2, '0') | ||
| return `${paddedHour}:${minutes} pm`; | ||
| } | ||
| return `${time} am`; | ||
| } | ||
|
|
||
| else if(hours=== 0o0){ | ||
|
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 does 0o0 mean here? |
||
| const newHour= hours+ 12; | ||
| return `${newHour}:${minutes} am`;} | ||
|
|
||
| else if(hours=== 12){ | ||
| return `12:00 pm`; | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| else if (hours < 12){ | ||
| const paddedHour2 = hours.toString().padStart(2, '0') | ||
|
|
||
| return `${paddedHour2}:${minutes} am`; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| const currentOutput = formatAs12HourClock("08:00"); | ||
| const targetOutput = "08:00 am"; | ||
|
|
@@ -23,3 +48,18 @@ console.assert( | |
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| ); | ||
|
|
||
| const currentOutput3 = formatAs12HourClock("00:00"); | ||
| const targetOutput3 = "12:00 am"; | ||
| console.assert( | ||
| currentOutput3 === targetOutput3, | ||
| `current output: ${currentOutput3}, target output: ${targetOutput3}` | ||
| ); | ||
|
|
||
|
|
||
| const currentOutput4 = formatAs12HourClock("12:00"); | ||
| const targetOutput4 = "12:00 pm"; | ||
| console.assert( | ||
| currentOutput4 === targetOutput4, | ||
| `current output: ${currentOutput4}, target output: ${targetOutput4}` | ||
| ); | ||
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.
Do you need to use the brackets here?