Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Guide ,
Jun 28, 2025 Jun 28, 2025

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'));
}

 

TOPICS
Scripting
153
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 28, 2025 Jun 28, 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);

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 28, 2025 Jun 28, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 29, 2025 Jun 29, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 29, 2025 Jun 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 29, 2025 Jun 29, 2025

Hi m1b.

I'll definitely look into it when this long busy period is over.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 29, 2025 Jun 29, 2025
LATEST

Hi m1b.

In my code in post #1, readwriteJSON(); is executed twice.
Now I realized that without readwriteJSON(); the code executes as usual.
Without the call, the function readwriteJSON(); function also executes ......
Why is that?

 

Thank you very much, I found another page hiding a readwriteJSON();
is it called again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines