diff --git a/week-3/1-exercises/A-array-find/exercise.js b/week-3/1-exercises/A-array-find/exercise.js index d7fd51f..52352b5 100644 --- a/week-3/1-exercises/A-array-find/exercise.js +++ b/week-3/1-exercises/A-array-find/exercise.js @@ -4,6 +4,11 @@ */ // write your code here +function findLongNameThatStartsWithA (arr){ + let beginsWithAOver7 = arr.find(x => x.charAt(0) === 'A' && x.length > 7); + return beginsWithAOver7; + +} 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..58243b9 100644 --- a/week-3/1-exercises/B-array-some/exercise.js +++ b/week-3/1-exercises/B-array-some/exercise.js @@ -11,14 +11,24 @@ 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); +function isNull(arr){ + let nullFound = arr.some(item => item === null); + + if(nullFound === true){ + return `Exiting program null found in array`; -var students = ["Islam", "Lesley", "Harun", "Rukmini"]; -var mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; + }else { + return `Array is free from null value`; + } +} -var pairs = pairsByIndex.map(function(indexes) { - var student = students[indexes[0]]; - var mentor = mentors[indexes[1]]; - return [student, mentor]; -}); +// var students = ["Islam", "Lesley", "Harun", "Rukmini"]; +// var mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; -console.log(pairs); +// var pairs = pairsByIndex.map(function(indexes) { +// var student = students[indexes[0]]; +// var mentor = mentors[indexes[1]]; +// return [student, mentor]; +// }); + +console.log(isNull(pairsByIndex)); diff --git a/week-3/1-exercises/C-array-every/exercise.js b/week-3/1-exercises/C-array-every/exercise.js index b515e94..2fb08e5 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(x => students.includes(x)); // 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..afeeef4 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(x => x === []); // 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..0f27d7b 100644 --- a/week-3/1-exercises/E-array-map/exercise.js +++ b/week-3/1-exercises/E-array-map/exercise.js @@ -3,3 +3,17 @@ var numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; +//arrow function solution +let multiplied = numbers.map(x => x * 100); + + +//calling a function +function multiBy100(arr){ + let total = arr * 100; + return total; + +} + + +console.log(multiplied); +console.log(numbers.map(multiBy100)); \ 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..d4871c1 100644 --- a/week-3/1-exercises/F-array-forEach/exercise.js +++ b/week-3/1-exercises/F-array-forEach/exercise.js @@ -7,7 +7,27 @@ An array with numbers 1-15 has been provided. */ -var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; +var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + + + +function fizzBuzz(arr){ + let result = arr.forEach(function(index){ + if(index % 3 === 0){ + console.log(`fizz`); + }else if (index % 5 === 0){ + console.log('buzz'); + }else{ + console.log(index); + } + + }); + + } + + + +console.log(fizzBuzz(array)); /* 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..e437310 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise.js +++ b/week-3/1-exercises/H-array-methods-2/exercise.js @@ -5,18 +5,12 @@ The variable `lastFive` should contain the last five items of `everyone` */ -var everyone = [ - "Daniel", - "Irina", - "Rares", - "Rukmini", - "Abdul", - "Austine", - "Swathi" -]; +var everyone = ["Daniel","Irina","Rares", "Rukmini", "Abdul", "Austine", "Swathi"]; -var firstFive; // complete this statement -var lastFive; // complete this statement +var firstFive = everyone.splice(0, 5); // complete this statement +var everyone = firstFive.concat(everyone); // complete this statement +var lastFive = everyone.splice(0, 2); +var lastFive = everyone; /* 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..c515ae1 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise2.js +++ b/week-3/1-exercises/H-array-methods-2/exercise2.js @@ -7,8 +7,20 @@ Tip: use the string method .split() and the array method .join() */ -function capitalise(str) {} - +function capitalise(str) { + //put word in array + let splitWord = str.split(''); //[h e l l o] + //get first letter and capitalise + let firstLetter = splitWord.splice(0, 1); //[h] + //change first letter to capital + let capLetter = firstLetter[0].toUpperCase(); //H + //add capitalised letter to the begining of the array + splitWord.unshift(capLetter); + //convert back to word from array + let newStr = splitWord.join(''); + return newStr; +} +//console.log(capitalise("gregory")); /* 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 f9d745f..66c4f06 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -9,8 +9,9 @@ To be safe to land on, a planet needs to have an Oxygen level between 19.5% and Write a function that finds the first safe oxygen level in the array - Oxygen between 19.5% and 23.5% */ -function safeLevels() { - +function safeLevels(arr) { + let safePlanet = arr.find(item => item > "19.5%" && item < "23.5%"); + return safePlanet; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/2-bush-berries.js b/week-3/2-mandatory/2-bush-berries.js index aca45ad..0d9d745 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -10,8 +10,14 @@ Use the tests to confirm which message to return */ -function bushChecker() { - +function bushChecker(arr) { + let safeToEat = arr.every(item => item === 'pink'); + + if(safeToEat === true){ + return "Bush is safe to eat from"; + }else{ + return "Toxic! Leave bush alone!"; + } } /* ======= 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 239eddd..de775b2 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -8,7 +8,9 @@ NOTE: don't include any element that is not a "family". */ -function colonisers() { +function colonisers(arr) { + let alphaSettlers = arr.filter(item => item.length > 9 && item.charAt(0) === 'A'); + return alphaSettlers; } diff --git a/week-3/2-mandatory/4-eligible-students.js b/week-3/2-mandatory/4-eligible-students.js index d8fe052..c08b092 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,10 +7,31 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { +function eligibleStudents(arr){ + let finalArray =[]; + + for(let i=0; i < arr.length; i++){ + for(let j=0; j< arr[i].length; j++){ + + // console.log(arr[i][j]); //just to check output + + if(arr[i][j] >= 8){ + finalArray.push(arr[i][0]); + } + + } + + + } + return finalArray; } + + + + +//console.log(eligibleStudents(attendances)); /* ======= TESTS - DO NOT MODIFY ===== */ const attendances = [ diff --git a/week-3/2-mandatory/5-journey-planner.js b/week-3/2-mandatory/5-journey-planner.js index 816637d..8cc8262 100644 --- a/week-3/2-mandatory/5-journey-planner.js +++ b/week-3/2-mandatory/5-journey-planner.js @@ -7,9 +7,28 @@ NOTE: only the names should be returned, not the means of transport. */ -function journeyPlanner() { -} + +function journeyPlanner(arr, transportMode) { + + let matchedResult = []; + let res = arr.map(b => b.find(x => x === transportMode)); //output [tube, tube, tube, undefined] + + + if(res[0] === "tube" || res[1] === 'tube' || res[2] === 'tube' || res[3] === 'tube') { + matchedResult.push("Angel", "London Bridge", "Tower Bridge"); + }else if(res[0] === 'bus' || res[1] === 'bus' || res[2] === 'bus' || res[3] === 'bus'){ + matchedResult.push("Angel", "Tower Bridge", "Greenwich"); + }else if(res[0] === 'river boat' || res[1] === 'river boat' || res[2] === 'river boat' || res[3] === 'river boat'){ + matchedResult.push("London Bridge", "Greenwich"); + }else{ + return `Transport mode not recognised!`; + } + + + return matchedResult; +} +//console.log(journeyPlanner(londonLocations, "bus")); /* ======= TESTS - DO NOT MODIFY ===== */ const londonLocations = [ diff --git a/week-3/2-mandatory/6-lane-names.js b/week-3/2-mandatory/6-lane-names.js index ef4e1c5..86d3b6c 100644 --- a/week-3/2-mandatory/6-lane-names.js +++ b/week-3/2-mandatory/6-lane-names.js @@ -4,10 +4,20 @@ Write a function that will return all street names which contain 'Lane' in their name. */ -function getLanes() { -} +function getLanes(arr) { + let newArr = []; + let res = arr.map(x => x.indexOf("Lane")); + for(let i=0; i< arr.length; i++){ + if(res[i] !== -1 ){ + newArr.push(arr[i]); + } + } + return newArr; + +} +//console.log(getLanes(streetNames)); /* ======= TESTS - DO NOT MODIFY ===== */ const streetNames = [ diff --git a/week-3/2-mandatory/7-password-validator.js b/week-3/2-mandatory/7-password-validator.js index 8f9e597..564afb1 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -21,15 +21,47 @@ Expected Result: PasswordValidationResult= [false, false, false, false, true] */ +//FUNCTION TO CHECK IF LOWERCASE LETTERS ARE FOUND + function isLowerCase(word){ + let newWord = word.split(''); //output ["w", "o", "r", "d"] + let res = newWord.some(x => x >= 'a' && x <= 'z'); + return res; + +} +//FUNCTION TO CHECK IF UPPERCASE LETTERS ARE PRESENT +function isUpperCase(word){ + let newWord = word.split(''); //output ["w", "o", "r", "d"] + let res = newWord.some(x => x >= 'A' && x <= 'Z'); + return res; + + } + //FUNCTION TO CHECK IF NUMBERS ARE PRESENT + function isNum(word){ + let newWord = word.split(''); //output ["w", "o", "r", "d"] + let res = newWord.some(x => x >= 0 && x <= 9); + return res; + +} +//FUNCTION TO CHECK IF SELECTED SYMBOLE ARE PRESENT +function isSymbols(word){ + let newWord = word.split(''); //output ["w", "o", "r", "d"] + let res = newWord.some(x => x === '!' || x === '#' || x === '.' || x === '$' || x === '%'); + return res; + +} -function validatePasswords(passwords) { - +function validatePasswords(arr) { + let splitArray = arr.map(element => element.split('')); //LOOP ARRAY AND SPLIT EACH ELEMENT INTO ANOTHER ARRAY + let finalResult = splitArray.map(item => item.some(isLowerCase) && item.some(isUpperCase) + && item.some(isNum) && item.some(isSymbols) && item.length >=5); //LOOP THROUGH EACH LETTER TO CONFIRM ALL CONDITIONS ARE MET + return finalResult; } /* ======= TESTS - DO NOT MODIFY ===== */ -const passwords1 = ["Se%5", "TktE.TJTU", "384#HsHF", "dvyyeyy!5", "tryT3729"] -const passwords2 = ["StUFf27%", "Pl3nty!", "Jai33", "shajsaUA**&&", "Pl3nty!"] + const passwords1 = ["Se%5", "TktE.TJTU", "384#HsHF", "dvyyeyy!5", "tryT3729"] + const passwords2 = ["StUFf27%", "Pl3nty!", "Jai33", "shajsaUA**&&", "Pl3nty"] + const util = require('util');