-
-
Notifications
You must be signed in to change notification settings - Fork 546
Cape Town | 26-ITP-September | Sima Nongawuza | Sprint 3 | Coursework/sprint 3 #1637
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
352f54e
38d9150
7dfcfcd
ea78769
9f6a5e5
9b6c0a5
cffe689
c4c22e3
a396ade
4a52872
6407f1a
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 |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
|
|
||
| // num is declared as a const variable of 103, this will give the same outcome because num is suppose to a placeholder not a number. | ||
| const num = 103; | ||
|
|
||
| function getLastDigit() { | ||
|
|
@@ -14,11 +14,20 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`); | |
| console.log(`The last digit of 806 is ${getLastDigit(806)}`); | ||
|
|
||
| // Now run the code and compare the output to your prediction | ||
| // =============> write the output here | ||
| // The output gives the same answer that is 3 | ||
|
|
||
| // Explain why the output is the way it is | ||
| // =============> write your explanation here | ||
| // The variable num was declared to a const number (103) instead of a placeholder therefore it cannot be changed | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function getLastDigit(num) { | ||
| return num.toString().slice(-1); | ||
| } | ||
|
|
||
| console.log(`The last digit of 42 is ${getLastDigit(42)}`); | ||
| console.log(`The last digit of 105 is ${getLastDigit(105)}`); | ||
| console.log(`The last digit of 806 is ${getLastDigit(806)}`); | ||
|
|
||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
| // The const variable must be removed from the code. The computer reads that first and gives the same output for all console.log statements. | ||
| // A placeholder variable for num must be declared in the function parameter. | ||
|
Comment on lines
+32
to
+33
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. I think line 33 would be a more accurate explanation. For line 32, even the |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,12 @@ | |
| // Then when we call this function with the weight and height | ||
| // It should return a string of their Body Mass Index to 1 decimal place | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| //function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| //} | ||
| function calculateBMI(weight, height) { | ||
| squareHieght = height * height; | ||
| calculateBMI = Math.round(((weight / squareHieght)) *10 )/10; | ||
|
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. While your program produce the correct result, the |
||
| return calculateBMI | ||
| } | ||
| console.log(calculateBMI(70, 1.73)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,15 @@ | |
| // 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) { | ||
| const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); | ||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length -2); | ||
| const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length -2).padEnd(2, "0"); | ||
|
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. Just to clarify a bit, why |
||
| return `${pounds}.${pence}`; | ||
| } | ||
| console.log(toPounds("399p")) | ||
| console.log(toPounds("10p")) | ||
| console.log(toPounds("1023p")) | ||
| console.log(toPounds("5p")) | ||
|
Comment on lines
+15
to
+18
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. Your program looks generally ok to me. However, have you think of |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,24 +2,28 @@ | |
| // 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 timeString = String(time); | ||
|
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. Just wondering why this line is needed? |
||
| let [hours, minutes] = timeString.split(":").map(Number); | ||
| if (isNaN(minutes)){ | ||
| minutes = 0; | ||
| } | ||
| let period = "am" | ||
| if (hours >= 12) { | ||
| period = "pm"; | ||
| if (hours > 12) { | ||
| hours = hours - 12; | ||
| } | ||
| } else if (hours === 0) { | ||
| hours = 12; | ||
| } | ||
| return `${time} am`; | ||
| const paddedHours = String(hours).padStart(2, "0"); | ||
| const paddedMinutes = String(minutes).padStart(2, "0"); | ||
| return `${paddedHours}:${paddedMinutes}${period}`; | ||
|
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. Based on the original given |
||
| } | ||
|
|
||
| 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}` | ||
| ); | ||
| console.log(formatAs12HourClock("00:30")) | ||
| console.log(formatAs12HourClock("23:32")) | ||
| console.log(formatAs12HourClock("08:50")) | ||
| console.log(formatAs12HourClock("13:00")) | ||
|
Comment on lines
+26
to
+29
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. Your test cases have covered am and pm. However, there are still 3 boundary cases which have not been covered yet. Can you think of them and add them too? Thank you. |
||
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.
Found a typo and should be "... The return value is not defined..." - because precisely speaking, the
sumfunction itself is defined (in line 4) and thereturnitself is a built-in function (i.e. defined in JavaScript).