Skip to content
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
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"requirePragma": false,
"semi": true,
"singleQuote": false,
"tabWidth": 2,
"tabWidth": 4,
"trailingComma": "es5",
"useTabs": false,
"vueIndentScriptAndStyle": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// 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) {
const rank = card.slice(0,-1) //removes the last digit
if (rank === "A") {
return 11;
}
Expand Down
80 changes: 80 additions & 0 deletions Sprint-3/3-stretch/card-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//Function to validate that input is 16 digit number
function isSixteenDigitNumber(cardNumber) {
const cardNumberLength = cardNumber.toString().length; // Get the length of the input cardNumber
if (cardNumberLength === 16) {
// Check if the length is exactly 16
for (let i = 0; i < cardNumberLength; i++) {
//Check each digit till the length of cardNumber
const char = cardNumber.toString().charAt(i); // The character at index i
if (isNaN(char)) {
//If the character is not a number
return "Input only numbers"; // Return error message
}
else {
continue; // Continue checking the next character
}
}
return true; // All characters are digits and length is 16
}
else {
return "Input 16 digits card number"; // Length is not 16
}
}

// Function to validate that the card number has at least two different digits
function hasTwoDifferentDigits(cardNumber) {
const firstDigit = cardNumber.toString().charAt(0); // Get the first digit of the card number
for (let i = 1; i < cardNumber.toString().length; i++) {
const digit = cardNumber.toString().charAt(i); // Get the digit at index i
if (digit !== firstDigit) {
return true; // At least two different digits found
}
}
return false; // All digits are the same
}

// Function to check if the last digit of the card number is even
function lastDigitIsEven(cardNumber) {
const lastDigit = cardNumber.toString().charAt(15); // Get the last digit of the card number
if (Number(lastDigit) % 2 === 0) {
return true; // Last digit is even
}
else {
return false; // Last digit is odd
}
}

//Function to calculate the sum of all digits to be > 16
function sumOfDigitsGreaterThan16(cardNumber) {
let sum = 0;
for (let i = 0; i < cardNumber.toString().length; i++) {
const digit = Number(cardNumber.toString().charAt(i)); // Get the digit at index i and convert to number
sum += digit; // Add the digit to the sum
}
if (sum > 16) {
return true; // Sum of digits is greater than 16
}
else {
return false; // Sum of digits is not greater than 16
}
}

// Main function to validate the card number
function cardValidator(cardNumber) {
if (isSixteenDigitNumber(cardNumber) === true) {
if (
hasTwoDifferentDigits(cardNumber) &&
lastDigitIsEven(cardNumber) &&
sumOfDigitsGreaterThan16(cardNumber)
) {
return "Valid card number"; // All validations passed
} else {
return "Invalid card number"; // One or more validations failed
}
}
else {
return isSixteenDigitNumber(cardNumber); // Return the error message from isSixteenDigitNumber function
}
}

module.exports = cardValidator;
20 changes: 20 additions & 0 deletions Sprint-3/3-stretch/card-validator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const cardValidator = require("./card-validator");
test (('valid 16digit card number passes validation'), () => {
expect(cardValidator(2549876514523548)).toEqual("Valid card number")
});

test (('16 digit card number that has an odd last digit'), () => {
expect(cardValidator(2547896254356557)).toEqual('Invalid card number')
});

test (('16 digit card number with all same digits'), () => {
expect(cardValidator(1111111111111111)).toEqual('Invalid card number')
});

test (('16 digit card number with at least two different digits, last digit even,sum off digits < 16'), () => {
expect(cardValidator(1010101010101012)).toEqual('Invalid card number')
});

