generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 313
Cape Town | 2026-ITP-Jan | Isaac Abodunrin | Sprint 2 | Coursework Exercises #933
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
bytesandroses
wants to merge
11
commits into
CodeYourFuture:main
Choose a base branch
from
bytesandroses:coursework/sprint-2
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
11 commits
Select commit
Hold shift + click to select a range
93ce739
Sprint 2 Coursework: Key Errors 0
bytesandroses 00c4555
Sprint 2 Coursework: Key Errors 1
bytesandroses 88934bf
Sprint 2 Coursework: Key Errors 2
bytesandroses 20039ce
Sprint 2 Coursework: Mandatory Debug 0
bytesandroses ac6d7df
Sprint 2 Coursework: Mandatory Debug 1
bytesandroses 26abdeb
Sprint 2 Coursework: Mandatory Debug 2
bytesandroses f1d4877
Sprint 2 Coursework: Mandatory Implement 1
bytesandroses 4af7d73
Sprint 2 Coursework: Mandatory Implement 2
bytesandroses cd69572
Sprint 2 Coursework: Mandatory Implement 2
bytesandroses bc3c74c
Sprint 2 Coursework: Mandatory Implement 3
bytesandroses 1aad8fa
Sprint 2 Coursework: Mandatory Interpret time-format
bytesandroses 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,23 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> Prediction <============= | ||
| // I think the code will work as expected. For example, if capitalise("andora") | ||
| // is run, it should return "Andora". | ||
|
|
||
| // call the function capitalise with a string input | ||
| // interpret the error message and figure out why an error is occurring | ||
| // =============> Actual Results <============= | ||
|
|
||
| // The code did not run successfully due to a syntax error. Since the variable | ||
| // str was already declared when in the capitalise function input, it cannot | ||
| // be declared again as was done on line 6. | ||
|
|
||
| // =============> Corrected Script <============= | ||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| if (str === "") { | ||
| return ""; | ||
| } | ||
|
|
||
| return `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| console.assert( | ||
| capitalise("andora") === "Andora", | ||
| `current output: ${capitalise("andora")}, expected output: Andora` | ||
| ); |
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,16 @@ | ||
| // Predict and explain first... | ||
| // =============> Prediction <============= | ||
| // The script won't run because the decimalNumber variable is being declared | ||
| // twice (first in the convertToPercentage input, and again inside the function) | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
| // =============> Actual Results <============= | ||
| // The script threw a syntax error due to the double variable declaration | ||
|
|
||
| // =============> Corrected Script <============= | ||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| return `${decimalNumber * 100}%`; | ||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| console.assert( | ||
| convertToPercentage(0.45) === "45%", | ||
| `current output: ${convertToPercentage(0.45)}, expected output: 45%` | ||
| ); |
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,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; | ||
| // =============> Prediction <============= | ||
| // It will throw an error. This is because the variable num is being used without | ||
| // being declared previously. | ||
|
|
||
| // =============> Actual Results <============= | ||
| // Error message: function square(3) { | ||
| // ^ | ||
| // SyntaxError: Unexpected number | ||
|
|
||
| // =============> Explanation <============= | ||
| // An error was thrown because the because we cannot name a variable with a number | ||
| // as has been done in the function input | ||
|
|
||
| // =============> Corrected Script <============= | ||
| function square(num) { | ||
| return num * num; | ||
| } | ||
|
|
||
| // =============> write the error message here | ||
|
|
||
| // =============> explain this error message here | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| console.assert( | ||
| square(3) === 9, | ||
| `actual result: ${square(3)}, expected result: 9` | ||
| ); |
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,26 @@ | ||
| // Predict and explain first... | ||
| // =============> Prediction <============= | ||
| // The code will not throw an error -- it's syntactically fine. However, the | ||
| // multiply function is missing a return call, so it will return undefined | ||
| // by default. The function therefore simply prints the result of the | ||
| // multiplication instead of actually returning it as a value. | ||
|
|
||
| // =============> write your prediction here | ||
| // =============> Actual Results <============= | ||
| // 320 | ||
| // The result of multiplying 10 and 32 is undefined | ||
|
|
||
| // =============> Explanation <============= | ||
| // The 320 is printed in the terminal because of the console.log(). Because | ||
| // no return was written, the default undefined is generated. | ||
|
|
||
| // =============> Corrected Script <============= | ||
| 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 | ||
| const actualResult = multiply(10, 32); | ||
| const expectedResult = 320; | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| console.assert( | ||
| actualResult === expectedResult, | ||
| `actual result: ${actualResult}, expected result: ${expectedResult}` | ||
| ); |
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,27 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> Prediction <============= | ||
| // The script will run but not as intended. The sum function has a valid | ||
| // return, though it is simply null. Since a + b calculation comes after | ||
| // the return, it isn't run as the program exits the loop once the return | ||
| // call has been made. I therefore expect the following to be printed on | ||
| // the terminal: | ||
| // The sum of 10 and 32 is undefined | ||
|
|
||
| // =============> Actual Results <============= | ||
| // The sum of 10 and 32 is undefined | ||
|
|
||
| // =============> Explanation <============= | ||
| // The return was made before a and b could be added. | ||
|
|
||
| // =============> Corrected Script <============= | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| return a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| const actualResult = sum(10, 32); | ||
| const expectedResult = 42; | ||
|
|
||
| // =============> write your explanation here | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| console.assert( | ||
| actualResult === expectedResult, | ||
| `actual result: ${actualResult}, expected result: ${expectedResult}` | ||
| ); |
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... | ||
| // =============> Prediction <============= | ||
| // The script will run but not as intended. Each time the getLastDigit | ||
| // function is ran, it will return "3". This is because it's using the | ||
| // global variable num | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // =============> Actual Results <============= | ||
| // The last digit of 42 is 3 | ||
| // The last digit of 105 is 3 | ||
| // The last digit of 806 is 3 | ||
|
|
||
| const num = 103; | ||
| // =============> Explanation <============= | ||
| // The getLastDigit is using the global num variable instead of an | ||
| // input variable | ||
|
|
||
| function getLastDigit() { | ||
| // =============> Corrected Script <============= | ||
| 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)}`); | ||
| const actualResult_1 = getLastDigit(42); | ||
| const actualResult_2 = getLastDigit(105); | ||
| const actualResult_3 = getLastDigit(806); | ||
|
|
||
| // Now run the code and compare the output to your prediction | ||
| // =============> write the output here | ||
| // Explain why the output is the way it is | ||
| // =============> write your explanation here | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| const expectedResult_1 = "2"; | ||
| const expectedResult_2 = "5"; | ||
| const expectedResult_3 = "6"; | ||
|
|
||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
| console.assert( | ||
| actualResult_1 === expectedResult_1, | ||
| `actual result: ${actualResult_1}, expected result: ${expectedResult_1}` | ||
| ); | ||
|
|
||
| console.assert( | ||
| actualResult_2 === expectedResult_2, | ||
| `actual result: ${actualResult_2}, expected result: ${expectedResult_2}` | ||
| ); | ||
|
|
||
| console.assert( | ||
| actualResult_3 === expectedResult_3, | ||
| `actual result: ${actualResult_3}, expected result: ${expectedResult_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,19 +1,13 @@ | ||
| // 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) { | ||
| const bmi = parseFloat((weight / height ** 2).toFixed(1)); | ||
|
|
||
| // You will need to implement a function that calculates the BMI of someone based off their weight and height | ||
| return bmi; | ||
| } | ||
|
|
||
| // 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 | ||
| const calculatedBMI = calculateBMI(70, 1.73); | ||
| const actualBMI = 23.4; | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| console.assert( | ||
| calculatedBMI === actualBMI, | ||
| `calculated BMI: ${calculatedBMI}, actual BMI: ${actualBMI}` | ||
| ); |
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,16 +1,21 @@ | ||
| // A set of words can be grouped together in different cases. | ||
| function upperCaseSnake(str) { | ||
| const upperCaseSnake = str.toUpperCase().replaceAll(" ", "_"); | ||
|
|
||
| // For example, "hello there" in snake case would be written "hello_there" | ||
| // UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces. | ||
| return upperCaseSnake; | ||
| } | ||
|
|
||
| // Implement a function that: | ||
| let returnedUpperCaseSnake = upperCaseSnake("hello_there"); | ||
| let actualUpperCaseSnake = "HELLO_THERE"; | ||
|
|
||
| // Given a string input like "hello there" | ||
| // When we call this function with the input string | ||
| // it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE" | ||
| console.assert( | ||
| returnedUpperCaseSnake === actualUpperCaseSnake, | ||
| `returned upperCaseSnake: ${returnedUpperCaseSnake}, actual upperCaseSnake: ${actualUpperCaseSnake}` | ||
| ); | ||
|
|
||
| // Another example: "lord of the rings" should be "LORD_OF_THE_RINGS" | ||
| returnedUpperCaseSnake = upperCaseSnake("lord of the rings"); | ||
| actualUpperCaseSnake = "LORD_OF_THE_RINGS"; | ||
|
|
||
| // 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 | ||
| console.assert( | ||
| returnedUpperCaseSnake === actualUpperCaseSnake, | ||
| `returned upperCaseSnake: ${returnedUpperCaseSnake}, actual upperCaseSnake: ${actualUpperCaseSnake}` | ||
| ); |
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,29 @@ | ||
| // In Sprint-1, there is a program written in interpret/to-pounds.js | ||
| function toPounds(str) { | ||
| const price = (parseInt(str.replace("p", ""), 10) / 100).toFixed(2); | ||
|
|
||
| // 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. | ||
| return `£${price}`; | ||
| } | ||
|
|
||
| // You should call this function a number of times to check it works for different inputs | ||
| let returnedPrice = toPounds("399p"); | ||
| let actualPrice = "£3.99"; | ||
|
|
||
| console.assert( | ||
| returnedPrice === actualPrice, | ||
| `returned price: ${returnedPrice}, actual price: ${actualPrice}` | ||
| ); | ||
|
|
||
| returnedPrice = toPounds("1p"); | ||
| actualPrice = "£0.01"; | ||
|
|
||
| console.assert( | ||
| returnedPrice === actualPrice, | ||
| `returned price: ${returnedPrice}, actual price: ${actualPrice}` | ||
| ); | ||
|
|
||
| returnedPrice = toPounds("102342342342p"); | ||
| actualPrice = "£1023423423.42"; | ||
|
|
||
| console.assert( | ||
| returnedPrice === actualPrice, | ||
| `returned price: ${returnedPrice}, actual price: ${actualPrice}` | ||
| ); |
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
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.
Note: Deleting the global
numis optional. Within the function block,numis resolved to the parameternum.If you are interested in the topic, you can looking up these two concepts, identifier scope and identifier resolution, in the context of JavaScript programming. ChatGPT can give a good explanation.