Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
43 changes: 39 additions & 4 deletions week-4/implement/count.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// implement a function countChar that counts the number of times a character occurs in a string

// Given the input string str,
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,// implement a function countChar that counts the number of times a character occurs in a string

// Given a string str and a single character char to search for,
// When the countChar function is called with these inputs,
Expand All @@ -11,7 +14,39 @@
// Then it should correctly count consecutive occurrences of char (e.g., 'a' appears five times in 'aaaaa').

// Scenario: No Occurrences
// Given the input string str,
// 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.
function countChar(str,char){
let count = 0;
for(let i=0; i<str.length; i++){
if(str[i] === char){
count++;
}
}
return count;
}
console.log(countChar("aaaaa","a")); // Expected output :5
console.log(countChar("banana", "a")); // Expected output: 3

// Scenario: No Occurrences
console.log(countChar("hello", "z")); // Expected output: 0
console.log(countChar("world", "a")); // Expected output: 0

// Edge cases
console.log(countChar("", "a")); // Expected output: 0 (empty string)
console.log(countChar("a", "a")); // Expected output: 1 (single character match)
console.log(countChar("AaAaA", "A"));

// assertions
console.assert(countChar("aaaaa", "a") === 5, "Test 1 Failed: Expected 5 for input ('aaaaa', 'a')");
console.assert(countChar("banana", "a") === 3, "Test 2 Failed: Expected 3 for input ('banana', 'a')");

// Scenario: No Occurrences
console.assert(countChar("hello", "z") === 0, "Test 3 Failed: Expected 0 for input ('hello', 'z')");
console.assert(countChar("world", "a") === 0, "Test 4 Failed: Expected 0 for input ('world', 'a')");

// Edge cases
console.assert(countChar("", "a") === 0, "Test 5 Failed: Expected 0 for empty string input ('', 'a')");
console.assert(countChar("a", "a") === 1, "Test 6 Failed: Expected 1 for input ('a', 'a')");
console.assert(countChar("AaAaA", "A") === 3, "Test 7 Failed: Expected 3 for case-sensitive input ('AaAaA', 'A')");

console.log("All tests passed!");
26 changes: 26 additions & 0 deletions week-4/implement/credit-card-vslidator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function creditCard(value){
Copy link

Choose a reason for hiding this comment

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

It may be too much at once. And it's not a requirement for the task. But it would be good to pay attention already to function naming and interface.
What does your function do? The current name doesn't tell that. Something about credit cards.
What should the function return? Just printing is not that useful in production. You will need to call the function from another function.
Here are a couple of examples I see could be clearer.

  1. validateCredictCard. The function would raise an exception is the card number is invalid or would just finish not returning anything if all good.
  2. isCreditCardValid. The function would return either true or false depending if the number is valid or not.

Copy link
Author

Choose a reason for hiding this comment

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

Yes I understand but they give these instructions for credit card implementation

In this project you'll write a script that validates whether or not a credit card number is valid.

Here are the rules for a valid number:

  • Number must be 16 digits, all of them must be numbers.
  • You must have at least two different digits represented (all of the digits cannot be the same).
  • The final digit must be even.
  • The sum of all the digits must be greater than 16.

For example, the following credit card numbers are valid:

9999777788880000
6666666666661666

And the following credit card numbers are invalid:

a92332119c011112 (invalid characters)
4444444444444444 (only one type of number)
1111111111111110 (sum less than 16)
6666666666666661 (odd final number)

It's a simple implementation, but I will try to improve it according to your suggestions. I’m truly happy to have your guidance as I work on improving my skills, and I appreciate the time you take to help me understand how to make my code better.

Copy link

Choose a reason for hiding this comment

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

Ah. Makes sense then. I didn't know you had precise requirements. All good then.
Did you have any questions about the function?

const cardPattern = /^\d{16}$/.test(value);
Copy link

Choose a reason for hiding this comment

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

Where did you take the rules from?
I mean 16 digits, at least 2 different digits, etc.
From what I see in Wikipedia the rules are different https://en.wikipedia.org/wiki/Payment_card_number.
Do you want to try to implement the rules? It would be an amazing excercice. Especially the the Luhn algorithm to check the last number.

Copy link
Author

Choose a reason for hiding this comment

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

I tried implementing a credit card feature, but my initial solution didn’t fully meet real-world requirements. I realized how much detail and accuracy are needed to make it work correctly. Even though my first attempt wasn’t perfect, I find it amazing to learn what’s involved in a proper implementation, and I’m excited to keep improving it to align with true real-world standards like implementing the Luhn algorithm to check the last character

if(!cardPattern){
return "Invalid: Must be 16 digits and all numbers";
}
const uniqueDigits = new Set(value.split('')).size;
if (uniqueDigits < 2) {
return "Invalid: Must contain at least two different digits";
}
if(parseInt(value.slice(-1)) % 2 !== 0){
return "Invalid: Final digit must be even";
}
const sum = Array.from(value).reduce((a,b) => a+parseInt(b),0);
if(sum <=16){
return "Invalid: Sum of all digits must be greater than 16";
}
return "Valid credit card number";

}
// Example tests
console.log(creditCard("9999777788880000")); // "Valid credit card number"
console.log(creditCard("6666666666661666")); // "Valid credit card number"
console.log(creditCard("a92332119c011112")); // "Invalid: Must be 16 digits and all numbers"
console.log(creditCard("4444444444444444")); // "Invalid: Must contain at least two different digits"
console.log(creditCard("1111111111111110")); // "Invalid: Sum of all digits must be greater than 16"
console.log(creditCard("6666666666666661")); // "Invalid: Final digit must be even"
15 changes: 15 additions & 0 deletions week-4/implement/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,18 @@

