0

When I try to console.log strings in an array it prints each text character and the comma separators on a separate line:

var name = ["add","bas","cun","deh"];
var size = [2,5,7,9];
var price = [250,150,25,60];
var count = 0;
var mVar = 4;
var nameLen = name.length;

while (count < mVar) {

    var maxSize = Math.max.apply(null, size);
    var posVar = size.indexOf(maxSize);
    console.log(name[posVar] + " " + size[posVar] + " " + price[posVar]);
    size[posVar] = null
    count++;
}

When I view the console, these are my results:

results

David Makogon
  • 69,407
  • 21
  • 141
  • 189
Sausejii
  • 63
  • 1
  • 5
  • Remove the vars (declarations) from inside the loop, just leave the assignments – user2182349 Nov 19 '16 at 02:48
  • Well, what exactly are you trying to get? Do you not want it on a separate line? Do you not want the comma separators? Having your objective would allow us to solve your problem. – TheGenie OfTruth Nov 19 '16 at 02:48

2 Answers2

0

You got bitten by global window.name. Change your variable to something else or get it out of the global scope.

epascarello
  • 204,599
  • 20
  • 195
  • 236
0

name is a reserved javascript word.

var names = ["add","bas","cun","deh"];
var sizes = [2,5,7,9];
var prices = [250,150,25,60];

console.log(names.map((n, i) => [n, sizes[i], prices[i]].join(' ')).join('\n'));
bruchowski
  • 5,043
  • 7
  • 30
  • 46