Skip to content
Advertisement

How can the output elements put in an array in javascript?

function main() {
    var increase = parseInt(readLine(), 10);
    var prices = [98.99, 15.2, 20, 1026];
    //your code goes here
    for(var i=0;i<prices.length;i++)
    {
    var b=prices[i]+increase;
    console.log(+b);
   }
}

Here the output is displayed in seperate elements but I want all the elements in a single array.

Advertisement

Answer

You should just create array and push into it:

function main() {
    let result = [];
    var increase = parseInt(readLine(), 10);
    var prices = [98.99, 15.2, 20, 1026];
    //your code goes here
    for(var i=0;i<prices.length;i++)
    {
    var b=prices[i]+increase;
    result.push(b);
   }
  
  return result;
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement