Skip to main content
deleted 3 characters in body
Source Link
cFreed
  • 2.9k
  • 16
  • 20

To avoidingavoid repeating console.log(), you can use library.reduce() which'll output a global result, then passed to the console only once.

Regarding your other concern about efficient concatenation, you can use tagged strings.

It results in a more compact code, like this:

var library = [ 
    {
        title: 'Bill Gates',
        author: 'The Road Ahead',
        readingStatus: true
    },
    {
        title: 'Steve Jobs',
        author: 'Walter Isaacson',
        readingStatus: true
    },
    {
        title: 'Mockingjay: The Final Book of The Hunger Games',
        author: 'Suzanne Collins',
        readingStatus: false
    }
];

function displayInformation() {
  console.log(library.reduce( (output, book) => output +=
    `${book.readingStatus ? 'Already read' : 'You still need to read'} ${book.title} by ${book.author}.\n`, ''
  ));
}

displayInformation();

To avoiding repeating console.log(), you can use library.reduce() which'll output a global result, then passed to the console only once.

Regarding your other concern about efficient concatenation, you can use tagged strings.

It results in a more compact code, like this:

var library = [ 
    {
        title: 'Bill Gates',
        author: 'The Road Ahead',
        readingStatus: true
    },
    {
        title: 'Steve Jobs',
        author: 'Walter Isaacson',
        readingStatus: true
    },
    {
        title: 'Mockingjay: The Final Book of The Hunger Games',
        author: 'Suzanne Collins',
        readingStatus: false
    }
];

function displayInformation() {
  console.log(library.reduce( (output, book) => output +=
    `${book.readingStatus ? 'Already read' : 'You still need to read'} ${book.title} by ${book.author}.\n`, ''
  ));
}

displayInformation();

To avoid repeating console.log(), you can use library.reduce() which'll output a global result, then passed to the console only once.

Regarding your other concern about efficient concatenation, you can use tagged strings.

It results in a more compact code, like this:

var library = [ 
    {
        title: 'Bill Gates',
        author: 'The Road Ahead',
        readingStatus: true
    },
    {
        title: 'Steve Jobs',
        author: 'Walter Isaacson',
        readingStatus: true
    },
    {
        title: 'Mockingjay: The Final Book of The Hunger Games',
        author: 'Suzanne Collins',
        readingStatus: false
    }
];

function displayInformation() {
  console.log(library.reduce( (output, book) => output +=
    `${book.readingStatus ? 'Already read' : 'You still need to read'} ${book.title} by ${book.author}.\n`, ''
  ));
}

displayInformation();

Source Link
cFreed
  • 2.9k
  • 16
  • 20

To avoiding repeating console.log(), you can use library.reduce() which'll output a global result, then passed to the console only once.

Regarding your other concern about efficient concatenation, you can use tagged strings.

It results in a more compact code, like this:

var library = [ 
    {
        title: 'Bill Gates',
        author: 'The Road Ahead',
        readingStatus: true
    },
    {
        title: 'Steve Jobs',
        author: 'Walter Isaacson',
        readingStatus: true
    },
    {
        title: 'Mockingjay: The Final Book of The Hunger Games',
        author: 'Suzanne Collins',
        readingStatus: false
    }
];

function displayInformation() {
  console.log(library.reduce( (output, book) => output +=
    `${book.readingStatus ? 'Already read' : 'You still need to read'} ${book.title} by ${book.author}.\n`, ''
  ));
}

displayInformation();