// continue testing and implementing getOrdinalNumber for additional cases
// Write your tests using Jest - remember to run your tests often for continual feedback
function getOrdinal(number){
const suffixes = ["th","st","nd","rd"];
const value = number % 100;
const sufix = (value > 10 && value <20) ? suffixes[0] : suffixes[Math.min(value%10,3)];
return value+sufix;
}
console.log(getOrdinal(1)); // "1st"
Copy link

Choose a reason for hiding this comment

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

You need more tests. Try 5 for example)

Copy link

Choose a reason for hiding this comment

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

Also the tests have to be written using Jest.
Here no better advice than just reading their docs.
Not an easy read but an important one. I think the biggest amount of time is spent in either writing tests or debugging. Not in writing code. So you should be very proficient and comfortable in tests.

Copy link
Author

Choose a reason for hiding this comment

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

"Yes my code isn’t working properly, but I’ll definitely read through the document you shared. If I run into any confusion, I’ll reach out to you for help. Thanks for your guidance!"

console.log(getOrdinal(2)); // "2nd"
console.log(getOrdinal(3)); // "3rd"
console.log(getOrdinal(4)); // "4th"
console.log(getOrdinal(11)); // "11th"
console.log(getOrdinal(21)); // "21st"
console.log(getOrdinal(42)); // "42nd"
console.log(getOrdinal(103)); // "103rd"
console.log(getOrdinal(112)); // "112th"
23 changes: 23 additions & 0 deletions week-4/implement/is-prime.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
// Given a positive integer num,
// When the isPrime function is called with num as input,
// Then it should return a boolean representing whether the num is prime
function isPrime(num){
Copy link

Choose a reason for hiding this comment

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

Nice. How did you come up with this solution?

Copy link
Author

@SyedArslanHaider SyedArslanHaider Nov 4, 2024

Choose a reason for hiding this comment

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

If conditions are written by my own and I search Google for loop code but this is very helpful

if(num <=1){
return false;
}
if(num === 2){
return true;
}
if(num % 2 === 0){
return false;
}
for(let i=3; i<=Math.sqrt(num); i++){
if(num % i === 0){
return false;
}
}
return true;
}
console.log(isPrime(2)); // true
console.log(isPrime(11)); // true
console.log(isPrime(15)); // false
console.log(isPrime(1)); // false
console.log(isPrime(17)); // true
console.log(isPrime(25)); // false
32 changes: 32 additions & 0 deletions week-4/implement/password-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,35 @@ To be valid, a password must:

You must breakdown this problem in order to solve it. Find one test case first and get that working
*/
function validPassword(pass){
if(pass < 5){
return "Error: Password must have at least 5 characters";
}
else if(!/[A-Z]/.test(pass)){
return "Error: Password must contain at least one uppercase letter (A-Z)";
}
else if(!/[a-z]/.test(pass)){
return "Error: Password must contain at least one lowercase letter (a-z)";
}
else if(!/[0-9]/.test(pass)){
return "Error: Password must contain at least one number(0-9)";
}
else if(!/[!#$%.*&]/.test(pass)){
return "Error: password at least one non-alphanumeric symbol";
}
else if(previousPasswords.includes(pass)){
return "Error: Password must not be any of the previous passwords";
}
else{
return "Password is valid";
}
}
const previousPasswords = ["Password123!", "abcDE1$", "Valid1$"];
console.log(validPassword("Abcde", previousPasswords)); // "Error: Password must contain at least one number (0-9)"
console.log(validPassword("ABCDE", previousPasswords)); // "Error: Password must contain at least one lowercase letter (a-z)"
console.log(validPassword("abcd1", previousPasswords)); // "Error: Password must have at least 5 characters"
console.log(validPassword("Valid1$", previousPasswords)); // "Error: Password must not be any of the previous passwords"
console.log(validPassword("lowercase1$", previousPasswords)); // "Error: Password must contain at least one uppercase letter (A-Z)"
console.log(validPassword("12345!", previousPasswords)); // "Error: Password must contain at least one uppercase letter (A-Z)"
console.log(validPassword("Valid1$", previousPasswords)); // "Error: Password must not be any of the previous passwords"
console.log(validPassword("NewPass1$", previousPasswords)); // password is valid
22 changes: 22 additions & 0 deletions week-4/implement/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,25 @@
// Given a target string str and a negative integer count,
// When the repeat function is called with these inputs,
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
function repeat(str,count){
if(count < 0){
throw new Error("Count must be a positive integer.");
}
if(count === 0){
return "";
}
if(count === 1){
return str;
}
if(count >1){
return str.repeat(count);
}
}
console.log(repeat("hello", 3));
console.log(repeat("world", 1));
try {
console.log(repeat("test", 0));
console.log(repeat("error", -2));
} catch (Error) {
console.error(Error.message);
}
7 changes: 7 additions & 0 deletions week-4/investigate/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ console.log(find("code your future", "z"));
// Pay particular attention to the following:

// a) How the index variable updates during the call to find
// the index value at start is 0 and string "code your future" = 16 so index value is ++ according to the condition

// b) What is the if statement used to check
// until str[index] === "u" because U is at position 7 so it returns 7

// c) Why is index++ being used?
// in each iteration the value of index is inx=creased by one until the condition is true

// d) What is the condition index < str.length used for?
// str.length means that the it takes string length that is total is 17 in this case