# program Q: Write a javascript function named reverse which takes a string argument and returns the reversed string. Ans function rev(str){ var arr = str.split(""); var result = arr.reverse().join(""); console.log(result); } rev("push"); Q: Write a javascript function named length_of_array, which takes an array as argument and returns the number of elements in that array (as opposed to what is given by the length property of the array). Remember undefined can also be an element if it is explicitly set at some index, e.g. x[5] = undefined;. This undefined should be counted, but elements which are not present in the array (as arrays can be sparse), should not be counted. Ans: function length_of_array(arr){ var result = 0; var arrlength = arr.length; for(var i=0; i<arr.length; i++){ if (arr[i] !== undefined) { result = result +1; } } console.log(result); } length_of_array([undefined, undefined, 1,2, 3, 5, undefined]); Q: Given a javascript array of objects having a radius property as shown [{radius: 5}, {radius: 9}, {radius: 2}], write a comparator function to sort it. Ans: Q: In Javascript, we can hold a function in a variable. E.g. var x = function() {...} Ans: true Q: Which of the following functions is NOT a higher order function? a).function abc(x, y) {var x = function() {return true;}; return x;} b. function abc(x, y) {for (var i=0; i < y; i++) {if x(i) {return true;} else { return false;}}} C. function abc() {return Math.sqrt;} d. function abc(x) {return Math.sqrt(x);} Q: Consider the following code: function add(to) { return function(x) { return x + to; } } What does add(2)("xyz") return? Ans: "xyz2"; Q: The function function sum() { return arguments.reduce(function(prev, curr) {return prev + curr;}, 0); } The call sum(2, 4, 6) Ans: gives an error during execution. Q: Write a function that takes an array of numbers and returns the sum of squares of those numbers. E.g. if the array passed is [1, 2, 3] then the function should return 14. Q: Write a function that takes an array of numbers as an argument and filters and returns the even numbers in them. Ans: var arr=[1,2,3,4,5,6,7,8]; arr.filter(function(ele){ if(ele % 2 === 0){ return ele; } });