test (('16 digit card number with not only numbers'), () => {
expect(cardValidator('1df0877999954677')).toEqual('Input only numbers')
});
80 changes: 77 additions & 3 deletions Sprint-3/3-stretch/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,80 @@
function passwordValidator(password) {
return password.length < 5 ? false : true
//Has at least 5 digits
function isAtLeastFive(password) {
if (password.length >= 5) {
return true;
}
return false;
}

//Has at least one upper case English letter
function hasAtLeastOneUpperCase(password) {
let index = 0; //starts from first index location to check
while (index < password.length) { //When our location is smaller than the total str length
const char = password.charAt(index); //This is the location of each digit at the moment of the loop
if (char >= "A" && char <= "Z"); //To include all alphabet in capital letters in at least one location
return true;
index++ //Moves loop to next digit for checking
}
return false; //If loop is finished and there were no capitals found
}

//Has at least one lower letter in English
function hasAtLeastOneLowerCase(password) {
let index = 0; //starts from first index location to check
while (index < password.length) { //When our location is smaller than the total str length
const char = password.charAt(index); //This is the location of each digit at the moment of the loop
if (char >= "a" && char <= "z"); //To include all alphabet in lower letters in at least one location
return true;
index++ //Moves loop to next digit for checking
}
return false; //If loop is finished and there were no lower letters found
}

//Has at least one number from 0 to 9
function hasAtLeastOneNumber(password) {
let index = 0; //starts from first index location to check
while (index < password.length) { //When our location is smaller than the total str length
const char = password.charAt(index); //This is the location of each digit at the moment of the loop
if (char >= "0" && char <= "9"); //To include all numbers in at least one location
return true;
index++ //Moves loop to next digit for checking
}
return false; //If loop is finished and there were no numbers found
}

//Has at least one special character "!", "#", "$", "%", ".", "*", "&"
function hasAtLeastOneSpecialCharacter(password) {
const specialCharacter = ["!", "#", "$", "%", "*", "&"]
let index = 0;
while (index < password.length) {
const char = password.charAt(index); //This is the location of each digit at the moment of the loop
if (specialCharacter.includes(char)); //To include a symbol in at least one location
return true;
index++ //Moves loop to next digit for checking
}
return false; //If loop is finished and there were no symbols found
}

//Must not be a previous password in passwords array
function isNewPassword(password, passwords) { //Password is the new input, passwords are the previous array
return !passwords.includes(password); //!is a negative statement so it will return false if new password was used before
}


//Has at least 5 digits & all above conditions are met
function passwordValidator(password, passwords) {
return (isAtLeastFive &&
hasAtLeastOneUpperCase(password) &&
hasAtLeastOneLowerCase(password) &&
hasAtLeastOneNumber(password) &&
hasAtLeastOneSpecialCharacter(password) &&
isNewPassword(password, passwords));
}

module.exports = passwordValidator;
module.exports = {isAtLeastFive,
hasAtLeastOneUpperCase,
hasAtLeastOneLowerCase,
hasAtLeastOneNumber,
hasAtLeastOneSpecialCharacter,
isNewPassword,
passwordValidator}
14 changes: 11 additions & 3 deletions Sprint-3/3-stretch/password-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ 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
*/
const isValidPassword = require("./password-validator");
const {isAtLeastFive,
hasAtLeastOneUpperCase,
hasAtLeastOneLowerCase,
hasAtLeastOneNumber,
hasAtLeastOneSpecialCharacter,
isNewPassword,
passwordValidator} = require("./password-validator");

test("password has at least 5 characters", () => {
// Arrange
const password = "12345";
const password = "1Ab4&";
const passwords = ["gJ25!", "15#Ki", "6z9F*"]
// Act
const result = isValidPassword(password);
const result = passwordValidator(password, passwords);
// Assert
expect(result).toEqual(true);
}
Expand Down
12 changes: 12 additions & 0 deletions add-an-I.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function drawStairs(n) {
let result = "";
for (let i = 0; i < n; i++) {
if (i > 0) {
result += "\n";
}
result += " ".repeat(i) + "I";
}
return result;
}

console.log(drawStairs(5));
8 changes: 8 additions & 0 deletions elevator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function elevatorCloser(left, call, right) {
const leftFloor = Math.abs(left - call);
const rightFloor = Math.abs(right - call);
if (leftFloor >= rightFloor) {
return "right";
}
return "left";
}