Skip to main content
edited tags
Link
Pimgd
  • 22.6k
  • 5
  • 68
  • 144
Source Link
Mast
  • 13.8k
  • 12
  • 57
  • 127

Library assistant

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 '<book_name>' by <author_name>.

  • If the book is read:

Already read '<book_name>' by <author_name>.

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 concatenation should only be used if there's no other way.

Since I have to print the name of the book between ' ', I picked concatenation for whenever the usual style (comma's) wouldn't work. Now it looks like a mess. Should I've switched to concatenation the moment one part of the string is using it?

Does JavaScript have something like a string.format() or would that make it look worse?

I'm not sure this is the idiomatic way of retrieving all the elements from the library either. Or hey, I may just be over-thinking a simple challenge.