Skip to content
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

CYF ITP South Africa - Cape Town | Emmanuel Siziba | Module-Structuring-and-Testing-Data | Week 3 #217

Open
wants to merge 63 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
0455567
installed jest
EmmanuelSiziba Dec 3, 2024
a5805ac
get angle type function
EmmanuelSiziba Dec 3, 2024
49b57b0
exporting angle type function for testing
EmmanuelSiziba Dec 3, 2024
23eb70b
test case file
EmmanuelSiziba Dec 3, 2024
6408987
5 type of angle tests
EmmanuelSiziba Dec 3, 2024
ed96256
get card value function
EmmanuelSiziba Dec 3, 2024
9cc7315
epsorting the getcardvalue function
EmmanuelSiziba Dec 3, 2024
d8f1af0
file for get-card-value test cases
EmmanuelSiziba Dec 3, 2024
babde67
getCardValue test cases
EmmanuelSiziba Dec 3, 2024
7b610f9
is properFraction function
EmmanuelSiziba Dec 3, 2024
aab6e2e
exporting isproperfraction function
EmmanuelSiziba Dec 3, 2024
968ddf8
file for is proper function test case
EmmanuelSiziba Dec 3, 2024
0537d6f
isproperfraction test cases
EmmanuelSiziba Dec 3, 2024
0e9c76e
isValidTriangle function
EmmanuelSiziba Dec 3, 2024
699b87f
exporting the isvalidtriangle function
EmmanuelSiziba Dec 3, 2024
cc6526e
isvalid triangle test case
EmmanuelSiziba Dec 3, 2024
11783cb
commenting
EmmanuelSiziba Dec 3, 2024
4fbab41
rotate character function
EmmanuelSiziba Dec 3, 2024
099c1aa
exporting the rotateCharacter function
EmmanuelSiziba Dec 3, 2024
2a4f69a
rotate character test cases
EmmanuelSiziba Dec 4, 2024
487fb5c
rotate character file and test cases
EmmanuelSiziba Dec 4, 2024
9e9ac0c
create a count js file
EmmanuelSiziba Dec 4, 2024
66706d0
count character function
EmmanuelSiziba Dec 4, 2024
909cc91
exporting the count char function
EmmanuelSiziba Dec 4, 2024
83d1fb8
get ordinal number js file
EmmanuelSiziba Dec 4, 2024
bbac933
getOrdinalNumber function
EmmanuelSiziba Dec 4, 2024
3da5a95
get ornial number test cases
EmmanuelSiziba Dec 4, 2024
6cab33c
exporting the get ordinal number function
EmmanuelSiziba Dec 4, 2024
1e17560
js file for is prime function
EmmanuelSiziba Dec 4, 2024
de9797e
js file for password validator project
EmmanuelSiziba Dec 4, 2024
0547e6d
js file for repeat function
EmmanuelSiziba Dec 4, 2024
d79ad1c
is prime function
EmmanuelSiziba Dec 4, 2024
25b1091
exporting my function isPrime
EmmanuelSiziba Dec 4, 2024
c5fbc33
test cases for is prime function
EmmanuelSiziba Dec 4, 2024
7867ccf
password validator function
EmmanuelSiziba Dec 4, 2024
2cf59b6
test cases the paswword validator function
EmmanuelSiziba Dec 4, 2024
aeda383
function repeat
EmmanuelSiziba Dec 4, 2024
da7f3cc
exporting the module
EmmanuelSiziba Dec 4, 2024
a5ff669
test cases for the repeat function
EmmanuelSiziba Dec 4, 2024
7a365d0
exporting the check password function
EmmanuelSiziba Dec 4, 2024
2e79cba
rotate character function fixing the export
EmmanuelSiziba Dec 4, 2024
4d5e0a7
solutions to questions
EmmanuelSiziba Dec 4, 2024
1c7e88b
card validator function
EmmanuelSiziba Dec 4, 2024
9c2bed0
Update get-angle-type.js
EmmanuelSiziba Dec 8, 2024
5618c98
Update get-card-value.js
EmmanuelSiziba Dec 8, 2024
e1fd233
Update get-card-value.test.js
EmmanuelSiziba Dec 8, 2024
8a32c3f
Update get-card-value.test.js
EmmanuelSiziba Dec 8, 2024
660da3e
Update repeat.js
EmmanuelSiziba Dec 9, 2024
2d225ca
Update repeat.test.js
EmmanuelSiziba Dec 9, 2024
dcf6ce9
Update find.js
EmmanuelSiziba Dec 9, 2024
3e307a3
Update find.js
EmmanuelSiziba Dec 9, 2024
5e6d56f
Update password-validator.test.js
EmmanuelSiziba Dec 9, 2024
3603ef8
Update is-prime.test.js
EmmanuelSiziba Dec 9, 2024
2f4f386
Update is-prime.js
EmmanuelSiziba Dec 9, 2024
293e59f
Update is-prime.js
EmmanuelSiziba Dec 9, 2024
9830f5b
Update get-ordinal-number.test.js
EmmanuelSiziba Dec 9, 2024
e94ff41
Update get-angle-type.js
EmmanuelSiziba Dec 14, 2024
7dc7129
Update get-angle-type.js
EmmanuelSiziba Dec 14, 2024
5d6a07d
Update get-card-value.js
EmmanuelSiziba Dec 14, 2024
806bfcd
Update is-proper-fraction.js
EmmanuelSiziba Dec 15, 2024
70d037e
Update is-proper-fraction.test.js
EmmanuelSiziba Dec 15, 2024
8877358
Update rotate-char.js
EmmanuelSiziba Dec 15, 2024
5d38bd1
Update get-ordinal-number.test.js
EmmanuelSiziba Dec 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion Sprint-3/implement/get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Implement a function getAngleType
i// Implement a function getAngleType

