user516883
user516883

Reputation: 9398

javascript keeping count on how many times a picture is deleted

I never used javascript in this way, but I would like to keep track of how many times a delete button is pressed. When the page originally loads I want to set the "value" to 0 and as they press delete keep track. Here is what I have is what obviously does not work, I didnt want to go the route of making a hidden variable and keep track that way. Thanks for any help.

Script

var deletePictureCount;
function MenuAction() {

}
MenuAction.prototype = {
    GetDeletePictureCount: function () {
        if (deletePictureCount == null || deletePictureCount < 0) { return 0; }
        return deletePictureCount;
    },
    SetDeletePictureCount: function () {
        deletePictureCount++;
    }
};

usage on delete

 var incement = new MenuAction().SetDeletePictureCount();

usage when trying to get value back

 var deletedPiccount = new MenuAction().GetDeletePictureCount();

Upvotes: 0

Views: 109

Answers (3)

Aravindan R
Aravindan R

Reputation: 3084

You need to initialize the deletePictureCount to 0 like here,

http://jsbin.com/axeyiq/2/edit#javascript,html

Upvotes: 0

Michael Sazonov
Michael Sazonov

Reputation: 1533

Let's take a look at your script:

var deletePictureCount; - Better set it's initial value if you gon'na use "set" before "get" before ;

if (deletePictureCount == null || deletePictureCount < 0) { return 0; } - this term is needless: I didn't see how exactly your deletePictureCount can get a negative value through your script.

Use if( deletePictureCount ) return deletePictureCount; instead.

It should look like

var deletePictureCount = 0;
function MenuAction() {

}
MenuAction.prototype = {
    GetDeletePictureCount: function () {
        if ( deletePictureCount ) return deletePictureCount; //if deletePictureCount is not null or 0 - return it's value;
        return 0; // else return 0;
    },
    SetDeletePictureCount: function () {
        deletePictureCount++;
    }
};

Upvotes: 1

Matt H
Matt H

Reputation: 6530

Looks a little odd to me - you aren't setting an initial value to the variable, and you are creating two instances of an object to get/set the value? And you are setting a return value to increment but aren't returning a value in that function.

Why not just use deletePictureCount++ and be done with it?

Upvotes: 2

Related Questions