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 all 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
3,617 changes: 3,617 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "week-4-test-example",
"description": "An example application showing how to write tests using the jest framework",
"scripts": {
"test": "jest"
},
"devDependencies": {
"jest": "^29.7.0"
}
}
73 changes: 73 additions & 0 deletions week-4/implement/card-validator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
function cardValidator(cardNumber) {
let points = 0;
if (cardNumber.lenght === 0 && isNaN(cardNumber)) {
points--;
}
const container = "";
for (i = 0; i < cardNumber.lenght; i++) {
if (!container.includes(cardNumber[i])) {
container += cardNumber[i];
}
}
if (container < 2) {
points--;
}
if (Number(cardNumber.slice(-1)) % 2 === 1) {
points--;
}
const containerNum = 0;
for (i = 0; i < cardNumber.lenght; i++) {
containerNum += Number(cardNumber[i]);
}
if (containerNum < 17) {
points--;
}

if (points < 4) {
return false;
} else {
return true;
}
}

// - 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.

test("Number must be 16 digits", function () {
const input = "123456789012345";
const currentOutput = cardValidator(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});

test("all of them must be numbers", function () {
const input = "123456789012345a";
const currentOutput = cardValidator(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});
test("at least two different digits", function () {
const input = "1111111111111111";
const currentOutput = cardValidator(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});
test("The final digit must be even", function () {
const input = "1234567890123455";
const currentOutput = cardValidator(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});
test("sum of all the digits must be greater than 16", function () {
const input = "1111111111111110";
const currentOutput = cardValidator(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});
41 changes: 41 additions & 0 deletions week-4/implement/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,44 @@
// 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;

// in case of not case sensitive strings
// str = str.toUpperCase();
// char = char.toUpperCase();

while (str.includes(char)) {
count++;
str = str.replace(char, "");
}
return count;
}

test("checks for how many specific char a string has", function () {
const inputStr = "code your future";
const inputChar = "o";
const currentOutput = countChar(inputStr, inputChar);
const targetOutput = 2;

expect(currentOutput).toBe(targetOutput);
});

test("checks for how many specific char a string has", function () {
const inputStr = "Fikret Ellek";
const inputChar = "E";
const currentOutput = countChar(inputStr, inputChar);
const targetOutput = 1;

expect(currentOutput).toBe(targetOutput);
});

test("checks for how many specific char a string has", function () {
const inputStr = "I am having a great time";
const inputChar = "a";
const currentOutput = countChar(inputStr, inputChar);
const targetOutput = 4;

expect(currentOutput).toBe(targetOutput);
});
54 changes: 54 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,57 @@

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

function getOrdinalNumber(number) {
const lastNum = number.toString().slice(-1);
if (lastNum === "1") {
return number + "st";
} else if (lastNum === "2") {
return number + "nd";
} else if (lastNum === "3") {
return number + "rd";
} else {
return number + "th";
}
return lastNum;
}

test("converts 1 to an ordinal number", function () {
const input = 1;
const currentOutput = getOrdinalNumber(input);
const targetOutput = "1st";

expect(currentOutput).toBe(targetOutput);
});

test("converts 10 to an ordinal number", function () {
const input = 10;
const currentOutput = getOrdinalNumber(input);
const targetOutput = "10th";

expect(currentOutput).toBe(targetOutput);
});

test("converts 22 to an ordinal number", function () {
const input = 22;
const currentOutput = getOrdinalNumber(input);
const targetOutput = "22nd";

expect(currentOutput).toBe(targetOutput);
});

test("converts 33 to an ordinal number", function () {
const input = 33;
const currentOutput = getOrdinalNumber(input);
const targetOutput = "33rd";

expect(currentOutput).toBe(targetOutput);
});

test("converts 45 to an ordinal number", function () {
const input = 45;
const currentOutput = getOrdinalNumber(input);
const targetOutput = "45th";

expect(currentOutput).toBe(targetOutput);
});
57 changes: 57 additions & 0 deletions week-4/implement/is-prime.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,60 @@
// 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) {
let dividers = 0;
for (i = 1; i <= num; i++) {
if (num % i === 0) {
dividers++;
}
}
if (dividers === 2) {
return true;
} else {
return false;
}
}

test("check if 0 is prime", function () {
const input = 0;
const currentOutput = isPrime(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});
test("check if 1 is prime", function () {
const input = 1;
const currentOutput = isPrime(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});
test("check if 2 is prime", function () {
const input = 2;
const currentOutput = isPrime(input);
const targetOutput = true;

expect(currentOutput).toBe(targetOutput);
});
test("check if 3 is prime", function () {
const input = 3;
const currentOutput = isPrime(input);
const targetOutput = true;

expect(currentOutput).toBe(targetOutput);
});
test("check if 4 is prime", function () {
const input = 4;
const currentOutput = isPrime(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});
test("check if 9 is prime", function () {
const input = 9;
const currentOutput = isPrime(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});
82 changes: 82 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,85 @@ 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 isPasswordValid(str) {
let points = 6;

if (str.length < 5) {
points--;
}

if (str.toUpperCase() === str) {
points--;
}

if (str.toLowerCase() === str) {
points--;
}

for (let char of str) {
if (isNaN(Number(char))) {
points--;
}
}

if (
!str.includes("!") &&
!str.includes("#") &&
!str.includes("$") &&
!str.includes("%") &&
!str.includes(".") &&
!str.includes("*") &&
!str.includes("&")
) {
points--;
}
if (points != 5) {
return false;
} else {
return true;
}
}

test("Have at least 5 characters", function () {
const input = "Aa1.";
const currentOutput = isPasswordValid(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});

test("Have at least one English uppercase letter (A-Z)", function () {
const input = "aaa1.";
const currentOutput = isPasswordValid(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});

test("Have at least one English lowercase letter (a-z)", function () {
const input = "AAA1.";
const currentOutput = isPasswordValid(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});

test("Have at least one number (0-9)", function () {
const input = "Aaaa.";
const currentOutput = isPasswordValid(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});

test(
"Have at least one non-alphanumeric symbol (!, #, $, %, ., *, " & ")",
function () {
const input = "Aaaa1";
const currentOutput = isPasswordValid(input);
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
}
);
Loading