// Acceptance criteria:

Expand All @@ -25,3 +25,31 @@
// Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"


function getAngleType(angle){
if(angle === 90){
return "Right angle";
}

if(angle < 90){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, 0 < acute angle < 90. Since the spec didn't specify, we can assume angle is always a positive number.
You don't have to change your code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay so you are saying since an acute angle is any angle that measures form 0 to 90, I should leave my solution as it is .

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was saying you can leave your solution as it is because the spec didn't say if the function needs to deal with angles that are negatives or angles that are more than or equal to 360.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have put in the else statement with the corresponding test case

return "Acute angle";
}
if (angle < 180){
return "Obtuse angle";
}

if(angle === 180){
return "Straight angle"
}

if(angle > 180 && angle < 360){
return "Reflex angle"
}

else {
return "Invalid angle"
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it were up to you, what do think the function should return when angle is >= 360?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will give us an angle number equal to or greater than 360.

Honestly, I was confused here because I just couldn't figure a way to code this statement " angle is greater than 180 degrees and less than 360 degrees"

so I thought I would just focus on the last part that says less than 360 degrees but I see that was only half of the answer please check I fixed my if statement.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your previous code is correct because the previous if statements have eliminated all the cases where angle is less than or equal to 180. That is, if angle is less than or equal to 180, one of the return statements would have been executed.

What I was asking is that, what if angle is more than 360? For example, 400. Someone could call your function as console.log( getAngleType(400) ).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have put in the else statement with the corresponding test case

module.exports = getAngleType;

23 changes: 23 additions & 0 deletions Sprint-3/implement/get-angle-type.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const getAngleType = require('./get-angle-type')

test('Returns "Right angle" when the angle is exactly 90 degrees', () => {
expect(getAngleType(90)).toBe('Right angle');
});

test('Returns "Acute" when the angle is less than 90 degrees', () => {
expect(getAngleType(45)).toBe('Acute angle');
});

test('Returns "Obtuse angle" when the angle is greater than 90 degrees but less than 180 degrees', () => {
expect(getAngleType(120)).toBe('Obtuse angle');
});


test('Returns "Straight angle" when the angle is exactly 180 degrees', () => {
expect(getAngleType(180)).toBe('Straight angle');
});


test('Returns "Reflex angle" when the angle is greater than 180 degrees but less than 360 degrees', () => {
expect(getAngleType(220)).toBe('Reflex angle');
});
20 changes: 20 additions & 0 deletions Sprint-3/implement/get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,23 @@
// 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."
const validRanks = new Set(['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']);

function getCardValue(rankString) {
const rank = rankString.substring(0, rankString.length - 1);

if (validRanks.has(rank)) {
if (rank === 'A') {
return 11;
} else if (rank === 'J' || rank === 'Q' || rank === 'K') {
return 10;
} else {
return parseInt(rank);
}
} else {
throw new Error(`Invalid card rank: ${rankString}`);
}
}

module.exports = getCardValue;

26 changes: 26 additions & 0 deletions Sprint-3/implement/get-card-value.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const getCardValue = require('./get-card-value')

test('getCardValue returns 11 for Ace', () => {
expect(getCardValue('A♠')).toBe(11);
});


test('getCardValue returns 10 for J, Q or K', () => {
expect(getCardValue('J♠')).toBe(10);
expect(getCardValue('Q♠')).toBe(10);
expect(getCardValue('K♠')).toBe(10);
});


test('getCardValue returns parseInt(rank) for 2 & 9', () => {
expect(getCardValue('2')).toBe(parseInt('2'));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to always include a suite character (or any character) as the last character in the "card string".

Also, "10" is a valid card rank.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this what you are saying i must do :
test('getCardValue returns parseInt(rank) for 2 to 10', () => {
expect(getCardValue('2♣')).toBe(2);
expect(getCardValue('3♥')).toBe(3);
expect(getCardValue('4♦')).toBe(4);
expect(getCardValue('5♠')).toBe(5);
expect(getCardValue('6♣')).toBe(6);
expect(getCardValue('7♥')).toBe(7);
expect(getCardValue('8♦')).toBe(8);
expect(getCardValue('9♠')).toBe(9);
expect(getCardValue('10♣')).toBe(10);
});

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function only needs to check the rank value and can assume the last character is a valid suite character.
So that means you can also set up tests like:

expect(getCardValue('2#')).toBe(2);
expect(getCardValue('9#')).toBe(9);
expect(getCardValue('10#')).toBe(10);
expect(getCardValue('Q#')).toBe(10);
expect(getCardValue('K#')).toBe(10);

  expect(() => getCardValue('200♠')).toThrow('Invalid card rank.');
  expect(() => getCardValue('2.1♠')).toThrow('Invalid card rank.');

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed it

expect(getCardValue('9')).toBe(parseInt('9'));
});

test('getCardValue throws error for invalid rank', () => {
expect(() => getCardValue('A♠♠')).toThrow('Invalid card rank.');
expect(() => getCardValue('Q$')).toThrow('Invalid card rank.');
expect(() => getCardValue('K&')).toThrow('Invalid card rank.');


});
10 changes: 10 additions & 0 deletions Sprint-3/implement/is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@
// 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.
// These acceptance criteria cover a range of scenarios to ensure that the isProperFraction function handles both proper and improper fractions correctly and handles potential errors such as a zero denominator.

function isProperFraction(numerator, denominator) {
if (denominator === 0) {
throw new Error("Denominator cannot be zero");
}
return Math.abs(numerator) < denominator;
}

module.exports = isProperFraction;

22 changes: 22 additions & 0 deletions Sprint-3/implement/is-proper-fraction.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const isProperFraction = require('./is-proper-fraction');

test('return true for a proper function', () => {
expect(isProperFraction(2, 3)).toBe(true);
});

test('returns an error for an improper function', () => {
expect(() => isProperFraction(3, 0)).toThrow('Denominator cannot be zero');
});

test('return false for a improper function', () => {
expect(isProperFraction(5, 2)).toBe(false);
});

test('return true for a proper function', () => {
expect(isProperFraction(4, 7)).toBe(true);
});

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you expect from the following function calls?
Does your function return the value you expected?

isProperFraction(4, -7);
isProperFraction(-4, 4);
isProperFraction(-7, -2);

Suggestion: Look up "Absolute Value"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isProperFraction(4, -7); I expect this one to be true
isProperFraction(-4, 4); I expect this one to false
isProperFraction(-7, -2); I expect this one to be false

So the issue with these function calls is that they did not use Absolute values is that right?

Absolute value is the distance of a number from zero on a number line. Regardless of whether the number is positive or negative,

1 its absolute value is always positive. This is the definition i found. So this means i have to remove the nagative sign in my code and use an Absolute value.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. You still need to make changes to your code.

test('return false for a improper function', () => {
expect(isProperFraction(3, 3)).toBe(false);
});

11 changes: 11 additions & 0 deletions Sprint-3/implement/is-valid-triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@
// Then it should return true because the input forms a valid triangle.

// This specification outlines the behavior of the isValidTriangle function for different input scenarios, ensuring it properly checks for invalid side lengths and whether they form a valid triangle according to the Triangle Inequality Theorem.
function isValidTriangle(a, b, c) {
if (a <= 0 || b <= 0 || c <= 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to include the if-statement at lines 37-39?
Can you find any values for a, b, and c, such that the function will fail after you removed the if-statement at lines 37-39?
If you cannot find such a, b, and c, that means you probably do not need that if-statement.

I will not go into details why in some programming languages (but not JavaScript) we need also to check if a, b, c are positives.

The main point I would like to make is, you should fully understand what you wrote in your code. An interviewer may ask you questions like what I am asking here, and it would reflect poorly on you if you cannot explain your code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought process while approaching this was since they said we must validate and invalidate our triangle, the only way to do this was through an If statement. Are you saying there's another way of solving this without an If statement?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main point I would like to make is, you should fully understand what you wrote in your code. An interviewer may ask you questions like what I am asking here, and it would reflect poorly on you if you cannot explain your code.

Thank you for this. I am at the point now in this journey whereby I can read a problem and immediately my mind starts to generate solutions. I have solved problems on code wars, that I didn't grasp their concept. i just knew what I had to do

So my problem is explaining what I am doing or just purely explaining my logic to someone else that's a real struggle but I am grateful for the comment you made. Instead of emphasizing writing the code, will place my emphasis on understanding what is needed, and what my code does and learn to explain my logic to others.

return false;
}
else if ((a + b > c) && (a + c > b) && (b + c > a)) {
return true;
}
return false;
}

module.exports = isValidTriangle;
22 changes: 22 additions & 0 deletions Sprint-3/implement/is-valid-triangle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const isValidTriangle = require('./is-valid-triangle');

test('validates a triangle where the sum of any two sides is always greater than the third side', () => {

expect(isValidTriangle('a, b, c')).toBe(true);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you were to call your isValidTriangle() function directly to check if 1, 1, 1 can form a triangle, how would you write the code to call the function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heres how i would write it :
let a = 1;
let b = 1;
let c = 1;

function isValidTriangle(a, b, c) {
if (a <= 0 || b <= 0 || c <= 0) {
return false;
} else if ((a + b > c) && (a + c > b) && (b + c > a)) {
return true;
}
return false;
}

const result = isValidTriangle(a, b, c);
console.log(result);

the mistake I made here was it the fact that I didn't plug in any values in the test case file submission.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that's not how you call the function in is-valid-triangle.test.js.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so I would have to say : let result isValidTriangle(1, 1, 1);
console.log(result);

});

//test('returns false when the sum of any two side lengths is less than or equal to the length of the third side', () => {

// expect(isValidTriangle('a, b, c')).toBe(false);
//});

//test('returns false when the sides are less than or equal to zero', () => {

// expect(isValidTriangle('a, b, c')).toBe(false);
//});

//test('returns true when the sides are the sum of any two sides is greater than the third side', () => {

// expect(isValidTriangle('a, b, c')).toBe(true);
//});
// I can not seem to make this test pass
27 changes: 20 additions & 7 deletions Sprint-3/implement/rotate-char.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,41 @@
// Given a lowercase letter character and a positive integer shift,
// When the function is called with these inputs,
// Then it should rotate the lowercase letter by shift positions within the lowercase alphabet, wrapping around if necessary, and return the rotated lowercase letter as a string.
console.log(rotateCharacter("a", 3)); // Output: "d"
console.log(rotateCharacter("f", 1)); // Output: "g"
//console.log(rotateCharacter("a", 3)); // Output: "d"
//console.log(rotateCharacter("f", 1)); // Output: "g"

// Scenario: Rotate Uppercase Letters:
// Given an uppercase letter character and a positive integer shift,
// When the function is called with these inputs,
// Then it should rotate the uppercase letter by shift positions within the uppercase alphabet, wrapping around if necessary, and return the rotated uppercase letter as a string.
console.log(rotateCharacter("A", 3)); // Output: "D"
console.log(rotateCharacter("F", 1)); // Output: "G"
//console.log(rotateCharacter("A", 3)); // Output: "D"
//console.log(rotateCharacter("F", 1)); // Output: "G"

// Scenario: Leave Non-Letter Characters Unchanged:
// Given a character that is not a letter (neither uppercase nor lowercase) and any positive or negative shift value,
// When the function is called with these inputs,
// Then it should return the character unchanged.
// This specification outlines the behavior of the rotateCharacter function for different input scenarios, including valid and invalid characters, and defines the expected output or action for each case.
console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter)
//console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter)

// Scenario: Shifting a Character with Wraparound
// Given a character char within the lowercase alphabet range (e.g., 'z') or the uppercase alphabet range (e.g., 'Z'),
// And a positive integer shift that causes the character to wrap around the alphabet when rotated (e.g., a shift of 3 for 'z' or 'Z'),
// When the rotateCharacter function is called with char and shift as inputs,
// Then it should correctly rotate the character by shift positions within the alphabet while handling the wraparound,
// And the function should return the rotated character as a string (e.g., 'z' rotated by 3 should become 'c', 'Z' rotated by 3 should become 'C').
console.log(rotateCharacter("z", 1)); // Output: "a" (preserves case, but wraps around)
console.log(rotateCharacter("Y", 2)); // Output: "A" (preserves case, but wraps around)
//console.log(rotateCharacter("z", 1)); // Output: "a" (preserves case, but wraps around)
//console.log(rotateCharacter("Y", 2)); // Output: "A" (preserves case, but wraps around)



function rotateCharacter(char, shift) {
const isLowercase = char.toLowerCase() === char;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Characters in other languages also have lowercase/uppercase alphabets. For example
'Ñ'.toLocaleLowerCase() yields 'ñ', and 'ñ'.charCodeAt(0) is 241.

So you probably need a better way to ensure the character is one of the 26 English alphabets.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was able to fix it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was able to fix it

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expression, char.toLowerCase() === char, cannot guarantee char is one of the 26 lowercase English alphabets. If char is 'ñ', for example, the expression would still evaluates to true.

const baseCode = isLowercase ? 'a'.charCodeAt(0) : 'A'.charCodeAt(0);
const charCode = char.charCodeAt(0);

let rotatedCode = ((charCode - baseCode + shift) % 26) + baseCode;
return String.fromCharCode(rotatedCode);
}

module.exports = rotateCharacter;
19 changes: 19 additions & 0 deletions Sprint-3/implement/rotate-char.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const rotateCharacter = require('./rotate-char');

test('rotates a lowercase letter by the given shift', () => {
expect(rotateCharacter('a', 3)).toBe('d');
});

test('rotates a lowercase letter with negative shift', () => {
expect(rotateCharacter('d', -3)).toBe('a');
});

test('rotates an uppercase letter by the given shift', () => {
expect(rotateCharacter('A', 3)).toBe('D');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can also try some huge shift value. For example,

expect(rotateCharacter('A', 26 * 100 + 3)).toBe('D');   // Wrap around 100 times and shift by 3 positions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay i tried it and it passed

});

test('rotates an uppercase letter with negative shift', () => {
expect(rotateCharacter('D', -3)).toBe('A');
});


17 changes: 17 additions & 0 deletions Sprint-3/revise/implement/card-validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,20 @@ These are the requirements your project needs to fulfill:
- Return a boolean from the function to indicate whether the credit card number is valid.

Good luck!


//Answer: Breakdown
touch command to create file cardValidator.js
define a function cardValidator(cardNumber16digits){}
// this function checks if the card number entered by a user is valid.
// a valid card number has the following parameters: 16 digits, ends in even number so use %2 if ==0 then even, if cardNumber sum > 16 card is valid.
function ends in return boolean if card is valid(true) or invalid(false) */



function cardValidator(cardNumber){
// this function checks if the card number entered by a user is valid.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good start. Would you try to complete the implementation of this function and its tests?

// a valid card number has the following parameters: 16 digits, ends in even number so use %2 if ==0 then even, if cardNumber sum > 16 card is valid.
// a conditional statement to say if the cardNumber is true or false
return validCard //this is a true or false to show if the card is valid
}
11 changes: 11 additions & 0 deletions Sprint-3/revise/implement/count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function countChar(str, char) {
let count = 0;
for (let i = 0; i < str.length; i++){
if (str[i] === char) {
count++;
}
}
return count;
}

module.exports = countChar;
12 changes: 12 additions & 0 deletions Sprint-3/revise/implement/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.

const countChar = require('./count');

test('counts muiltiple characters occurrences of a character in a string', () => {
expect(countChar('aaaaa', 'a')).toBe(5);
});

test('counts overlapping occurrences of a character in a string', () => {
expect(countChar('', 'a')).toBe(0);
});


12 changes: 12 additions & 0 deletions Sprint-3/revise/implement/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function getOrdinalNumber(number) {
if (number === 1) {
return number + "st";
} else if (number === 2) {
return number + "nd";
} else if (number === 3) {
return number + "rd";
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ordinal number of 101 is "101st", and ordinal number of 11 is "11th".

May I suggest using Google or ChatGPT to find out the rules for constructing an ordinal number from an integer?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is still not yet implemented correctly.

}

module.exports = getOrdinalNumber;

24 changes: 23 additions & 1 deletion Sprint-3/revise/implement/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
// In this week's prep, we started implementing getOrdinalNumber

// continue testing and implementing getOrdinalNumber for additional cases
//Continue testing and implementing getOrdinalNumber for additional cases
// Write your tests using Jest - remember to run your tests often for continual feedback

const getOrdinalNumber = require('./get-ordinal-number');

test("Expect 'st' as suffix when the last digit is 1 but the last two digits are not 11, 12, or 13", () => {
expect(getOrdinalNumber(1)).toBe("1st");
expect(getOrdinalNumber(21)).toBe("21st");
});

test("Expect 'nd' as suffix when the last digit is 2 but the last two digits are not 12", () => {
expect(getOrdinalNumber(2)).toBe("2nd");
expect(getOrdinalNumber(22)).toBe("22nd");
});

test("Expect 'rd' as suffix when the last digit is 3 but the last two digits are not 13", () => {
expect(getOrdinalNumber(3)).toBe("3rd");
expect(getOrdinalNumber(23)).toBe("23rd");
});

test("Expect 'th' as suffix for all other cases", () => {
expect(getOrdinalNumber(4)).toBe("4th");
expect(getOrdinalNumber(11)).toBe("11th");
});
16 changes: 16 additions & 0 deletions Sprint-3/revise/implement/is-prime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function isPrime(num) {

if (num <= 1) return false;
if (num === 2) return true;

const sqrtNum = Math.sqrt(num);
for (let i = 3; i <= sqrtNum; i += 2) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good.

if (num % i === 0) {
return false;
}
}

return true;
}

module.exports = isPrime;
14 changes: 14 additions & 0 deletions Sprint-3/revise/implement/is-prime.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Given a positive integer num,
// When the isPrime function is called with num as input,
// Then it should check if the num is prime

const isPrime = require('./is-prime');

test('checks if 7 is a prime number', () => {
expect(isPrime(7)).toBe(true);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should test more numbers (especially those special cases and boundary cases like 0, 1, 2) in this script to make the test comprehensive.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, i was able to add more test numbers

expect(isPrime(0)).toBe(false);
expect(isPrime(1)).toBe(false);
expect(isPrime(2)).toBe(true);
expect(isPrime(3)).toBe(true);
expect(isPrime(5)).toBe(true);
expect(isPrime(6)).toBe(false);
expect(isPrime(23)).toBe(true);
expect(isPrime(24)).toBe(false);
});
Loading