Get the last character of string inside array

Viewed 58

The question required me to get the first char from the first word in array with the last char from the second word in an array I can just get the first char but my problem is with the second one like:

var array1 = ["hello", "sam", "aola"];
var result = [];
  const nextString =array1[1][array1[1].length - 1];  
  array1.map(function(currentString){
  result.push(currentString.charAt(0)+nextString)

});

 console.log(result);

here is my code give me this result: [ 'hm', 'sm', 'am' ]
but it expect to get this result :[ 'hm', 'sa', 'ao' ]

So please can anyone help me?

1 Answers

const array = ["hello", "sam", "aola"];

const result = array.map( (element, index, array) => {
  return element.charAt(0) + array[(index + 1) % array.length].slice(-1)
 } );

console.log(result)

Related