-
-
Notifications
You must be signed in to change notification settings - Fork 546
London | 26-ITP-Sep | Bartosz Kawiak| Sprint 3 | Coursework-completed #1582
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
b285176
ef676e6
d0828f2
e67a022
989aa0b
c0d3b07
29c88fd
da1df5f
eabe4c7
3fdce9e
fc6e83d
457005f
8473252
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,34 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| /* =============> I predict that the function would make 1st character uppercase and | ||
| then adds the rest of the string using slice. */ | ||
|
|
||
| // call the function capitalise with a string input | ||
| // interpret the error message and figure out why an error is occurring | ||
|
|
||
| //We cannot declare str again because it has already been declared as a parameter of the function. | ||
|
|
||
| /* OLD CODE | ||
|
|
||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
| */ | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
|
|
||
| //First version below, another approach displayed as a working code | ||
|
|
||
| // function capitalise(str) { | ||
| // str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // return str; | ||
| // } | ||
|
|
||
| function capitalise(str) { | ||
| const firstLetter = str[0]; | ||
| const smallStr = str.slice(1); | ||
| const upperLetter1 = firstLetter.toUpperCase(); | ||
| let newStr = upperLetter1 + smallStr; | ||
| return newStr; | ||
| } | ||
| 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 | ||
| // =============> We will get a SyntaxError as decimalNumber has already been declared as a parameter of the function. | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| /* | ||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
| console.log(decimalNumber); */ | ||
|
|
||
| // =============> write your explanation here | ||
| // We don't need to redeclare decimalNumber as it's value comes from function parameter, | ||
| // also console.log wont work as decimalNumber is only created inside the function. | ||
|
|
||
| // 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,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 | ||
| // =============> SyntaxError because function parameter must be a name not a number or value. The error occurs because 3 is a number. | ||
|
|
||
| /* | ||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| } */ | ||
|
|
||
| // =============> write the error message here | ||
| // =============> SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // =============> This error message is cause by trying to assign a number as a parameter, a parameter needs to be identifier such as num. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
| console.log(square(5)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,23 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // =============> We will log the result in a console but function won't return it as a value. | ||
| // console.log only displays the result, it does not return it from the function. | ||
|
|
||
| /* | ||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| */ | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| //We need to return a * b so the result can be used where the function is called. | ||
|
|
||
| // 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,20 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
|
|
||
| // =============> return on its own means that the function ends without returning a value, so the result is undefined. | ||
| /* | ||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| */ | ||
|
|
||
| // =============> write your explanation here | ||
| // return on it's own means that the function wont refer to parameters and will come up as undefined. a+ b is never reached because it comes after return. | ||
| // 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)}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,5 +15,24 @@ | |
| // 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 | ||
| let bmiNum = weight / (height * height); | ||
| bmiNum = bmiNum.toFixed(1); | ||
| return typeof bmiNum; | ||
| } | ||
|
Comment on lines
+18
to
21
Contributor
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. Does this function return what you expect? |
||
|
|
||
| console.log(calculateBMI(70, 1.73)); | ||
|
|
||
| // What type of value do you expect your function to return? A number or a string? | ||
| //I expect function to return string as required, toFixed() change the type of number to a string. | ||
|
|
||
| // Does your function return the type of value you expect? | ||
| //Yes I did expect a string returned. | ||
|
|
||
| // Different types of values may appear identical in the console output, but they are represented and treated differently in the program. For example, | ||
|
|
||
| // console.log(123); // Output 123(number) | ||
| // console.log("123"); // Output 123(string) | ||
|
|
||
| // // Treated differently in the program | ||
| // let sum1 = 123 + 100; // Evaluate to 223 -- a number | ||
| // let sum 2 = "123" + 100; // Evaluate to "123100" -- a string. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,13 @@ | |
|
|
||
| function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| const minutes = Number(time.slice(3, 5)); | ||
|
Contributor
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. Note: The .slice() method supports negative indices, which count positions from the end of the string. For example, |
||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`; | ||
| } else if (hours === 12) { | ||
| return `${hours}:${minutes.toString().padStart(2, "0")} pm`; | ||
| } else if (hours === 0) { | ||
| return `12:${minutes.toString().padStart(2, "0")} am`; | ||
| } | ||
| return `${time} am`; | ||
|
Comment on lines
8
to
15
Contributor
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. This work. Could also consider this approach:
|
||
| } | ||
|
|
@@ -14,12 +19,63 @@ 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}`, | ||
| ); | ||
|
|
||
| formatAs12HourClock("19:00"); | ||
| let output = formatAs12HourClock("19:00"); | ||
| let target = "07:00 pm"; | ||
| console.assert( | ||
| output === target, | ||
| `current output: ${output}, target output: ${target}`, | ||
| ); | ||
| console.log(formatAs12HourClock("19:00")); | ||
| // //works | ||
| formatAs12HourClock("9:00"); | ||
| output = formatAs12HourClock("9:00"); | ||
| target = "9:00 am"; | ||
| console.assert( | ||
| output === target, | ||
| `current output: ${output}, target output: ${target}`, | ||
| ); | ||
| console.log(formatAs12HourClock("9:00")); | ||
| // //works | ||
| formatAs12HourClock("12:00"); | ||
| output = formatAs12HourClock("12:00"); | ||
| target = "12:00 pm"; | ||
| console.assert( | ||
| output === target, | ||
| `current output: ${output}, target output: ${target}`, | ||
| ); | ||
|
|
||
| console.log(formatAs12HourClock("12:00")); | ||
| //Function needs else if statement to includes code behavior when "12:00" will be the argument value. | ||
|
|
||
| console.log(formatAs12HourClock("9:00")); | ||
| // //works | ||
| formatAs12HourClock("00:00"); | ||
| output = formatAs12HourClock("00:00"); | ||
| target = "12:00 am"; | ||
| console.assert( | ||
| output === target, | ||
| `current output: ${output}, target output: ${target}`, | ||
| ); | ||
| console.log(formatAs12HourClock("00:00")); | ||
| //Function needs else if statement to includes code behavior when "00:00" will be the argument value. | ||
|
|
||
| formatAs12HourClock("19:37"); | ||
| output = formatAs12HourClock("19:37"); | ||
| target = "07:37 pm"; | ||
| console.assert( | ||
| output === target, | ||
| `current output: ${output}, target output: ${target}`, | ||
| ); | ||
| console.log(formatAs12HourClock("19:37")); | ||
| //For the test to pass I had to create a minutes variable to extract the minutes from the input. When minutes were converted to a number, zero at the start of the string was removed, so padStart(2, "0") was used to add it back. | ||
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.
This work.
The alternatives of reassigning the function parameters are:
constvariableSuggestion: Use AI to explore the trade-off of these approaches.