-
-
Notifications
You must be signed in to change notification settings - Fork 219
LONDON | ITP-MAY-25 | TEWODROS BEKERE | Coursework/sprint 2 #540
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
Changes from all commits
bafaee8
ea11210
9b714c8
9b5c5d4
1640e2c
1537baf
d79a1ca
7a35859
6b2be6c
dc3b4e8
aa1f8ee
62bba09
95332ed
01079c2
e4425d9
de2a5de
e3ebe30
6e88202
376d990
1593416
ba26a99
9a7b904
09838ac
e7ceb40
46a7807
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 |
---|---|---|
|
@@ -5,16 +5,33 @@ | |
|
||
// =============> write your prediction of the error here | ||
|
||
// # Prediction: The code will throw a ReferenceError because 'num' is not defined in the function scope. | ||
// # This is because we're trying to use the variable 'num' before it's declared. | ||
// # To fix this, we need to pass the number to be squared as an argument to the function | ||
// # or declare 'num' before using it. | ||
/* | ||
function square(3) { | ||
return num * num; | ||
} | ||
|
||
*/ | ||
// =============> write the error message here | ||
// # SyntaxError: Unexpected number | ||
|
||
// =============> explain this error message here | ||
// # Explanation: The error occurs because the function parameter is incorrectly defined. | ||
// # Instead of using a variable name, a number is used directly in the function definition. | ||
// # This is not a valid way to define a function parameter in JavaScript. | ||
// # To fix this, we need to replace '3' with a valid parameter name, such as 'num'. | ||
|
||
|
||
// Finally, correct the code to fix the problem | ||
|
||
// =============> write your new code here | ||
function square(num) { | ||
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. Adding the parameter is good |
||
return num * num; | ||
} | ||
// Test the corrected function | ||
console.log(square(3)); // Output: 9 | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,11 @@ | |
|
||
// Predict the output of the following code: | ||
// =============> Write your prediction here | ||
// # The last digit of 42 is 3 | ||
// # The last digit of 105 is 3 | ||
// # The last digit of 806 is 3 | ||
|
||
const num = 103; | ||
//const num = 103; | ||
|
||
function getLastDigit() { | ||
return num.toString().slice(-1); | ||
|
@@ -15,10 +18,25 @@ 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 last digit of 42 is 3 | ||
// # The last digit of 105 is 3 | ||
// # The last digit of 806 is 3 | ||
|
||
// Explain why the output is the way it is | ||
// =============> write your explanation here | ||
// # The output is incorrect because the function getLastDigit does not take any parameters. | ||
// # It always returns the last digit of the number 103, which is 3, regardless of the input. | ||
// # The function should take a number as an argument and return its last digit. | ||
|
||
// Finally, correct the code to fix the problem | ||
// =============> write your new code here | ||
function getLastDigit(num) { | ||
return num.toString().slice(-1); | ||
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. Recognising that we need a parameter here was the main exercise, good job. It would be nice for you to remove the 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 program should tell the user the last digit of each number. | ||
|
||
// Explain why getLastDigit is not working properly - correct the problem | ||
// # The function getLastDigit was not designed to accept an argument, so it always returned the last digit of the hardcoded number 103. | ||
// # The function should accept a number as an argument and return its last digit. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,10 @@ | |
// It should return their Body Mass Index to 1 decimal place | ||
|
||
function calculateBMI(weight, height) { | ||
// return the BMI of someone based off their weight and height | ||
} | ||
const bmi = weight / (height * height); | ||
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 looks good. |
||
return bmi.toFixed(1); // Round to 1 decimal place | ||
// # Calculate BMI by dividing weight by height squared | ||
// return the BMI of someone based off their weight and height | ||
} | ||
// Example usage: | ||
console.log(`Your BMI is ${calculateBMI(70, 1.73)}`); // Should log: Your BMI is 23.4 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,30 @@ | |
// 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 to_pound(penceString) { | ||
|
||
//const penceString = PenceNumber; | ||
|
||
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"); | ||
|
||
return `£${pounds}.${pence}`; | ||
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. It is good that you tested this function and also used the return statement. Perhaps we could use 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. |
||
} | ||
console.log(to_pound("399p")); // Output: £3.99 | ||
console.log(to_pound("100p")); // Output: £1.00 | ||
console.log(to_pound("50p")); // Output: £0.50 | ||
console.log(to_pound("1p")); // Output: £0.01 | ||
console.log(to_pound("999p")); // Output: £9.99 | ||
console.log(to_pound("0p")); // Output: £0.00 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
// This is the latest solution to the problem from the prep. | ||
// 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. | ||
// The function should take a time in 24-hour format (HH:MM) and return it in 12-hour format with am/pm. | ||
// For example, "08:00" should return "08:00 am" and "23:00" should return "11:00 pm". | ||
"use strict"; | ||
/** | ||
* Converts a time string from 24-hour format (HH:MM) to 12-hour format with am/pm. | ||
* @param {string} time - The time in 24-hour format (e.g., "08:00", "23:00"). | ||
* @returns {string} The time in 12-hour format with am/pm. | ||
*/ | ||
|
||
function formatAs12HourClock(time) { | ||
const hours = Number(time.slice(0, 2)); | ||
|
@@ -23,3 +31,11 @@ console.assert( | |
currentOutput2 === targetOutput2, | ||
`current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
); | ||
|
||
// Additional test cases | ||
const currentOutput3 = formatAs12HourClock("00:00"); | ||
const targetOutput3 = "00:00 am"; // Midnight should be 12:00 am | ||
console.assert( | ||
currentOutput3 === targetOutput3, | ||
`current output: ${currentOutput3}, target output: ${targetOutput3}` | ||
); | ||
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. Well done for completing the extension here. A point to consider for this program: Inputting 12:00 gives Only for reference, here is a tested example that also handles minutes:
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
function getCardValue(card) { | ||
// replace with your code from key-implement | ||
return 11; | ||
// return 11; | ||
|
||
if (card === 'A') { | ||
return 11; // Ace can be 11 or 1, but we'll | ||
} | ||
else if (card === 'K' || card === 'Q' || card === 'J') { | ||
return 10; // Face cards are worth 10 | ||
} | ||
else { | ||
return parseInt(card); // Number cards are worth their face value | ||
} | ||
|
||
} | ||
module.exports = getCardValue; | ||
console.log(getCardValue('K')); // Should return 11 | ||
module.exports = getCardValue |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,41 @@ | ||
const getCardValue = require("./3-get-card-value"); | ||
const getCardValue = require("./3-get-card-value"); | ||
|
||
|
||
test("should return 11 for Ace of Spades", () => { | ||
const aceofSpades = getCardValue("A♠"); | ||
expect(aceofSpades).toEqual(11); | ||
}); | ||
|
||
// Case 2: Handle Number Cards (2-10): | ||
test("should return 5 for 5 of Diamonds", () => { | ||
const fiveOfDiamonds = getCardValue("5♦"); | ||
expect(fiveOfDiamonds).toEqual(5); | ||
}); | ||
test("should return 10 for 10 of Diamonds", () => { | ||
const tenOfDiamonds = getCardValue("10♦"); | ||
expect(tenOfDiamonds).toEqual(10); | ||
}); | ||
|
||
// Case 3: Handle Face Cards (J, Q, K): | ||
test("should return 10 for Face Cards", () => { | ||
const faceCards = getCardValue("K♣"); | ||
expect(faceCards).toEqual(10); | ||
}); | ||
|
||
// Case 4: Handle Ace (A): | ||
test("should return 11 for Ace of Spades", () => { | ||
const aceOfSpades = getCardValue("A♠"); | ||
expect(aceOfSpades).toEqual(11); | ||
}); | ||
|
||
// Case 5: Handle Invalid Cards: | ||
test("should return 0 for invalid card '1♠'", () => { | ||
const invalidCard = getCardValue("1♠"); | ||
expect(invalidCard).toEqual(0); | ||
}); | ||
|
||
test("should return 0 for invalid input 'Z♠'", () => { | ||
const invalidInput = getCardValue("Z♠"); | ||
expect(invalidInput).toEqual(0); | ||
}); | ||
|
||
test("should return 0 for empty string", () => { | ||
const emptyString = getCardValue(""); | ||
expect(emptyString).toEqual(0); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
function countChar(stringOfCharacters, findCharacter) { | ||
return 5 | ||
let count = 0; | ||
for (let char of stringOfCharacters) { | ||
if (char === findCharacter) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
console.log(countChar("hello world", "l")); // ➜ 3 | ||
console.log(countChar("Mississippi", "s")); // ➜ 4 | ||
console.log(countChar("banana", "n")); | ||
|
||
module.exports = countChar; |
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.
Reusing the same variable name solves this issue. Looks good.
Bear in mind for a more complicated program it could be confusing to reuse the same variables.