Experiencing inconsistent results with ticket seller kata evaluations

I'm working on improving my javascript testing skills through katas. I am facing some challenges with a specific one that involves creating a TicketClerk object to handle movie ticket transactions.

ticketClark.js

var TicketClerk = function() {
  this.till = { 25: 0, 50: 0, 100: 0 };
};

TicketClerk.prototype.sell = function(array) {
  for (var i = 0; i < array.length; i++) {
    if (this.canMakeChange(array[i])) {
      this.giveChange(array[i]);
    } else {
      return "NO";
    }
  }
  return "YES";
};

TicketClerk.prototype.canMakeChange = function(note) {
  if (note === 50) {
    return this.till[25] > 0;
  }
  if (note === 100) {
    return this.canGiveFiftyTwentyFive() || this.canGiveThreeTwentyFives();
  }
  return true;
};

TicketClerk.prototype.giveChange = function(note) {
  if (note === 25) {
    this.till[25]++;
  }
  if (note === 50) {
    this.till[25]--;
    this.till[50]++;
  }
  if (note === 100 && this.canGiveFiftyTwentyFive()) {
    this.till[25]--;
    this.till[50]--;
    this.till[100]++;
  }
  if (note === 100 && this.canGiveThreeTwentyFives()) {
    this.till[25] -= 3;
    this.till[100]++;
  }
};

TicketClerk.prototype.canGiveThreeTwentyFives = function() {
  return this.till[25] > 2;
};

TicketClerk.prototype.canGiveFiftyTwentyFive = function() {
  return this.till[25] > 0 && this.till[50] > 0;
};

test.js

describe("#TicketClerk", function() {
  beforeEach(function() {
    ticketClerk = new TicketClerk();
  });
  describe("#initialize", function() {
    it("verifies the initial state of the till as having zero of each denomination", function() {
      expect(ticketClerk.till).toEqual({ 25: 0, 50: 0, 100: 0 });
    });
  });

  describe("#sell", function() {
    it("ensures 'YES' is returned when able to give change for tickets purchased with 25, 25, 50", function() {
      ticketClerk.sell([25, 25, 50, 50]);
      expect(ticketClerk.sell).toEqual("YES");
    });
    it("ensures 'NO' is returned when unable to give change for tickets purchased with 50, 25, 50", function() {
      ticketClerk.sell([50, 25, 50]);
      expect(ticketClerk.sell).toEqual("NO");
    });
  });
});

I have additional tests omitted here but the main idea is accepting $25 movie tickets and providing change to customers based on given denominations. The expected output should be "YES" if change can be provided to all customers, and "NO" if not.

Answer №1

Ensure you are passing the result of the method call instead of just the method name in the expect statement. Update your code as follows:

