user3338991
user3338991

Reputation: 461

js console log string formating starting point

let amounts = [1,10,100]
let animals = [cat,dog,horse]
for (let n = 0; n<3; n++){
console.log("I have " + amounts[n] + " of " + animals[n] + " in total" )
} 

Currently I get:

I have 1 cat in total
I have 10 dog in total
I have 100 horse in total

Is there a way to format this log with "columns" (they are incoming one by one, and I cannot log them as table)

I have 1    cat    in total
I have 10   dog    in total
I have 100  horse  in total

Upvotes: 2

Views: 38

Answers (1)

ecoplaneteer
ecoplaneteer

Reputation: 1984

let amounts = [1, 10, 100]
let animals = ['cat', 'dog', 'horse']
for (let n = 0; n < 3; n++){
console.log("I have\t" + amounts[n] + "\t" + animals[n] + "\tin total" )
}

Another solution is to use Array.forEach()

let amounts = [1, 10, 100]
let animals = ['cat', 'dog', 'horse']

amounts.forEach((amount, index) => {console.log(`I have\t${amount}\t${animals[index]}\tin total`)})

Upvotes: 1

Related Questions