diff --git a/.DS_Store b/.DS_Store index d06ffd2ea..93fc62522 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Week3/homework/creditcardvalidator.js b/Week3/homework/creditcardvalidator.js new file mode 100644 index 000000000..a0bceec98 --- /dev/null +++ b/Week3/homework/creditcardvalidator.js @@ -0,0 +1,107 @@ +'use strict'; +/*main function is getting the info from other functions and returns the errors of the +invalid card number or returns the statement that says card number is valid.*/ +function main(cardNum) { + if (isValid(cardNum) === false) { + console.log(`(${cardNum})`) + if (checkLength(cardNum) === false) { + console.log('Number must be 16 digits!'); + } + if (checkSum(cardNum) === false) { + console.log('Sum less than 16!'); + } + if (allEqualCharacters(cardNum) === false) { + console.log('All numbers can not be the same!'); + } + if (onlyNumbers(cardNum) === false) { + console.log('Invalid characters!'); + } + if (isEndEven(cardNum) === false) { + console.log('Odd final number!'); + } + return; + } + + console.log(`"${cardNum}" is a valid credit card number.`); +} + +//checking the length of number if its equal to 16 +function checkLength(cardNum) { + let numberLength = cardNum.length; + if (numberLength == 16) { + return true; + } + return false; +} + +//checking the sum of the digits of the card number if its greater than 16 +function checkSum(cardNum) { + let sum = 0; + for (let i = 0; i < cardNum.length; i++) { + sum = sum + parseInt(cardNum[i]); + } + if (sum > 16) { + return true; + } + return false; +} + +/* checking the card number if it has at least 2 different numbers +.split() method and every() method takes each character of the string into an array +then the function checks all the characters if they match with the first one, returns true if all characters same*/ +function allEqualCharacters(cardNum) { + let characterCheck = cardNum.split('').every(char => char === cardNum[0]); + if (characterCheck == true) { + return false; + } + return true; +} + +// checking if the card number ends with an even number +function isEndEven(cardNum) { + let lastNumber = cardNum[cardNum.length - 1]; + if (lastNumber % 2 == 0) { + return true; + } + return false; +} + +/* checking if the card number is made of only numbers +^ does a match for the first character of the string + \d searches for the digits between 0-9*/ +function onlyNumbers(cardNum) { + let isNumOnly = /^\d+$/.test(cardNum); + if (isNumOnly == true) { + return true; + } + return false; +} + +// checking if the card number is valid according to the given requirements +function isValid(cardNum) { + switch (false) { + case checkLength(cardNum): + return false; + case checkSum(cardNum): + return false; + case allEqualCharacters(cardNum): + return false; + break; + case onlyNumbers(cardNum): + return false; + break; + case isEndEven(cardNum): + return false; + break; + } + return true; +} + + +main('9999777788880000'); //valid number example +main('6666666666661666'); //valid number example + +main('a92332119c011112'); //Invalid number example +main('4444444444444444'); //Invalid number example +main('1111111111111110'); //Invalid number example +main('6666666666666661'); //Invalid number example diff --git a/Week3/homework/js-exercises/dog-years.js b/Week3/homework/js-exercises/dog-years.js new file mode 100644 index 000000000..37c596bc8 --- /dev/null +++ b/Week3/homework/js-exercises/dog-years.js @@ -0,0 +1,10 @@ +'use strict'; + +function calculateDogAge(ageInHumanYear) { + let ageInDogYear = 7 * ageInHumanYear; + console.log(`Your doggie is ${ageInDogYear} years old in dog years!`); +} + +calculateDogAge(1); +calculateDogAge(3); +calculateDogAge(7); \ No newline at end of file diff --git a/Week3/homework/js-exercises/fortune-teller.js b/Week3/homework/js-exercises/fortune-teller.js new file mode 100644 index 000000000..fdbb2b174 --- /dev/null +++ b/Week3/homework/js-exercises/fortune-teller.js @@ -0,0 +1,17 @@ +'use strict'; + +const numChildren = [1, 2, 3, 4, 5, 6, 7]; +const partnerNames = ['Elise', 'Emma', 'Adam', 'John', 'Sophia', 'Amelia', 'Henry']; +const locations = ['Amsterdam', 'Paris', 'Den Hauge', 'Bangkok', 'Osaka', 'Chicago', 'Istanbul']; +const jobs = ['Teacher', 'Plumber', 'Software Developer', 'Constraction Worker', 'Doctor', 'Nurse', 'Lawyer']; + +//Math.random() produces a random number between 0-1. Math.floor rounds the number +function tellFortune(numChildren, partnerNames, locations, jobs) { + let job_title = jobs[Math.floor(Math.random() * jobs.length)]; + let location = locations[Math.floor(Math.random() * locations.length)]; + let wifeName = partnerNames[Math.floor(Math.random() * partnerNames.length)]; + let number_kids = numChildren[Math.floor(Math.random() * numChildren.length)]; + console.log(`You will be a ${job_title} in ${location}, and married to ${wifeName} with ${number_kids} kids.`) +} + +tellFortune(numChildren, partnerNames, locations, jobs); \ No newline at end of file diff --git a/Week3/homework/js-exercises/randomcompliment.js b/Week3/homework/js-exercises/randomcompliment.js new file mode 100644 index 000000000..279ab7d26 --- /dev/null +++ b/Week3/homework/js-exercises/randomcompliment.js @@ -0,0 +1,17 @@ +'use strict'; + +let complimentsArr = ['great', 'awesome', 'smart', 'ambitious', 'unique', 'generous', 'thoughtful', 'cheerful', 'supportive', 'open-minded']; + +/* to get random compliment ; Math.random() produces a random number between +0-1. Then multiply it with the length of the array, this will give a random +index in the array. Then it is passed as an index to array to select a +compliment from the array, randomly. */ +function giveCompliment(name) { + console.log(name.length); + let compliment = complimentsArr[Math.floor(Math.random() * complimentsArr.length)]; + console.log(`You are ${compliment}, ${name}`); +} + +giveCompliment("Mucahit"); +giveCompliment("Mucahit"); +giveCompliment("Mucahit"); \ No newline at end of file diff --git a/Week3/homework/js-exercises/shopping-cart.js b/Week3/homework/js-exercises/shopping-cart.js new file mode 100644 index 000000000..6fd2578a9 --- /dev/null +++ b/Week3/homework/js-exercises/shopping-cart.js @@ -0,0 +1,15 @@ +'use strict'; + +let itemsInTheCart = ['bananas', 'milk']; + +function addToShoppingCart(newItem) { + itemsInTheCart.push(newItem); + if (itemsInTheCart.length > 3) { + itemsInTheCart.shift(); + } + console.log(`You bought ${itemsInTheCart}!`); +} + +addToShoppingCart('kiwi'); +addToShoppingCart('honey'); +addToShoppingCart('bread'); \ No newline at end of file diff --git a/Week3/homework/js-exercises/total-cost.js b/Week3/homework/js-exercises/total-cost.js new file mode 100644 index 000000000..a1f5324ad --- /dev/null +++ b/Week3/homework/js-exercises/total-cost.js @@ -0,0 +1,22 @@ +'use strict'; + + + + +function calculateTotalPrice(obj) { + let totalCost = 0; + for (let key in obj) { + totalCost = totalCost + obj[key]; + } + console.log(`Total price of all items is ${totalCost.toFixed(2)} .`); +} + +let cartForParty = { + chips: 1.20, + cookies: 3.99, + chocolate: 2.75, + coke: 2.50, + sunFlowerSeeds: 4.00, +} + +calculateTotalPrice(cartForParty);