Repository navigation
Manchester | 26-ITP-Sep | Gustarv Nchitu | Sprint 3 | Coursework/sprint 3 #1641
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
Open
Jestavo1
wants to merge
10
commits into
CodeYourFuture:main
Choose a base branch
from
Jestavo1:coursework/sprint-3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
328f55f
Fix capitalise function error
Jestavo1 775070d
Fix percentage conversion function
Jestavo1 500a0f6
Fix square function error
Jestavo1 3c54085
Fix multiply function return
Jestavo1 cc17fe8
Fix sum function return
Jestavo1 6c11281
Fix getLastDigit function
Jestavo1 93698c7
Implement BMI calculation
Jestavo1 8c01268
Implement upper snake case function
Jestavo1 072cfc4
Implement kilograms to pounds conversion
Jestavo1 e5ebd4a
Answer time format interpretation questions
Jestavo1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // Prediction: I predict that the code will produce an error because | ||
| // str is already declared as a function parameter and is declared | ||
| // again with let. | ||
|
|
||
| // 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; | ||
| return `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| // Explanation: The parameter str is already declared when the | ||
| // function is created. We cannot declare another variable called | ||
| // str with let in the same scope. | ||
|
|
||
| // New code: | ||
| // The function now uses the existing str parameter and returns | ||
| // the capitalised string. | ||
|
|
||
| console.log(capitalise("hello")); |
|
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. Again good accurate explanations and rework here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,28 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // Prediction: An error will occur because decimalNumber is already | ||
| // declared as a function parameter and is declared again with const. | ||
| // Also, decimalNumber is being used outside the function where it | ||
| // is not available. | ||
|
|
||
| // 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(convertToPercentage(0.5)); | ||
|
|
||
| // =============> write your explanation here | ||
| // Explanation: The function parameter decimalNumber already contains | ||
| // the value we give to the function. We cannot declare another | ||
| // decimalNumber inside the function with const. The original code | ||
| // also tried to use decimalNumber outside the function, where it | ||
| // does not exist. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| // New code: The function uses the decimalNumber parameter and returns | ||
| // the percentage. We call the function with 0.5 and log the result. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,29 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
|
|
||
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // Prediction: I predict that an error will occur because 3 cannot | ||
| // be used as a function parameter name. | ||
|
|
||
| // =============> write your prediction of the error here | ||
|
|
||
| function square(3) { | ||
| function square(num) { | ||
|
|
||
| return num * num; | ||
|
|
||
| } | ||
|
|
||
| // =============> write the error message here | ||
|
|
||
| // SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
|
|
||
| // The error happens because 3 is a number, not a valid parameter name. | ||
| // A function parameter needs to be a variable name, such as num. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| console.log(square(3)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,25 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Prediction: I predict that the function will print 320, | ||
| // but the final message will say undefined because the function | ||
| // does not return the result. | ||
|
|
||
| // =============> write your prediction here | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| return a * b; | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // Explanation: console.log() displays a value on the screen, | ||
| // but return sends a value back from the function. | ||
| // The original function used console.log() instead of return, | ||
| // so the function returned undefined. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // =============> write your new code here | ||
|
|
||
| // The corrected function returns the result of multiplying a and b. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Prediction: I predict that the function will return undefined | ||
| // because return stops the function before a + b can be calculated. | ||
|
|
||
| // =============> write your prediction here | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| return a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // Explanation: The original code used return on its own line. | ||
| // When JavaScript reaches return, the function stops and returns | ||
| // undefined. The line a + b is therefore never used. | ||
| // We need to return a + b together. | ||
|
|
||
| // =============> write your explanation here | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // =============> write your new code here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,41 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| // Prediction: I predict that the program will print 3 for all | ||
| // three numbers because the function always uses the value 103. | ||
|
|
||
| // =============> Write your prediction here | ||
|
|
||
| const num = 103; | ||
|
|
||
| function getLastDigit() { | ||
| return num.toString().slice(-1); | ||
| function getLastDigit(number) { | ||
| return number.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)}`); | ||
|
|
||
| // Now run the code and compare the output to your prediction | ||
| // Output after correction: | ||
| // The last digit of 42 is 2 | ||
| // The last digit of 105 is 5 | ||
| // The last digit of 806 is 6 | ||
|
|
||
| // =============> write the output here | ||
| // Explain why the output is the way it is | ||
|
|
||
| // Explanation: The original getLastDigit function did not have | ||
| // a parameter, so the numbers 42, 105, and 806 passed to the | ||
| // function were ignored. Instead, the function always used the | ||
| // global value 103, so it returned 3 every time. | ||
|
Comment on lines
+27
to
+30
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. nice explanation here well done |
||
| // | ||
| // The corrected function has a parameter called number. | ||
| // This allows it to receive the number passed into the function. | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
| // The function now receives a number as an argument and returns | ||
| // its last digit. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js | ||
| function toPounds(kg) { | ||
| return kg * 2.20462; | ||
| } | ||
|
|
||
| // 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. | ||
|
|
||
| // You should call this function a number of times to check it works for different inputs | ||
| console.log(toPounds(1)); | ||
| console.log(toPounds(5)); | ||
| console.log(toPounds(10)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,16 @@ | ||
| function pad(num) { | ||
| let numString = num.toString(); | ||
| while (numString.length < 2) { | ||
| numString = "0" + numString; | ||
| } | ||
| return numString; | ||
| } | ||
|
|
||
| function formatTimeDisplay(seconds) { | ||
| const remainingSeconds = seconds % 60; | ||
| const totalMinutes = (seconds - remainingSeconds) / 60; | ||
| const remainingMinutes = totalMinutes % 60; | ||
| const totalHours = (totalMinutes - remainingMinutes) / 60; | ||
|
|
||
| return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; | ||
| } | ||
|
|
||
| // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit | ||
| // to help you answer these questions | ||
|
|
||
| // Questions | ||
|
|
||
| // a) When formatTimeDisplay is called how many times will pad be called? | ||
| // =============> write your answer here | ||
| // =============> 3 | ||
|
|
||
| // Call formatTimeDisplay with an input of 61, now answer the following: | ||
|
|
||
| // b) What is the value assigned to num when pad is called for the first time? | ||
| // =============> write your answer here | ||
| // =============> 0 | ||
|
|
||
| // c) What is the return value of pad when it is called for the first time? | ||
| // =============> write your answer here | ||
| // =============> "00" | ||
|
|
||
| // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer | ||
| // =============> write your answer here | ||
| // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer | ||
| // =============> 1, because the remaining seconds are 1. | ||
|
|
||
| // e) What is the return value of pad when it is called for the last time in this program? Explain your answer | ||
| // =============> write your answer here | ||
| // e) What is the return value of pad when it is called for the last time in this program? Explain your answer | ||
| // =============> "01", because pad adds a 0 to the front of 1. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Great explanations - nice refactor of the code