Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8586317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:16:55+00:00 2026-06-11T22:16:55+00:00

I am beginner of CoffeeScript and Jasmine. At first, I tried to pass test

  • 0

I am beginner of CoffeeScript and Jasmine. At first, I tried to pass test with below code:

class Counter
    count: 0

    constructor: ->
        @count = 0

    increment: ->
        @count++

    decrement: ->
        @count--

    reset: ->
        @count = 0

root = exports ? this
root.Counter = Counter

The jasmine test code is below:

describe("Counter", ->
    counter = new Counter
    it("shold have 0 as a count variable at first", ->
        expect(counter.count).toBe(0)
    )

    describe('increment()', ->
        it("should count up from 0 to 1", ->
            expect(counter.increment()).toBe(1)
        )
    )
)

Then kind person told me that the code should be as below:

class Counter
    count: 0

    constructor: ->
        @count = 0

    increment: ->
        ++@count

    decrement: ->
        --@count

    reset: ->
        @count = 0

root = exports ? this
root.Counter = Counter

Yes, this code passed the test. But I have a question that the former code is more natural than latter code. I have no idea how to certain this question. Thank you for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:16:56+00:00Added an answer on June 11, 2026 at 10:16 pm

    you could change your code to the following to be more clear about return values if you choose to stick to post increment

    class Counter
      count: 0
    
      constructor: ->
        @count = 0
    
      increment: ->
        @count++
        @count
    
      decrement: ->
        @count--
        @count
      reset: ->
        @count = 0
    
    root = exports ? this
    root.Counter = Counter
    

    or you could change your test to:

    describe('increment()', ->
      it("should count up from 0 to 1", ->
        expect(counter.count).toBe(0)
        counter.increment()
        expect(counter.count).toBe(1)
      )
    )
    

    but then you don’t expect the returned value of increment and decrement to reflect the updated value of @count

    here’s an example to play with that will make the differences obvious:
    http://coffeescript.org/#try:class%20Counter%20%20count%3A%200%20%20increment%3A%20-%3E%20%20%20%20%40count%2B%2B%20%20%20%20%40count%20%20%20%20inc%3A%20-%3E%20%20%20%20%40count%2B%2B%20%20decrement%3A%20-%3E%20%20%20%20–%40count%20%20dec%3A%20-%3E%20%20%20%20%40count–cnt%20%3D%20new%20Counteralert%20cnt.increment()alert%20cnt.countalert%20cnt.inc()alert%20cnt.countalert%20cnt.decrement()alert%20cnt.countalert%20cnt.dec()alert%20cnt.count

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am beginner of CoffeeScript and Jasmine. I tried to test a simple class
Beginner in Android development. My code crashes. I have made a simple Java method
Question : Beginner Level Code: Pure Javascript I had created a web page in
A beginner question: I don't know how best to structure this bit of code,
Beginner Android and Java developer here. I have this snippet of code inside a
very beginner question. I am using Rails 3's query interface as shown: class User
iam beginner in Objective-C, i tried to compile small Hello world program to start,iam
Django beginner question. I have the following model: class Leg(models.Model): startpoint = models.CharField(max_length=50, help_text=examples:
Beginner Question. Below is an example given on the Cradle CouchDB documentation: https://github.com/cloudhead/cradle What
Beginner question: I want to debug some code that compiles. I pressed debug, it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.