HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Library assistant

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
assistantlibrarystackoverflow

Problem

HackerRank currently has '7 days of JavaScript' going, a quick intro into the JavaScript language. Every day a couple of challenges will be made available. One of the challenges of today was this:


Task


Write a JavaScript program to display the status (i.e. display book name, author name and reading status) of books. You are given an object library in the code's template. It contains a list of books with the above mentioned properties. Your task is to display the following:



  • If the book is unread:





You still need to read '' by .



  • If the book is read:





Already read '' by .

library was provided, as was the empty function displayInformation() (and no, it is not allowed to pass library as an argument to the function).

The required code is as straight-forward as it gets, but I feel there's a more proper way to do the string formatting.

function displayInformation() {
    for (var i = 0; i < library.length; i++) {
        if (library[i].readingStatus) {
            console.log("Already read '" + library[i].title + "' by", library[i].author + ".");
        } else {
            console.log("You still need to read '" + library[i].title + "' by", library[i].author + ".");
        }
    }
} 

// provided by HackerRank
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
    }
];

displayInformation();


As you can see, I'm inconsistent within the console.log() functions and there's needless repetition. I'm also hacking my way into the library where I feel a more OO approach would look better. I think separating whatever you want to send to console.log() should be done by comma's whenever possible and concat

Solution

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();

Context

StackExchange Code Review Q#119534, answer score: 4

Revisions (0)

No revisions yet.