├── README.md └── main.js /README.md: -------------------------------------------------------------------------------- 1 | # Assignment-JS-4 2 | Assignment-JS-4 3 | 4 | ## Assignment Questions 5 | 6 | 1. Write a function that takes an array of numbers as input and returns the sum of all even numbers in the array. 7 | 8 | 2. Write a function that takes a string as input and returns the number of vowels in the string. 9 | 10 | 3. Write a function that takes an array of strings as input and returns the length of the longest string in the array. 11 | 12 | 4. Write a function that takes an array of numbers as input and returns the average of all the numbers in the array. 13 | 14 | 5. Write a function that takes an object as input and returns an array of all the keys in the object. 15 | 16 | 6. Write a function that takes an array of objects as input and returns an array of all the values for a specified key in each object. 17 | 18 | 7. Write a function that takes an array of strings as input and returns an array of all the unique strings in the array. 19 | 20 | 8. Write a function that takes an array of numbers as input and returns the product of all the numbers in the array. 21 | 22 | ## Example for Input and Output 23 | 24 | 1. Function to sum all even numbers in an array: 25 | Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 26 | Output: 30 27 | 28 | 2. Function to count the number of vowels in a string: 29 | Input: "hello world" 30 | Output: 3 31 | 32 | 3. Function to find the length of the longest string in an array: 33 | Input: ["apple", "banana", "pear", "grapefruit"] 34 | Output: 10 35 | 36 | 4. Function to find the average of all the numbers in an array: 37 | Input: [1, 2, 3, 4, 5] 38 | Output: 3 39 | 40 | 5. Function to return an array of all the keys in an object: 41 | Input: { name: "John", age: 30, city: "New York" } 42 | Output: ["name", "age", "city"] 43 | 44 | 6. Function to return an array of all the values for a specified key in each object: 45 | Input: [{ name: "John", age: 30 }, { name: "Mary", age: 25 }, { name: "Peter", age: 35 }] 46 | Output: ["John", "Mary", "Peter"] 47 | 48 | 7. Function to return an array of all the unique strings in an array: 49 | Input: ["apple", "banana", "apple", "pear", "pear", "orange"] 50 | Output: ["apple", "banana", "pear", "orange"] 51 | 52 | 8. Function to return the product of all the numbers in an array: 53 | Input: [1, 2, 3, 4, 5] 54 | Output: 120 55 | 56 | 57 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | 2 | console.log("-----------------------------Task-1-----------------------------"); 3 | //function that takes an array of numbers as input and returns the sum of all even numbers in the array. 4 | 5 | let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 6 | 7 | function sumEvens(array) { 8 | let sum = 0; 9 | for (let i = 0; i < array.length; i++) { 10 | if (array[i] % 2 === 0) { 11 | 12 | sum = sum + array[i]; 13 | } 14 | } 15 | return sum; 16 | } 17 | 18 | console.log("Sum of Even Numbers in the Array is:", sumEvens(array)); 19 | 20 | 21 | 22 | 23 | 24 | console.log("-----------------------------Task-2-----------------------------"); 25 | //function that takes a string as input and returns the number of vowels in the string. 26 | 27 | let str = "It was an honour meeting you"; 28 | const vowels = ["a", "e", "i", "o", "u"]; 29 | let Result = countVowels(str); 30 | function countVowels(str) { 31 | let count = 0; 32 | for (let letter of str.toLowerCase()) { 33 | if (vowels.includes(letter)) { 34 | count++; 35 | } 36 | } 37 | return count 38 | } 39 | console.log("Count of vowels in the String : ", Result); 40 | 41 | 42 | 43 | 44 | 45 | console.log("-----------------------------Task-3-----------------------------"); 46 | //function that takes an array of strings as input and returns the length of the longest string in the array. 47 | 48 | 49 | let arr = ["Apple", "Mango", "Orange", "Watermelon", "Guava"]; 50 | 51 | function getLongestStr(arr) { 52 | let longestStr = { 53 | length: 0, 54 | }; 55 | for (let i = 0; i < arr.length; i++) { 56 | if (arr[i].length > longestStr.length) { 57 | longestStr.length = arr[i].length; 58 | } 59 | } 60 | return longestStr; 61 | } 62 | let longestString = getLongestStr(arr); 63 | console.log("length of the longest string in an array : ", (longestString.length)); 64 | 65 | 66 | 67 | 68 | 69 | console.log("-----------------------------Task-4-----------------------------"); 70 | //function that takes an array of numbers as input and returns the average of all the numbers in the array. 71 | 72 | let Array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 73 | 74 | function calculateAverage(Array) { 75 | let total = 0; 76 | let count = 0; 77 | 78 | Array.forEach(function (item) { 79 | total += item; 80 | count++; 81 | }); 82 | 83 | return total / count; 84 | } 85 | console.log("The Average is : ", calculateAverage(Array)); 86 | 87 | 88 | 89 | console.log("-----------------------------Task-5-----------------------------"); 90 | // a function that takes an object as input and returns an array of all the keys in the object. 91 | 92 | const objects = { 93 | Name: "Sheikh", 94 | Age: 16, 95 | City: "USA", 96 | }; 97 | const keys = Object.keys(objects) 98 | 99 | console.log("Keys of the Object : ", keys); 100 | 101 | 102 | 103 | 104 | console.log("-----------------------------Task-6-----------------------------"); 105 | //function that takes an array of objects as input and returns an array of all the values for a specified key in each object. 106 | 107 | const object = [{ 108 | name: "John", 109 | age: 19, 110 | city: "New york" 111 | }, 112 | { 113 | name: "Maria", 114 | age: 18, 115 | city: "Amsterdam" 116 | }, 117 | { 118 | name: "Peter", 119 | age: 20, 120 | city: "Brazil" 121 | }, 122 | ]; 123 | const names = object.map((values) => values.name); 124 | console.log("Values for the specified Keys of each Object : ", names); 125 | 126 | 127 | 128 | 129 | console.log("-----------------------------Task-7-----------------------------"); 130 | //a function that takes an array of strings as input and returns an array of all the unique strings in the array. 131 | 132 | let arrObject = ["Apple", "Mango", "Apple", "Orange", "Watermelon", "Orange", "Guava"]; 133 | let newArrObject = arrObject.filter(unique); 134 | 135 | function unique(value, index, self) { 136 | return self.indexOf(value) === index 137 | } 138 | console.log("Unique Strings in the Array : ", newArrObject); 139 | 140 | 141 | 142 | 143 | 144 | console.log("-----------------------------Task-8-----------------------------"); 145 | // a function that takes an array of numbers as input and returns the product of all the numbers in the array. 146 | 147 | let newArray = [1, 2, 3, 4, 5, 6]; 148 | let product = 1, 149 | i; 150 | for (let i = 0; i < newArray.length; i++) { 151 | product *= newArray[i]; 152 | } 153 | console.log("The Product of Array is : ", (product)); 154 | 155 | 156 | 157 | --------------------------------------------------------------------------------