1

I have an array and I need to double the values of the odd possitions and return the doubled values and the untouched ones in a new array in the same order as they came. I already tried with indexOf() but when a number is repeated in the array it won't double and returns the same. I know I can use a "for", but I'm mostly looking for a direct simple way to keep my code short. I'm using vanilla Javascript.

let num="123456"
let numArray=Array.from(num)

Wanted output:

newArray[1,4,3,8,5,12]

Also, very new developer here...so, sorry if this went too long. Thanks in advance!

G9D4
  • 11
  • 1
  • 1
    `const newArray = Array.from("123456", (num, index) => { if(index % 2 === 1){ return num * 2; } return Number(num); });`. Familiarize yourself with [how to access and process nested objects, or arrays](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Oct 13 '21 at 06:04
  • [..."123456"].map((e,i)=>i%2 ? 2*e : +e) – QuentinUK Oct 13 '21 at 06:13

0 Answers0