-
-
Notifications
You must be signed in to change notification settings - Fork 134
ITP_GLASGOW_MAR | HANNA_MYKYTIUK | MODULE_STRUCTURING_AND_TESTING_DATA | SPRINT_1 #433
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
f4e9c6b
6c4cfde
980fac0
3ee1a41
c61a07a
6b88353
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,6 +1,6 @@ | ||
let count = 0; | ||
let count = 0; //Variable "count" created and assigned value equal to 0. | ||
|
||
count = count + 1; | ||
count = count + 1; // Variable "count" assigned new value, equal old value + 1. | ||
|
||
// Line 1 is a variable declaration, creating the count variable with an initial value of 0 | ||
// Describe what line 3 is doing, in particular focus on what = is doing |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
This is just an instruction for the first activity - but it is just for human consumption | ||
We don't want the computer to run these 2 lines - how can we solve this problem? | ||
//This is just an instruction for the first activity - but it is just for human consumption | ||
//We don't want the computer to run these 2 lines - how can we solve this problem? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
// trying to create an age variable and then reassign the value by 1 | ||
|
||
const age = 33; | ||
let age = 33; | ||
age = age + 1; | ||
|
||
console.log(age); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// Currently trying to print the string "I was born in Bolton" but it isn't working... | ||
// what's the error ? | ||
|
||
console.log(`I was born in ${cityOfBirth}`); | ||
const cityOfBirth = "Bolton"; | ||
console.log(`I was born in ${cityOfBirth}`); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
const 12HourClockTime = "20:53"; | ||
const 24hourClockTime = "08:53"; | ||
const TwelveHourClockTime = "20:53"; | ||
const TwentyFourHourClockTime = "08:53"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ let carPrice = "10,000"; | |
let priceAfterOneYear = "8,543"; | ||
|
||
carPrice = Number(carPrice.replaceAll(",", "")); | ||
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); | ||
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); | ||
|
||
const priceDifference = carPrice - priceAfterOneYear; | ||
const percentageChange = (priceDifference / carPrice) * 100; | ||
|
@@ -12,11 +12,12 @@ console.log(`The percentage change is ${percentageChange}`); | |
// Read the code and then answer the questions below | ||
|
||
// a) How many function calls are there in this file? Write down all the lines where a function call is made | ||
|
||
// Three functions calls in lines 4,5,10 | ||
// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? | ||
|
||
// The error in line 5 | ||
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. You've identified the error, but haven't made a fix |
||
// c) Identify all the lines that are variable reassignment statements | ||
|
||
//Reassignment in line 4,5 | ||
// d) Identify all the lines that are variable declarations | ||
|
||
//The variable declarations are in line 1,2,7,8 | ||
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
//This code removes comas from prices before conversion to numbers. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,12 @@ | |
// write one test at a time, and make it pass, build your solution up methodically | ||
|
||
function isProperFraction(numerator, denominator) { | ||
if (numerator < denominator) return true; | ||
} | ||
if (numerator < denominator) { | ||
return true; | ||
} else if ( numerator >= denominator) { | ||
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. Why do you need the second if test? |
||
return false; | ||
} | ||
} | ||
|
||
// here's our helper again | ||
function assertEquals(actualOutput, targetOutput) { | ||
|
@@ -40,13 +44,15 @@ assertEquals(improperFraction, false); | |
// target output: true | ||
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true. | ||
const negativeFraction = isProperFraction(-4, 7); | ||
assertEquals(negativeFraction,true ); | ||
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 code has given you one case with negative numerators. Can you construct a test case with a negative numerator that you expect to return false. Does it? |
||
// ====> complete with your assertion | ||
|
||
// Equal Numerator and Denominator check: | ||
// Input: numerator = 3, denominator = 3 | ||
// target output: false | ||
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. | ||
const equalFraction = isProperFraction(3, 3); | ||
assertEquals(equalFraction, false); | ||
// ====> complete with your assertion | ||
|
||
// Stretch: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,22 @@ | |
// write one test at a time, and make it pass, build your solution up methodically | ||
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers | ||
function getCardValue(card) { | ||
if (rank === "A") return 11; | ||
let rank; | ||
if (card === "10"){ | ||
rank = card; | ||
} else { | ||
rank = card.charAt(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. Consider getCardValue("10♥") How does your code behave? |
||
if (rank === "A") { | ||
return 11; | ||
} else if (Number(rank) >= 2 && Number(rank) <= 9) { | ||
return Number(rank); | ||
} else if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") { | ||
return 10; | ||
} else { | ||
throw new Error("Invalid card rank"); | ||
} | ||
} | ||
|
||
// You need to write assertions for your function to check it works in different cases | ||
|
@@ -33,19 +48,30 @@ assertEquals(aceofSpades, 11); | |
// When the function is called with such a card, | ||
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). | ||
const fiveofHearts = getCardValue("5♥"); | ||
// ====> write your test here, and then add a line to pass the test in the function above | ||
assertEquals(fiveofHearts, 5); | ||
|
||
// Handle Face Cards (J, Q, K): | ||
// Given a card with a rank of "10," "J," "Q," or "K", | ||
// When the function is called with such a card, | ||
// Then it should return the value 10, as these cards are worth 10 points each in blackjack. | ||
const faceCard = getCardValue("10"); | ||
assertEquals(faceCard, 10); | ||
|
||
// Handle Ace (A): | ||
// Given a card with a rank of "A", | ||
// When the function is called with an Ace, | ||
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. | ||
const handleAce = getCardValue("A"); | ||
assertEquals(handleAce, 11); | ||
|
||
// Handle Invalid Cards: | ||
// Given a card with an invalid rank (neither a number nor a recognized face card), | ||
// When the function is called with such a card, | ||
// Then it should throw an error indicating "Invalid card rank." | ||
try { | ||
const invalidCard = getCardValue("g"); | ||
assertEquals(invalidCard, "Invalid card rank."); | ||
} catch(error){ | ||
console.log(error.message); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
function isProperFraction(numerator, denominator) { | ||
if (numerator < denominator) return true; | ||
// add your completed function from key-implement here | ||
if (numerator < denominator) { | ||
return true; | ||
} else if ( numerator >= denominator) { | ||
return false; | ||
} | ||
} | ||
|
||
module.exports = isProperFraction; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,16 @@ test("should return true for a proper fraction", () => { | |
}); | ||
|
||
// Case 2: Identify Improper Fractions: | ||
test("should return true for a improper fraction", () => { | ||
expect(isProperFraction(5, 3)).toEqual(false); | ||
}); | ||
|
||
// Case 3: Identify Negative Fractions: | ||
test("should return true for a negative fraction", () => { | ||
expect(isProperFraction(-2, 3)).toEqual(true); | ||
}); | ||
|
||
// Case 4: Identify Equal Numerator and Denominator: | ||
test("should return true for a equal fraction", () => { | ||
expect(isProperFraction(3, 3)).toEqual(false); | ||
}); | ||
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. Re-read your code carefully here. I'm guessing that you wrote this test by copying and pasting the previous test and changing it a bit? Which is common programmer practice, but a frequent source of bugs. If you do it, be careful, as the problem you have here is all too common. Also common when correcting this sort of error is to make the wrong correction! |
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 is the answer this exercise is looking for as it teaches you basic string manipulation, but if you are doing this for real, use a library (you'll get to those shortly - when you have, google path.parse )