2
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

1 Answer 1

1

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`)})

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.