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

Write a java program creating an array of the numbers 1 through 10. Shuffle those numbers....
Write a java program creating an array of the numbers 1 through 10. Shuffle those numbers. Randomly pick a number between one and ten and then sequentially search the array for that number. Once the number is found, place that number at the top of the list. Print the list. Perform #3 thru #5 ten times.
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...
JavaScript Write out a function that takes in array of numbers. You do not need to...
JavaScript Write out a function that takes in array of numbers. You do not need to check if each item of the array is a number, assume this has already been done. Create a new array using the map function that that the original item and adds 4 to it. Then iterate through the new array and log each item to the console. If you do not use map it will be counted wrong. Call your function with the following...
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...
JAVASCRIPT: Please create an array of student names and another array of student grades. - Create...
JAVASCRIPT: Please create an array of student names and another array of student grades. - Create a function that can put a name and a grade to the arrays. - Keep Read student name and grade until student name is “???”. And save the reading by using a function - Create another function to show all the grade in that object. - Create the third function that can display the maximum grade and the student’s name. - Create a sorting...
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">...
<HTML JAVASCRIPT> Please create an array of student names and another array of student grades. Create...
<HTML JAVASCRIPT> Please create an array of student names and another array of student grades. Create a function that can put a name and a grade to the arrays. Keep Read student name and grade until student name is “???”. And save the reading by using a function Create another function to show all the grade in that object. Create the third function that can display the maximum grade and the student’s name. Create a sorting function that can sort...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT