GithubHelp home page GithubHelp logo

ticket-clerk's Introduction

Movie Clerk

This is a kata from https://www.codewars.com/kata/555615a77ebc7c2c8a0000b8/train/javascript

The new "Avengers" movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single dollars bill. An "Avengers" ticket costs 25 dollars.

Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line.

Can Vasya sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?

Return YES, if Vasya can sell a ticket to each person and give the change with the bills he has at hand at that moment. Otherwise return NO.

Examples

tickets([25, 25, 50]) // => YES
tickets([25, 100])
        // => NO. Vasya will not have enough money to give change to 100 dollars

Installation

Code is written in Javascript and tested using Jasmine.

[email protected]:puyanwei/ticket-clerk.git
cd ticket-clerk
open SpecRunner.html

Conclusion

On having a pairing session and trying this out in Ruby first, it seems to me that it is indeed easier, and more readable than Javascript.

I had a lot of issues with trying to get my forEach loop to work, and it loses its scope inside it and is unable to refer to the this keyword. This meant that you had to attach the this keyword using the bind method or use an arrow function which stops the scope problem from happening.

However, once this was done I soon realised that you cannot break out of Javascript forEach loops, meaning that when one transaction returns false because there is no change, it keeps on going.

Javascript also can be quite confusing in remembering to have to explicitly keep returning outcomes of the functions. This is especially important when using controller functions that call other functions.

Note that the Kata tests don't use constructor functions, so till would be a global variable hash. It should also be noted that the Codewar "best solutions" typically are biased towards the shortest lines of code rather then readability and clarity.

ticket-clerk's People

Contributors

puyanwei avatar

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.