-
-
Notifications
You must be signed in to change notification settings - Fork 546
Cape Town | 26-ITP-Sept | Leigh Ross | Sprint 3 | Coursework: Sprint 3 #1580
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
4fdac8d
e986963
da52a82
21c1378
24565bc
6aa92d2
b49b6c9
dc179bb
f77cdce
1a46297
c67e10a
8d45e6f
5125f87
d1b3b63
ec4411e
aaecbb4
6d0e108
8e8a147
dcb8312
0dfc659
e574bb5
e339abe
7d2bbe6
656d965
82a1922
b492e02
42e81a7
bb1e2e0
4fef432
5f75bcf
194c793
ac1886b
cd6e897
689ea33
cf34a26
595728f
3c8783e
0b6a121
a680b43
7277b60
388daad
47508e8
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,32 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // capitalise function takes 1 str arg | ||
| // let srting will give an error if i try to pass my string. | ||
| // remove let | ||
| // str is being put in template literal | ||
| // str[0]: 1st index of str becomes uppercase | ||
| // str.slice(1): 2nd index slice until end of str | ||
| // str = "frankocean" | ||
| // return = "Frankocean" | ||
|
|
||
|
|
||
| // 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)}`; | ||
| return str; | ||
| } | ||
| // function capitalise(str) { | ||
| // let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // return str; | ||
| // } | ||
|
|
||
|
|
||
| // =============> write your explanation here | ||
| // let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // I was right, error given is because of let str within the function. | ||
| // SyntaxError: Identifier 'str' has already been declared | ||
| // We cannot have two declarations of the same variable so we remove the let inside .capitalise. | ||
| // =============> write your new code here | ||
| function capitalise(str) { | ||
| str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
| console.log(capitalise("frankocean")) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,26 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // It will print 2 lines: | ||
| // (10*32=320) line 1: 320 | ||
| // line 2: The result of multiplying 10 and 32 is undefined | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
| // 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 | ||
| // There are two console.log() being called. | ||
| // The one inside the funtion prints the result of the multiplication | ||
| // The one outside the function prints the desired result of the whole string but there is no return from the function so it is undefined | ||
| // The result of multiplying 10 and 32 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,13 +1,24 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // I dont think the code will run at all since the return is separated from the sum that should happen | ||
| // the console.log() oustide the function will say the sum is undefined. | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| 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 of 10 and 32 is undefined | ||
| // I was wrong, I thought that the code would not run. | ||
| // The output says the sum is undefined because the return statement and the "a + b" are separated by ; | ||
| // To fix it, I just need to remove the ; next to return on line 7. | ||
| // 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 |
|---|---|---|
|
|
@@ -16,4 +16,6 @@ | |
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| return (weight / (height * height)).toFixed(1) | ||
|
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. What type of value do you expect your function to return? A number or a string? 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
console.log("123"); // Output 123
// Treated differently in the program
let sum1 = 123 + 100; // Evaluate to 223 -- a number
let sum 2 = "123" + 100; // Evaluate to "123100" -- a string. |
||
| } | ||
| console.log(calculateBMI(70, 1.73)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,10 @@ | |
| // 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 UPPER_SNAKE_CASE(str) { | ||
| let snake_case = str.replaceAll(" ", "_"); | ||
| return snake_case.toUpperCase() | ||
| } | ||
|
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. Could you look up the naming conventions in JavaScript? In particular,
Then, update the variable names according to those conventions. |
||
|
|
||
| console.log(UPPER_SNAKE_CASE("have you ever had a krispy kreme")) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,16 @@ | |
| // 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(str) { | ||
| let penceStringWithoutTrailingP = str.substring(0, str.length - 1); | ||
|
|
||
| let paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| let pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); | ||
|
|
||
| let pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); | ||
|
|
||
| console.log(`£${pounds}.${pence}`); | ||
| } | ||
|
Comment on lines
+15
to
+17
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. Outputting a value via |
||
|
|
||
| toPounds("5045p") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,24 +2,61 @@ | |
| // 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. | ||
|
|
||
| // original code: | ||
| // function formatAs12HourClock(time) { | ||
| // const hours = Number(time.slice(0, 2)); | ||
| // if (hours > 12) { | ||
| // return `${hours - 12}:00 pm`; | ||
| // } | ||
| // return `${time} am`; | ||
| // } | ||
|
|
||
| // 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}` | ||
| // ); | ||
|
|
||
| // my code: | ||
| function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| if (hours >= 12) { | ||
| return `${hours - 12}:${time.slice(3)} pm`; | ||
|
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, |
||
| } | ||
| return `${time} am`; | ||
| return `${time}:${time.slice(3)} am`; | ||
| } | ||
|
|
||
| const currentOutput = formatAs12HourClock("08:00"); | ||
| const targetOutput = "08:00 am"; | ||
| console.assert( | ||
| currentOutput === targetOutput, | ||
| `current output: ${currentOutput}, target output: ${targetOutput}` | ||
| ); | ||
| const cases = [ | ||
| { input: "00:00", expected: "12:00 am" }, // midnight | ||
| { input: "01:00", expected: "01:00 am" }, | ||
| { input: "02:00", expected: "02:00 am" }, // your failing test | ||
| { input: "09:00", expected: "09:00 am" }, | ||
| { input: "11:59", expected: "11:59 am" }, | ||
| { input: "12:00", expected: "12:00 pm" }, // noon | ||
| { input: "12:30", expected: "12:30 pm" }, | ||
| { input: "13:00", expected: "01:00 pm" }, | ||
| { input: "23:00", expected: "11:00 pm" }, // your other test | ||
| { input: "23:59", expected: "11:59 pm" }, | ||
|
Comment on lines
+38
to
+47
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. These are good test data. However, your function could not yet pass all these tests. Can you update your code accordingly? |
||
| ]; | ||
|
|
||
| const currentOutput2 = formatAs12HourClock("23:00"); | ||
| const targetOutput2 = "11:00 pm"; | ||
| console.assert( | ||
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| ); | ||
| function runTests(cases) { | ||
| let passed = 0; | ||
| for (const { input, expected } of cases) { | ||
| const actual = formatAs12HourClock(input); | ||
| const ok = actual === expected; | ||
| if (ok) passed++; | ||
| console.log( | ||
| `${ok ? "PASS" : "FAIL"} input: ${input} expected: ${expected} actual: ${actual}` | ||
| ); | ||
| } | ||
| console.log(`\n${passed}/${cases.length} passed`); | ||
| } | ||
| runTests(cases); | ||
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 works.
The alternatives of reassigning the function parameters are:
constvariableSuggestion: Use AI to explore the trade-off of these approaches.