javascript - Use map function to decode a string with an array of numbers -


i trying write decode function arguments string of letters (in case alphabet) , array of numbers. want use .map method on array return coded message string. numbers positive integers.

the function, given array passed in, should return string "beans".

i cannot figure out logic. here have:

function decoder(key, code) {   var arr = key.split('');   var result = "";   arr.map(function(item) {     for(var = 0; <= code.length; i++) {         if(item[i] === code[i]) {         result += item;       }     }     return result;   });  }  console.log(decoder("abcdefghijklmnopqrstuvwxyz", [1, 4, 0, 13, 18 ])); 

a perfect match array.prototype.reduce():

function decoder(arr, indices){    return indices.reduce(function(previousvalue, currentvalue){      return previousvalue + arr[currentvalue];    }, "");  }    alert(decoder("abcdefghijklmnopqrstuvwxyz", [1, 4, 0, 13, 18 ]));


Comments