Skip to content
Advertisement

Swapping array with alternative numbers

In an array take alternate numbers and swap them. Put it again in the place of array again e.g., arr = [8,7,4,6,9,3,2,1] newarr= [8,1,4,3,9,6,2,7]

The Code which I tried is below one

JavaScript

This is not the correct way to proceed.

Advertisement

Answer

When you consider every odd indexed element to be alternate –

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement