└── assignment5.js /assignment5.js: -------------------------------------------------------------------------------- 1 | // 1 Write a function that takes two parameters, an array of numbers and a target number. The function should return a pair of numbers from the array that adds up to the target number. If no such pair is found, the function should return null. 2 | const array = [2, 7, 11, 15]; 3 | const target = 13; 4 | const pair = findPair(array, target); 5 | 6 | 7 | function findPair(array, target) { 8 | for (let i = 0; i < array.length; i++) { 9 | for (let j = i + 1; j < array.length; j++) { 10 | if (array[i] + array[j] === target) { 11 | return [array[i], array[j]]; 12 | } 13 | } 14 | } 15 | 16 | return null; 17 | } 18 | 19 | console.log("1 Answer"); 20 | console.log(pair); 21 | 22 | //2 Write a function that takes a string as input and returns the reverse of the string. 23 | 24 | function reverseString(str) { 25 | return str.split("").reverse().join(" "); 26 | } 27 | 28 | console.log("2 Answer"); 29 | console.log(reverseString("bano qabil")); 30 | 31 | 32 | //3 Write a function that takes an array of numbers as input and returns the sum of the two largest numbers in the array. 33 | 34 | const number = [1, 2, 35, 4, 15, 6, 7, 10] 35 | const sum = sumoftwo(number); 36 | 37 | 38 | function sumoftwo(ppl) { 39 | ppl.sort(function(a, b) { 40 | return b - a; 41 | }); 42 | return ppl[0] + ppl[1]; 43 | 44 | } 45 | 46 | console.log("3 Answer"); 47 | console.log(sum) 48 | 49 | //end 50 | 51 | //4 Write a function that takes an array of strings as input and returns a new array that contains only the strings that have more than three characters. 52 | 53 | const strings = ["muzammil", "haseeb", "ali", "at"]; 54 | const filteredStrings = filterStrings(strings); 55 | 56 | function filterStrings(arr) { 57 | let filteredArr = []; 58 | for (let i = 0; i < arr.length; i++) { 59 | if (arr[i].length > 3) { 60 | filteredArr.push(arr[i]); 61 | } 62 | } 63 | return filteredArr; 64 | } 65 | 66 | console.log("4 Answer"); 67 | console.log(filteredStrings); 68 | 69 | //end 70 | 71 | //5 Write a function that takes an array of numbers as input and returns a new array that contains only the even numbers. 72 | 73 | const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 74 | const evenNumbers = EvenNumbers(numbers); 75 | 76 | function EvenNumbers(arr) { 77 | let newarr = []; 78 | for (let i = 0; i < arr.length; i++) { 79 | if (arr[i] % 2 === 0) { 80 | newarr.push(arr[i]); 81 | } 82 | } 83 | return newarr; 84 | } 85 | console.log("5 Answer"); 86 | console.log(evenNumbers); 87 | //7 Write a function that takes a string as input and returns true if the string is a palindrome (reads the same backwards as forwards), and false otherwise. 88 | 89 | const str1 = "racecar"; 90 | const str2 = "hello"; 91 | 92 | function pal(str) { 93 | for (let i = 0; i < str.length / 2; i++) { 94 | if (str[i] !== str[str.length - i - 1]) { 95 | return false; 96 | } 97 | } 98 | 99 | return true; 100 | } 101 | 102 | console.log("7 Answer"); 103 | console.log(pal(str1)); // Output: true 104 | console.log(pal(str2)); // Output: false 105 | 106 | //9 Write a function that takes an array of strings as input and returns a new array that contains the length of each string in the original array. 107 | var arr1 = ["helloworld", "hey"] 108 | 109 | function abc(arr) { 110 | let newarray = [] 111 | for (let i = 0; i < arr.length; i++) { 112 | const l = arr1[i].length; 113 | newarray.push(l); 114 | } 115 | return newarray; 116 | } 117 | 118 | console.log("9 Answer"); 119 | console.log(abc(arr1)) 120 | 121 | //end 122 | 123 | //10 Write a function that takes an array of numbers as input and returns the index of the first number in the array that is greater than or equal to 10. 124 | 125 | const numberS = [2, 7, 12, 5, 9, 11]; 126 | const index = hello(numberS); 127 | 128 | function hello(arr) { 129 | for (let i = 0; i < arr.length; i++) { 130 | if (arr[i] >= 10) { 131 | return i; 132 | } 133 | } 134 | return -1; 135 | } 136 | 137 | console.log("10 Answer"); 138 | console.log(index); 139 | 140 | --------------------------------------------------------------------------------