diff --git a/week-3/1-exercises/A-array-find/exercise.js b/week-3/1-exercises/A-array-find/exercise.js index d7fd51f..b45dca2 100644 --- a/week-3/1-exercises/A-array-find/exercise.js +++ b/week-3/1-exercises/A-array-find/exercise.js @@ -4,6 +4,9 @@ */ // write your code here +function findLongNameThatStartsWithA(arr){ + return arr.find(item => item.startsWith("A") && item.length > 7); +} var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"]; diff --git a/week-3/1-exercises/B-array-some/exercise.js b/week-3/1-exercises/B-array-some/exercise.js index 965c052..5213fbc 100644 --- a/week-3/1-exercises/B-array-some/exercise.js +++ b/week-3/1-exercises/B-array-some/exercise.js @@ -11,6 +11,10 @@ var pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; // If there is a null value in the array exit the program with the error code // https://nodejs.org/api/process.html#process_process_exit_code // process.exit(1); +if(pairsByIndex.some(item => item === null)){ + process.exit(1); +} + var students = ["Islam", "Lesley", "Harun", "Rukmini"]; var mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; diff --git a/week-3/1-exercises/C-array-every/exercise.js b/week-3/1-exercises/C-array-every/exercise.js index b515e94..bbb94f6 100644 --- a/week-3/1-exercises/C-array-every/exercise.js +++ b/week-3/1-exercises/C-array-every/exercise.js @@ -5,7 +5,7 @@ var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; var group = ["Austine", "Dany", "Swathi", "Daniel"]; -var groupIsOnlyStudents; // complete this statement +var groupIsOnlyStudents = group.every(item => students.find(student => student === item)); // complete this statement if (groupIsOnlyStudents) { console.log("The group contains only students"); diff --git a/week-3/1-exercises/D-array-filter/exercise.js b/week-3/1-exercises/D-array-filter/exercise.js index 6e32cc6..199598a 100644 --- a/week-3/1-exercises/D-array-filter/exercise.js +++ b/week-3/1-exercises/D-array-filter/exercise.js @@ -8,7 +8,7 @@ var pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -var pairsByIndex; // Complete this statement +var pairsByIndex = pairsByIndexRaw.filter(item => Array.isArray(item) && item.length === 2 ); // Complete this statement var students = ["Islam", "Lesley", "Harun", "Rukmini"]; var mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; diff --git a/week-3/1-exercises/E-array-map/exercise.js b/week-3/1-exercises/E-array-map/exercise.js index 2835e92..d0e49dc 100644 --- a/week-3/1-exercises/E-array-map/exercise.js +++ b/week-3/1-exercises/E-array-map/exercise.js @@ -3,3 +3,26 @@ var numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; +function multiplyByHundred(item){ + return item * 100; +} + +let numbers1 = numbers.map(multiplyByHundred); + +console.log(numbers1); + +let numbers2 = numbers.map(function multiplyBy100(item){return item * 100;}); + +console.log(numbers2); + +let numbers3 = numbers.map(function (item){return item * 100;}); + +console.log(numbers3); + +let numbers4 = numbers.map(item => {return item * 100;}); + +console.log(numbers4); + +let numbers5 = numbers.map(item => item * 100); + +console.log(numbers5); \ No newline at end of file diff --git a/week-3/1-exercises/F-array-forEach/exercise.js b/week-3/1-exercises/F-array-forEach/exercise.js index e83e2df..0921bc4 100644 --- a/week-3/1-exercises/F-array-forEach/exercise.js +++ b/week-3/1-exercises/F-array-forEach/exercise.js @@ -8,7 +8,16 @@ */ var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; - +arr.forEach((item) => { + if(item % 3 === 0 && item % 5 === 0){ + console.log("FizzBuzz"); + }else if (item % 3 === 0){ + console.log("Fizz"); + }else if(item % 5 === 0){ + console.log("Buzz"); + }else {console.log(item);} + } + ); /* EXPECTED OUTPUT */ /* diff --git a/week-3/1-exercises/G-array-methods/exercise.js b/week-3/1-exercises/G-array-methods/exercise.js index 44e9c80..6a8e4b3 100644 --- a/week-3/1-exercises/G-array-methods/exercise.js +++ b/week-3/1-exercises/G-array-methods/exercise.js @@ -4,7 +4,7 @@ */ var numbers = [3, 2, 1]; -var sortedNumbers; // complete this statement +var sortedNumbers = numbers.sort(); // complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/week-3/1-exercises/G-array-methods/exercise2.js b/week-3/1-exercises/G-array-methods/exercise2.js index 3dd24a1..dd87089 100644 --- a/week-3/1-exercises/G-array-methods/exercise2.js +++ b/week-3/1-exercises/G-array-methods/exercise2.js @@ -7,7 +7,7 @@ var mentors = ["Daniel", "Irina", "Rares"]; var students = ["Rukmini", "Abdul", "Austine", "Swathi"]; -var everyone; // complete this statement +var everyone = mentors.concat(students); // complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/week-3/1-exercises/H-array-methods-2/exercise.js b/week-3/1-exercises/H-array-methods-2/exercise.js index d36303b..3aa962b 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise.js +++ b/week-3/1-exercises/H-array-methods-2/exercise.js @@ -15,8 +15,8 @@ var everyone = [ "Swathi" ]; -var firstFive; // complete this statement -var lastFive; // complete this statement +var firstFive = everyone.slice(0, 5); // complete this statement +var lastFive = everyone.slice(-5); // complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/week-3/1-exercises/H-array-methods-2/exercise2.js b/week-3/1-exercises/H-array-methods-2/exercise2.js index b7be576..27e9e26 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise2.js +++ b/week-3/1-exercises/H-array-methods-2/exercise2.js @@ -7,7 +7,11 @@ Tip: use the string method .split() and the array method .join() */ -function capitalise(str) {} +function capitalise(str) { + let arr = str.split(""); + arr[0] = arr[0].toUpperCase(); + return arr.join(""); +} /* DO NOT EDIT BELOW THIS LINE diff --git a/week-3/1-exercises/H-array-methods-2/exercise3.js b/week-3/1-exercises/H-array-methods-2/exercise3.js index 82e9dd8..75a6b76 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise3.js +++ b/week-3/1-exercises/H-array-methods-2/exercise3.js @@ -7,7 +7,7 @@ var ukNations = ["Scotland", "Wales", "England", "Northern Ireland"]; function isInUK(country) { - return; // complete this statement + return ukNations.includes(country); // complete this statement } /* diff --git a/week-3/2-mandatory/1-oxygen-levels.js b/week-3/2-mandatory/1-oxygen-levels.js index 3c02135..eae5382 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -9,7 +9,8 @@ To be safe, they need to land on the first unamed planet that has Oxygen levels Write a function that finds the oxygen level of the first safe planet - Oxygen between 19.5% and 23.5% */ -function safeLevels() { +function safeLevels(arr) { + return arr.find(oxLevel => parseFloat(oxLevel) > 19.5 && parseFloat(oxLevel) < 23.5); } diff --git a/week-3/2-mandatory/2-bush-berries.js b/week-3/2-mandatory/2-bush-berries.js index d900323..223afd0 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -10,8 +10,11 @@ Use the tests to confirm which message to return */ -function bushChecker() { - +function bushChecker(arr) { + if(arr.some(berryColor => berryColor != "pink")){ + return "Toxic! Leave bush alone!"; + } + return "Bush is safe to eat from"; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/3-space-colonies.js b/week-3/2-mandatory/3-space-colonies.js index f99891a..b45f242 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -8,7 +8,8 @@ NOTE: don't include any element that is not a "family". */ -function colonisers() { +function colonisers(arr) { + return arr.filter(item => item.startsWith("A") && item.includes("family")); } diff --git a/week-3/2-mandatory/4-eligible-students.js b/week-3/2-mandatory/4-eligible-students.js index 6424b01..0e9031b 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,8 +7,8 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { - +function eligibleStudents(arr) { + return arr.filter(item => item[1] > 7).map(item => item[0]); } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/5-journey-planner.js b/week-3/2-mandatory/5-journey-planner.js index 53499c3..612175f 100644 --- a/week-3/2-mandatory/5-journey-planner.js +++ b/week-3/2-mandatory/5-journey-planner.js @@ -7,8 +7,8 @@ NOTE: only the names should be returned, not the means of transport. */ -function journeyPlanner() { - +function journeyPlanner(arr, by) { + return arr.filter(item => item.includes(by)).map(item => item[0]); } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/6-lane-names.js b/week-3/2-mandatory/6-lane-names.js index eddfe44..cce51a8 100644 --- a/week-3/2-mandatory/6-lane-names.js +++ b/week-3/2-mandatory/6-lane-names.js @@ -4,8 +4,8 @@ Write a function that will return all street names which contain 'Lane' in their name. */ -function getLanes() { - +function getLanes(arr) { + return arr.filter(item => item.includes("Lane")); } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/7-password-validator.js b/week-3/2-mandatory/7-password-validator.js index 57b3d53..63e5fe5 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -21,11 +21,30 @@ Expected Result: PasswordValidationResult= [false, false, false, false, true] */ - function validatePasswords(passwords) { + return passwords.map((password,index,arr) => { + let passwordArray = password.split(""); + + return password.length > 5 && // check the min length of password + // check if the password is duplicated + index === arr.indexOf(password) && + // check if the password have uppercase letter + passwordArray.some(item => item >= "A" && item <= "Z") && + // check if the password have lowercase letter + passwordArray.some(item => item >= "a" && item <= "z") && + // check if the password have number + passwordArray.some(item => item >= "0" && item <= "9") && + // check if the password have special symbols + passwordArray.some(item =>["!", "#", "$", "%", "."].includes(item)) && + /* this validation for exception of all symbols that are not English alphabet symbols, + not numbers or non-alphanumeric symbols ("!", "#", "$", "%", "."). */ + passwordArray.every(item =>(item >= "A" && item <= "Z") || (item >= "a" && item <= "z") || + (item >= "0" && item <= "9") || ["!", "#", "$", "%", "."].includes(item)); + }); } + /* ======= TESTS - DO NOT MODIFY ===== */ const passwords1 = ["Se%5", "TktE.TJTU", "384#HsHF", "dvyyeyy!5", "tryT3729"] diff --git a/week-3/2-mandatory/8-codewars.md b/week-3/2-mandatory/8-codewars.md index 8bd2b85..11a0e17 100644 --- a/week-3/2-mandatory/8-codewars.md +++ b/week-3/2-mandatory/8-codewars.md @@ -30,4 +30,6 @@ Today, you'll be using a platform called [CodeWars](https://codewars.com) to hel - [people in bus](https://www.codewars.com/kata/number-of-people-in-the-bus/train/javascript) - [sum without highest and lowest](https://www.codewars.com/kata/sum-without-highest-and-lowest-number/train/javascript) - [reveersed array of digits](https://www.codewars.com/kata/convert-number-to-reversed-array-of-digits/train/javascript) -- [slash sum of negatives](https://www.codewars.com/kata/count-of-positives-slash-sum-of-negatives/train/javascript) \ No newline at end of file +- [slash sum of negatives](https://www.codewars.com/kata/count-of-positives-slash-sum-of-negatives/train/javascript) + + diff --git a/week-3/3-extra/card-validator.js b/week-3/3-extra/card-validator.js new file mode 100644 index 0000000..24aa5f2 --- /dev/null +++ b/week-3/3-extra/card-validator.js @@ -0,0 +1,38 @@ +// - 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 + +function isBankCardValid(cardNumber){ + let numberArray = cardNumber.split(""); + + firstGroup = cardNumber.slice(0, 4); + secondGroup = cardNumber.slice(4, 8); + thirdGroup = cardNumber.slice(8, 12); + fourthGroup = cardNumber.slice(12); + if (numberArray.length !== 16){ + return `The bank card number ${firstGroup} ${secondGroup} ${thirdGroup} ${fourthGroup} is not valid.length`; + }// check if a length of card number equals 16 + if (numberArray.some(number => !(number >=0 && number <= 9))){ + return `The bank card number ${firstGroup} ${secondGroup} ${thirdGroup} ${fourthGroup} is not valid. There is non number characters.`; + }// check if all characters in card number are digit + if(numberArray.reduce((accumulator, number) => accumulator + parseInt(number),0) <= 16){ + return `The bank card number ${firstGroup} ${secondGroup} ${thirdGroup} ${fourthGroup} is not valid. The summ of numbers is lower than 16`; + }// check if a summ of all digits is greater or equals to 16 + if(numberArray[15] % 2 === 1){ + return `The bank card number ${firstGroup} ${secondGroup} ${thirdGroup} ${fourthGroup} is not valid. The last number is not even.`; + }// check if the last is an even digit + if(numberArray.every((number, index, arr) => number === numberArray[0])){ + return `The bank card number ${firstGroup} ${secondGroup} ${thirdGroup} ${fourthGroup} is not valid. All numbers the same.`; + }// check if all didgits are the same + return `The bank card number ${firstGroup} ${secondGroup} ${thirdGroup} ${fourthGroup} is valid.`; + +} +console.log(isBankCardValid("9999777788880000")); +console.log(isBankCardValid("6666666666661666")); +console.log(isBankCardValid("a92332119c011112")); +console.log(isBankCardValid("4444444444444444")); +console.log(isBankCardValid("1111111111111110")); +console.log(isBankCardValid("6666666666666661")); + +