Giter Club home page Giter Club logo

hackerrank-drawingbook-js-solution's Introduction

Hackerrank-DrawingBook-JS-solution

Function description

Complete the pageCount function in the editor below. It should return the minimum number of pages Brie must turn.

pageCount has the following parameter(s):

  • n: the number of pages in the book
  • p: the page number to turn to

Solution (JavaScript)

function pageCount(n, p) {
  var frontFlip = Math.floor(p/2);
  var backFlip = Math.floor((n/2)-frontFlip);
  
  var result = Math.min(frontFlip, backFlip);
  return result;


}

Explanation

There are two possible ways for Brie to flip her book open: either from the front or the back. The point of the function is to determine in which way she would have to flip less to get to the target page.

For the first case i.e. Front flip

The number of flip she has to make is just the lower bound of the target page divided by 2 i.e.

Math.floor(p / 2).

For the seconde case i.e. Back flip

In this case, we firstly need to get the total number of flips to see through the entire pages of the book then minus the front flip from it. i.e.

Math.floor((n/2)-frontFlip).

Once the number of flip for the two ways have been dertermined, just take the min one. i.e.

Math.min(frontFlip, backFlip)

If you have an alternative solution, kindly checkout on a new branch and request for a pull request to merge your copy.

hackerrank-drawingbook-js-solution's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.