Question

In: Computer Science

Please use javascript. Starting with an array containing the numbers 1 through 10, use filter, map...

Please use javascript.

Starting with an array containing the numbers 1 through 10, use filter, map and reduce to produce the following. Use console.log to display the results.

a. An array of odd numbers

b. An array of numbers divisible by 2 or 5

c. An array of numbers divisible by 3 squared

d. The sum of the following: square the numbers divisible by 5

Solutions

Expert Solution

let arr = [1,2,3,4,5,6,7,8,9,10];
let odd_arr = arr.filter(num => num % 2 != 0);  // filter out elements not divisible by 2 == that are odd
let div_by_2or5 = arr.filter(num => (num % 2 == 0) || (num % 5 == 0));  // filter out number divisible by 2 or 5
let div_by_3_squared = arr.filter(num => num % 3 == 0).map(num => num * num);   // filter out numbers divisible by 3, and then square them
let sum_sqrs_num_div_by_5 = arr.filter(num => num % 5 == 0).map(num => num * num).reduce((a, b) => a+b);    // filter out numbers divisible by 5, then square them, then add them

console.log('Original Array: ', arr);
console.log('Odd numbers: ', odd_arr);
console.log('Numbers divisible by 2 or 5: ', div_by_2or5);
console.log('square of Numbers divisible by 3:', div_by_3_squared);
console.log('Sum of squares of numbers divisible by 5: ', sum_sqrs_num_div_by_5);

Output:


Related Solutions

JavaScript (HTML) Task: Please read 10 numbers that are list below, sort the numbers and then...
JavaScript (HTML) Task: Please read 10 numbers that are list below, sort the numbers and then print those numbers. 10 numbers = { 9, 3, 2, 1, 10, 30, 4, 6, 7, 8} [Reference JavaScript code] <html> <body>     <H1>prompt()</h1>     <p id="pro"></p>     <script>        // Array creation        var num= new Array();               var ix = 0;        // Read 10 numbers        for (ix = 0; ix < 10; ix++)        {num[ix] = prompt("Enter a number");...
Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the...
Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the second highest number from the array. If the highest value occurs more than once, then it is also the second highest (see example). Assume the input array will always have length at least two (so there is always a second highest value). The original array must NOT be modified by this function. Example secondHighest([5,3,8,6,2,7,4]) must return 7, and secondHighest([5,3,8,6,2,7,8]) must return 8. I have...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Use of any built in string functions or built in array functions is not allowed, Any help would be much appreciated
Please take the code below and add some javascript to it. Please use javascript inline. Please...
Please take the code below and add some javascript to it. Please use javascript inline. Please be sure to specify within the webpage with something like 'Click here' too show what was done and how to activate it. Please post in a format that can be directly copied. Thank you in advance HINT: add some fun widgets to the page index-css.html: <!DOCTYPE html> <html lang="en"> <head> <!-- title for web page --> <title>Index-CSS Page</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1">...
convert this code to Python and make sure you use chained map and filter as well....
convert this code to Python and make sure you use chained map and filter as well. https://book.pythontips.com/en/latest/map_filter.html CODE BELOW IS IN JAVASCRIPT let people = [ {name: "Amy", pounds_weight: 152, inches_height: 63}, {name: "Joe", pounds_weight: 120, inches_height: 64}, {name: "Tom", pounds_weight: 210, inches_height: 78}, {name: "Jim", pounds_weight: 180, inches_height: 68}, {name: "Jen", pounds_weight: 120, inches_height: 62}, {name: "Ann", pounds_weight: 252, inches_height: 63}, {name: "Ben", pounds_weight: 240, inches_height: 72}, ]; //functions to convert pounds to kg and inches to meters let...
In C an array Numbers is declared float Numbers[10][10]. Due to a bug, the program tried...
In C an array Numbers is declared float Numbers[10][10]. Due to a bug, the program tried to reference Numbers[11][-13]. Which element of Numbers will actually be accessed?
Mini JAVASCRIPT Algorithm Exercise "Sum All In Array" Objectives Create a function that adds all numbers...
Mini JAVASCRIPT Algorithm Exercise "Sum All In Array" Objectives Create a function that adds all numbers of a provided array (arr), accounting for non-integer values in arr. The output should return an integer. Notes Remember your data types? If the element is an integer (8), it should be added. If the element is a string with a number value ("8"), it should be added. If the element is not a number, or if it is a string with a non-number...
int[]array={90, 57, 34, 81, 59, 51, 99, 52, 78, 67}; use for numbers 1 through 4...
int[]array={90, 57, 34, 81, 59, 51, 99, 52, 78, 67}; use for numbers 1 through 4 Write code to create two int arrays called odd and even of length 10. Populate the arrays with even numbers in the even array and odd numbers in the odd array as you go through the int[]array [ reminder 7%2 is 1, 8%2 is 0. The modulus operator yields the remainder]. Do not worry about the 0’s that are not filled. Write code to...
It is desired to use a rotary vacuum filter to separate slurry containing 20 kg of...
It is desired to use a rotary vacuum filter to separate slurry containing 20 kg of water per kilogram of solid material. Tests on the rotary filter at the conditions to be used for the filtration have indicated that the dimensionless ratio α/β' is 0.6 and 19 kg of filtrate (not including wash water) is obtained for each 27.8 kg of slurry. The temperature and pressure of the surroundings are 25 oC and 1 atm, respectively. The pressure drop to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT