0

I have function at the bottom of my script which counts for how many tools were used.

Then based on how many tools were used I want to perform different actions.

I can easily check the output of the Function but I struggle to put it into If statement.

How to get Functions output and re-use it within IF Statement?

var HowManyTools = HowManyTools1();

if (HowManyTools <= 2) {
  Category13();
} else if (HowManyTools >= 6) {
  Category14();
} else if (HowManyTools > 2) {
  Category12();
}

function HowManyTools1() {
//returns value between 1-9
}

Update: I've added if to the last else. It executes the Category13();. Without if all previous statements were simply false so it went straight to the last statement with Category12();

I can output into Category13/ Category12. But not the Category14.

It seems like my function can't get defined, as soon as I put it within a variable, and if I try to alert(HowManyTools) I simply get undefined Error.

Tried a few examples from here but to no avail

Ryul
  • 139
  • 12
  • your function is empty. return something from it. Also this is not a valid code snippet, it doesn't run. – Steven Stark Mar 28 '19 at 17:38
  • 1
    What you have works, as long as you fix the syntax error - you're missing an `if` before the last condition – VLAZ Mar 28 '19 at 17:39
  • well, `HowManyTools1()` should return a number... something like: `HowManyTools1(){do stuff; return 1}` – Calvin Nunes Mar 28 '19 at 17:39
  • Are you trying to work with the output of `Category12()`, `Category13()`, etc? Or are you just concerned with `HowManyTools` right now? – Scott Sauyet Mar 28 '19 at 17:42
  • @CalvinNunes original `HowManyTools1()` does return a value. The problem is that when I try `var HowManyTools = HowManyTools1();` this still gives `undefined` :P – Ryul Mar 28 '19 at 20:48
  • What happens inside `HowManyTools1()` ? if no errors are thrown, it is impossible that if a `return` statement is present, that it returns undefined. Unless the return value is wrong, you implicitly returns `undefined`, OR you are making an asynchronous call that did not returned yet. If the last is the case, it is VERY important to let it clear in your question – Calvin Nunes Mar 28 '19 at 20:56
  • `HowManyTools1()` simply counts how many `trues` were within an array. If I simply run `HowManyTools1();` - it returns a value between 1-9. But as soon as I assign it to a `var` - I get `undefined`. In here they have written a whole book, why this throws an `undefined`: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Ryul Mar 28 '19 at 21:04
  • The cartoon does not explain anything to me.. Seems more promise - deffered related – F.H. Mar 29 '19 at 09:44
  • @F.H. you are completely correct - the linked thread is about returning results from asynchronous functions. This code is not async at all. OP seems to have a problem that shouldn't be there because the code is *correct*. The explanation of what goes wrong is confusing. – VLAZ Mar 29 '19 at 09:54
  • All I need is somehow get the output of the function. And reuse it n- amount of times within IF condition, so I could compare to various values. When I am back to my machine, I’ll post the full script - maybe it will make more sense. Maybe as you guys say, there is some small syntax error within function itself, which allows for function to be executed and return a value, but causes problems down the line when I try to parse its output into IF condition. I start to think that this is again one of those things which are not well suppported or implemented on PS CC2014 jsx, or maybe simply a bug. – Ryul Mar 29 '19 at 12:19
  • Ohh just realised that the new stackoverflow update has messed up the tags for the question... I had to specify the script runs in Photoshop... which is a whole new story... – Ryul Mar 29 '19 at 12:23

1 Answers1

0

This variant should demonstrate that every one of your Category functions will run if its if-condition is met.

I pass HowManyTools to each of those functions for demonstration purposes.

for (var i = 0; i < 20; i++) {
  test();
}  

function test () {
  var HowManyTools = HowManyTools1();

  if (HowManyTools <= 2) {
    Category13(HowManyTools);
  } else if (HowManyTools >= 6) {
    Category14(HowManyTools);
  } else if (HowManyTools > 2) {
    Category12(HowManyTools); 
  }
}

function HowManyTools1() {
  return  Math.floor(Math.random() * 9 + 1);
}

function Category12(val) {
  console.log(`Category 12: value = ${val}`);
}

function Category13(val) {
  console.log(`Category 13: value = ${val}`);
}

function Category14(val) {
  console.log(`Category 14: value = ${val}`);
}
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103