-3

Let's say I have this array :

fruits: string[] = ['banana', 'strawberry', 'kiwi', 'apple'];

How can I do to have :

fruits = ['ananab', 'yrrebwarts', 'iwki', 'elppa'];
Eudz
  • 540
  • 6
  • 19

3 Answers3

2

You can do it like this:

const fruits = ['banana', 'strawberry', 'kiwi', 'apple'].map(item => item.split('').reverse().join(''))
Dan Cantir
  • 2,915
  • 14
  • 24
1

First we need to travel the array (we will use a map)

fruits.map(ele => { 
})

Then we need to reverse the order of the string there (we do this by converting the string back to array using split(''), then we reverse the array using reverse(), and finally we use join() to put it together as a string

fruits.map(ele => {
  return ele.split('').reverse().join('')
})

Finally we save this into the reversedFruits and voila

const reversedFruist = fruits.map(ele => {
    return ele.split('').reverse().join('')
})
rule
  • 281
  • 1
  • 8
0

Try this:

 fruits = fruits.map(i => i = i.split("").reverse().join(""));
Rajat Badjatya
  • 760
  • 6
  • 13