JavaScript - Js Reduce
I need to implement a function called map (it needs to be written using reduce) in the below code that takes an array (arr) and a function (iter) as its arguments and returns an array with all its items transformed by iter, e.g. map([1, 2, 3, 4, 5], function(x) {return x * 2;}) should return [2, 4, 6, 8, 10]
PHP Code: (function(){ var reduce = function(arr, iter, acc){ var len = arr.length, i = 0; for (i = 0; i < len; i += 1) acc = iter(acc, arr[i], i); return acc; }; var map = function(arr, iter){ // Implementation code should go here // // }; return map([1, 2, 3, 4, 5], function(x){return x * 2;}); })(); Can anyone please help me? Thanks Similar TutorialsTrying to check validation on some dropdowns with this but man it's ugly. Is there a way to make it cleaner? Code: function checkDrop() { var sMorn = document.getElementById('wcsave_10_msession'); var sAft = document.getElementById('wcsave_10_asession'); var mMast = document.getElementById('wcsave_10_session_mmastery'); var aMast = document.getElementById('wcsave_10_session_amastery'); var mConc1 = document.getElementById('wcsave_10_session_mconc1'); var mConc2 = document.getElementById('wcsave_10_session_mconc2'); var aConc1 = document.getElementById('wcsave_10_session_aconc1'); var aConc2 = document.getElementById('wcsave_10_session_aconc2'); if(sMorn.options[sMorn.selectedIndex].value == 'Mastery session 1 - Computer Lab: Searching Essentials') { if ((sAft.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==0) && (aConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==1) && (aConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==0) && (aConc2.selectedIndex ==1)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } } if(sAft.options[sAft.selectedIndex].value == 'Mastery session 2 - Beyond the Basics of Searching!') { if ((sMorn.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==0) && (mConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==1) && (mConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==0) && (mConc2.selectedIndex ==1)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } } if(sMorn.options[sMorn.selectedIndex].value == 'Concurrent sessions 1-6') { if ((sAft.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==0) && (aConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==1) && (aConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==0) && (aConc2.selectedIndex ==1)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } } if(sAft.options[sAft.selectedIndex].value == 'Concurrent sessions 7-12') { if ((sMorn.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==0) && (mConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==1) && (mConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==0) && (mConc2.selectedIndex ==1)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } } return true; } |