Skip to main content
dublove
Legend
June 29, 2025
Answered

It's strange how cNum always gets +1, even when cNum==10

  • June 29, 2025
  • 2 replies
  • 271 views

The cNum is taken from my.json.
Is it a string? But then it can participate in arithmetic.
“cNum”: 24.

 

Here (cNum>6) seems to work, even though checkCode(); has been executed, cNum still increases by 1.
cNum keeps increasing whenever you click on it!

 

And it will add two 1's (+1+1),I checked elsewhere without the same setting.

 

if (cNum <= 6) {
    readwriteJSON();
}

else if (cNum >6) {
    checkCode();
}

function readwriteJSON() {
    var myJSON = readFile(pathToMyJSONFile);
    if (!myJSON)
        return alert('Could not read JSON file.');
     myJSON = JSON.parse(myJSON);
      cNum = myJSONObject.cNum;
    myJSON.cNum = cNum + 1;
    alert(cNum);
    var f = writeFile(pathToMyJSONFile, JSON.stringify(myJSON, undefined, '\t'));
}

 

Correct answer m1b

@dublove you are confusing things.

 

In this line

if (cNum <= 6) {

 

you are referring to an undeclared variable, which will give an error, unless you declare it earlier like this:

var cNum = 0

 

I'm assuming you have done that earlier in your code.

 

But the in the readwriteJSON function you increment the object—hopefully a number—that is stored in the myJSON variable's cNum property. And then you show the variable cNum in an alert box. But because these are totally different values, it makes no sense.

 

In your readwriteJSON function, change these lines:

cNum = myJSONObject.cNum;
myJSON.cNum = cNum + 1;

to

cNum++;
myJSON.cNum = cNum;

 

(Note that cNum++; is the same as cNum = cNum + 1;)

 

Also, very important to note is that you can access cNum from the readwriteJSON function only because they are in the same variable scope—in some cases you can do it that way.

- Mark

2 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
June 29, 2025

@dublove you are confusing things.

 

In this line

if (cNum <= 6) {

 

you are referring to an undeclared variable, which will give an error, unless you declare it earlier like this:

var cNum = 0

 

I'm assuming you have done that earlier in your code.

 

But the in the readwriteJSON function you increment the object—hopefully a number—that is stored in the myJSON variable's cNum property. And then you show the variable cNum in an alert box. But because these are totally different values, it makes no sense.

 

In your readwriteJSON function, change these lines:

cNum = myJSONObject.cNum;
myJSON.cNum = cNum + 1;

to

cNum++;
myJSON.cNum = cNum;

 

(Note that cNum++; is the same as cNum = cNum + 1;)

 

Also, very important to note is that you can access cNum from the readwriteJSON function only because they are in the same variable scope—in some cases you can do it that way.

- Mark

dublove
dubloveAuthor
Legend
June 29, 2025

But my cNum isn't 0.
It has an initial value.
Its value is cNum = myJSONObject.cNum;

m1b
Community Expert
Community Expert
June 29, 2025

@dublove most of your problems lately are that you don't understand basic javascript. I would strongly recommend you spend some time studying basic ECMAScript 3 (ES3) javascript which is almost identical to ExtendScript. Please find a tutorial or two.

 

Specifically need to understand about variables and variable scope, objects, properties, functions and passing values to and from functions.

 

Your questions are becoming very muddled because you don't share the whole code and your partial code snippets make no sense. We can't be much help if this is the way.

- Mark

dublove
dubloveAuthor
Legend
June 29, 2025

Now I only keep it outside

if (cNum > 6) {
checkCode();
}

And putting the following into readwriteJSON(){    It is seems to work fine.
But I still can't figure out the front side.

if (cNum <= 6) {
myJSON.cNum = cNum + 1;
alert(cNum);