From f00e8de8aded7bc80bea53a545a34cbc1fdae8eb Mon Sep 17 00:00:00 2001 From: Mucahit Date: Sat, 8 Feb 2020 13:36:55 +0100 Subject: [PATCH 1/3] added week 3 homework. --- .DS_Store | Bin 6148 -> 6148 bytes Week3/homework/creditcardvalidator.js | 105 ++++++++++++++++++ Week3/homework/js-exercises/dog-years.js | 10 ++ Week3/homework/js-exercises/fortune-teller.js | 17 +++ .../homework/js-exercises/randomcompliment.js | 17 +++ Week3/homework/js-exercises/shopping-cart.js | 15 +++ Week3/homework/js-exercises/total-cost.js | 23 ++++ 7 files changed, 187 insertions(+) create mode 100644 Week3/homework/creditcardvalidator.js create mode 100644 Week3/homework/js-exercises/dog-years.js create mode 100644 Week3/homework/js-exercises/fortune-teller.js create mode 100644 Week3/homework/js-exercises/randomcompliment.js create mode 100644 Week3/homework/js-exercises/shopping-cart.js create mode 100644 Week3/homework/js-exercises/total-cost.js diff --git a/.DS_Store b/.DS_Store index d06ffd2eacce6d51581b14d992d983569932683d..93fc62522a039f16d2d0eb7cd7904d5849160b52 100644 GIT binary patch delta 559 zcmZoMXfc=|#>B!ku~2NHo}wTZ0|Nsi1A_nqLkUB%XHI_d#<;bNn**5tG1h~m*ccKS zih(c{CY_X%p9B;Ks%NkUVvYY`0Aw*RNHSyqRp&9JFz7O50%=_!Kc68NsJ9$Q7cpc5 zMba5cfpRH8F+HG63dlkv6IUXe7z5;RG6XTWGB`50F!%y#ELs@Fk+lHXtPJ5m!&8Bl z8KN4_*ov%jGY^vvV?9E#5t`!9$cj;nHAYnl4uu&=p@1;cIH|n20P4GwNd-BX#U%y? z*BP0ZSy_VuLgC%Y#c2OG=BK5{sfiynw`#jHJZL*-V~s92}e+obdt@ z)z!MDraB6SW@fcI3f1Q32098R#%8s(oE+k+hPIvwxs_GbHMMm!fi49CMqq@30Y8+6 rQL}&yB==3$WZt})or9kPm>f1cGJj{D%rD}|0o2R{(y%!~WDPR_7LJFJ delta 83 zcmZoMXfc=|#>B)qu~2NHo+3XJ0|Nsi1A_nqgJDv6alyufwTzPuL|8VzWYJ{YEW#no kGO=OLW_AvK4xpCJf*jwOC-aLqasV}fbg^uX5Lv?v055M6xBvhE diff --git a/Week3/homework/creditcardvalidator.js b/Week3/homework/creditcardvalidator.js new file mode 100644 index 000000000..c1697cf26 --- /dev/null +++ b/Week3/homework/creditcardvalidator.js @@ -0,0 +1,105 @@ +'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) { + // let errors = []; + 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 = parseInt(sum) + parseInt(cardNum[i]); + } + if (sum > 16) { + return true; + } + return false; +} + +// checking the card number if it has at least 2 different numbers +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 +function onlyNumbers(cardNum) { + let isNumOnly = /^\d+$/.test(cardNum); //this method is to check if the items of the variable is only made of numbers if so it returns 'true'. + 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('1000000000006'); +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 \ No newline at end of file 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..037f7bb32 --- /dev/null +++ b/Week3/homework/js-exercises/total-cost.js @@ -0,0 +1,23 @@ +'use strict'; + + + + +function calculateTotalPrice(obj) { + let totalCost = 0; + for (let key in obj) { + totalCost = totalCost + obj[key]; + 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); \ No newline at end of file From d0d20c3a89b54369973195aa1edad973aa21cbd4 Mon Sep 17 00:00:00 2001 From: mucahit04 <57506115+mucahit04@users.noreply.github.com> Date: Thu, 13 Feb 2020 01:10:59 +0100 Subject: [PATCH 2/3] made improvements according to Unmes's advice. removed unnecessary parsInt, better explanation added as comments. --- Week3/homework/creditcardvalidator.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Week3/homework/creditcardvalidator.js b/Week3/homework/creditcardvalidator.js index c1697cf26..a0bceec98 100644 --- a/Week3/homework/creditcardvalidator.js +++ b/Week3/homework/creditcardvalidator.js @@ -2,7 +2,6 @@ /*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) { - // let errors = []; if (isValid(cardNum) === false) { console.log(`(${cardNum})`) if (checkLength(cardNum) === false) { @@ -39,7 +38,7 @@ function checkLength(cardNum) { function checkSum(cardNum) { let sum = 0; for (let i = 0; i < cardNum.length; i++) { - sum = parseInt(sum) + parseInt(cardNum[i]); + sum = sum + parseInt(cardNum[i]); } if (sum > 16) { return true; @@ -47,7 +46,9 @@ function checkSum(cardNum) { return false; } -// checking the card number if it has at least 2 different numbers +/* 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) { @@ -65,9 +66,11 @@ function isEndEven(cardNum) { return false; } -// checking if the card number is made of only numbers +/* 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); //this method is to check if the items of the variable is only made of numbers if so it returns 'true'. + let isNumOnly = /^\d+$/.test(cardNum); if (isNumOnly == true) { return true; } @@ -95,11 +98,10 @@ function isValid(cardNum) { } -//main('1000000000006'); 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 \ No newline at end of file +main('6666666666666661'); //Invalid number example From 4f61688d65087abf3c0d96110978366f8439aaac Mon Sep 17 00:00:00 2001 From: mucahit04 <57506115+mucahit04@users.noreply.github.com> Date: Thu, 13 Feb 2020 01:14:38 +0100 Subject: [PATCH 3/3] removed unnecessary key++ --- Week3/homework/js-exercises/total-cost.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Week3/homework/js-exercises/total-cost.js b/Week3/homework/js-exercises/total-cost.js index 037f7bb32..a1f5324ad 100644 --- a/Week3/homework/js-exercises/total-cost.js +++ b/Week3/homework/js-exercises/total-cost.js @@ -7,7 +7,6 @@ function calculateTotalPrice(obj) { let totalCost = 0; for (let key in obj) { totalCost = totalCost + obj[key]; - key++; } console.log(`Total price of all items is ${totalCost.toFixed(2)} .`); } @@ -20,4 +19,4 @@ let cartForParty = { sunFlowerSeeds: 4.00, } -calculateTotalPrice(cartForParty); \ No newline at end of file +calculateTotalPrice(cartForParty);