describe("#TicketClerk", function() {
  beforeEach(function() {
    ticketClerk = new TicketClerk();
  });
  describe("#initialize", function() {
    it("verifies the initial till values are all zero", function() {
      expect(ticketClerk.till).toEqual({ 25: 0, 50: 0, 100: 0 });
    });
  });

  describe("#sell", function() {
    it("should return 'YES' when given correct change", function() {
      const result = ticketClerk.sell([25, 25, 50, 50]); // pass result
      expect(result).toEqual("YES");
    });
    it("should return 'NO' when unable to give change", function() {
      const result = ticketClerk.sell([50, 25, 50]); // pass result
      expect(result).toEqual("NO");
    });
  });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I effectively utilize executeScript in Selenium and webdriver.io?

I'm currently working on testing a basic form using Selenium, WebDriver.io, and Node.js (with Mocha). Here is a snippet of the code I have: var webdriverio = require('webdriverio'); var expect = require('expect'); describe(' ...

Encountering a "404 not found" error when trying to access `/en/first` with the routing

I am currently working on setting up a routing module in Node.js & express, but I seem to be encountering some issues. The specific error message I am getting is Cannot GET /en/first The goal is to organize different folders like 'en', each wit ...

How can I utilize Javascript, HTML, JQuery, and CSS to dynamically set a variable based on a HTML Select's OnChange event, perform calculations, and automatically update the result?

I'm in the process of creating a pool pump calculator. While my HTML onchange function is working perfectly, I am struggling with passing the active Div value into my Javascript If Else statement and updating the outputs accordingly for each case of E ...

Problems with the functionality of Javascript and ajax

I've created this JavaScript/AJAX code, but unfortunately, it's not functioning as expected and I am unable to determine the reason behind it. $(document).ready( function(){ $('#btn4').click( function(){ var plano = $(&a ...

OpenLayers' circular frames surrounding the icons

I am currently using openlayers and trying to implement a feature that creates a circle around the icons on the map. I have been referring to this example on Stack Overflow but unable to draw the circle successfully. Can someone please assist me with this? ...

Mongoose encountered an error when attempting to cast the value "......" as an ObjectId in the "author" path. The error was caused by a BSONError

I'm currently facing an issue with Mongoose in my NextJS project. Specifically, I am encountering a problem when trying to save a document where one of the fields references an ObjectId. The error message I receive is as follows: Cast to ObjectId fail ...

Continue running the remaining part of the function once the asynchronous function has completed its execution

To obtain the last 4 digits of a payment using Stripe, I need to execute an async function that contains another async function. Once these functions are resolved, I aim to update the database with the last four digits. It is crucial to ensure that the dat ...

What can Yeoman do with php files?

How can I set up Yeoman (latest version: v0.9.6) to serve php files? I came across this post but couldn't get it to work. I installed https://github.com/fgnass/gateway, https://github.com/fgnass/tamper and made the necessary updates to Yeoman accor ...

Is there a way to modify localStorage without having to refresh the page?

I'm currently working on a shopping cart that pulls products from a json-server. So far, I've successfully displayed the products, added the functionality to add items to the cart, and show the quantity. However, I'm facing a roadblock with ...

Utilizing jQuery to manage asynchronous tasks, such as executing AJAX requests, handling promises, and using deferred

Exploring My jQuery Plugins: (function ($, window, document, undefined) { $.fn.loadPageContent = function (url, dataToSend) { url = url || window.location.href; dataToSend = dataToSend ? dataToSend : {}; return $.post(url, data ...

Getting a Next.js error after performing a hard refresh on a page that contains a dynamic query

I have encountered an issue with my Next.js app when I attempt to hard reload it in production mode. The error message I receive is 404 - File or directory not found. Below is the code snippet I am using: import { useRouter } from "next/router"; import ...

Separate the express node js into a pair

I'm attempting to divide a code into two parts using express. Here is how I approached it: app.js var express = require('express'); var app = express(); var stud = require('./grades'); var port = process.env.PORT || 3000; stud. ...

Error: Morris.js is unable to access the property 'x' because it is undefined

Seeking assistance in utilizing Morris.js to create an area chart using data from a JSON file. The JSON data is as follows: [{"period": 0, "time": 121.0}, {"period": 1, "time": 102.0}, {"period": 2, "time": 104.0}, {"period": 3, "time": 91.0}, {"period": ...

What makes React Router distinct as a React component?

What is the reasoning behind react-router being a React Component that utilizes React internally? As routing issues were already addressed before the introduction of React Components? In the case where the path property does not align with the URL path, w ...

Looking for the module or file associated with a function handle

When working with NodeJS, how can one identify the file or module where a given function was declared based on its function handle? For instance: // File 1 import { test } from './file1' function fileFile(fn: Function){ //... facing an issu ...

Transitioning a NPM project to the Apache server

Recently, I successfully managed to run a simple example project by following these steps: I downloaded and installed Node.js for windows x64. I then used Git to clone the project from https://github.com/BretCameron/three-js-sample.git Next, I ran t ...

Utilizing HTML5 to Access and Update custom data attributes

I have implemented the following code: var activeFilter = $('<li></li>').data('input-id', 'mycustomId'); $('#container').append(activeFilter); Now, I am faced with the challenge of retrieving a specific ...

The wonders of JSON.stringify() and the dynamic world of JavaScript Objects

Maybe I overlooked something in my JavaScript knowledge and I'm just realizing it now. I was experimenting with this code snippet in the Chrome console: a = []; a.name = "test"; JSON.stringify(a); // this returns [] a = new Object(); a.name = "test ...

Creating my very own slider

I'm currently working on a slider and encountering a few challenges. 1) The animation on the first slide isn't functioning as expected. 2) The spacing for the last box initially appears incorrect, but adjusts as the slider progresses. 3) I&apo ...

Accessing information from specified JSON nested arrays in JavaScript without using the getJSON method

I'm still learning about JSON and ajax, and I have a question about accessing data within nested arrays. Specifically, I am struggling to access items enumerated in a sub array which is located within another sub array. Whenever I try to access elemen ...