0

How can i stop executing this Javascript after executing? Usually this problem is caused because of missing return statements in functions. Well, both functions return something. So why does the Skript continue running and what can i do against it?

var system = require('system');
var args = system.args;

function hexToRgb(hex) {
  // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
  var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  hex = hex.replace(shorthandRegex, function(m, r, g, b) {
    return r + r + g + g + b + b;
  });
  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? {
    r: parseInt(result[1], 16),
    g: parseInt(result[2], 16),
    b: parseInt(result[3], 16)
  } : null;
}


function rgb2hex(red, green, blue) {
  var rgb = blue | (green << 8) | (red << 16);
  return '#' + (0x1000000 + rgb).toString(16).slice(1)
}



hexPrimaryColor = args[1];
redPrimaryColor = hexToRgb(hexPrimaryColor).r;
greenPrimaryColor = hexToRgb(hexPrimaryColor).g;
bluePrimaryColor = hexToRgb(hexPrimaryColor).b;
redComplimentaryColor = 255 - redPrimaryColor;
greenComplimentaryColor = 255 - greenPrimaryColor;
blueComplimentaryColor = 255 - bluePrimaryColor;
hexCcomplimentaryColor = rgb2hex(redComplimentaryColor, greenComplimentaryColor, blueComplimentaryColor);
//console.log("Primärfarbe:" + hexPrimaryColor +"\n");
console.log(hexCcomplimentaryColor);
//throw Error();
Chris G
  • 7,957
  • 4
  • 17
  • 29
jo87casi
  • 141
  • 8
  • 3
    This doesn't solve your answer, but please use indentation – vityavv Mar 08 '18 at 13:50
  • 1
    What do you mean, 'continue running'? Functions, when you call `return`, will return a value to their caller. It's the end of this function, but not the end of the entire script. – GolezTrol Mar 08 '18 at 13:55
  • Are you looking for this? https://stackoverflow.com/questions/5266152/how-to-exit-in-node-js – Chris G Mar 08 '18 at 13:56

3 Answers3

1

Try adding process.exit(0) to the end of your program

vityavv
  • 1,386
  • 9
  • 20
0

put you code in a function and call it after return somewhere down

condition()
 return; // put your code after return 

callFunction();

function callFunction(){
 hexPrimaryColor = args[1];
 redPrimaryColor = hexToRgb(hexPrimaryColor).r;
 greenPrimaryColor = hexToRgb(hexPrimaryColor).g;
 bluePrimaryColor = hexToRgb(hexPrimaryColor).b;
 redComplimentaryColor = 255 - redPrimaryColor;
 greenComplimentaryColor = 255 - greenPrimaryColor;
 blueComplimentaryColor = 255 - bluePrimaryColor;
 hexCcomplimentaryColor = rgb2hex(redComplimentaryColor, 
 greenComplimentaryColor, blueComplimentaryColor);
 //console.log("Primärfarbe:" + hexPrimaryColor +"\n");
 console.log(hexCcomplimentaryColor);
   //throw Error();
}
Mustkeem K
  • 5,524
  • 1
  • 24
  • 38
0

Thanks for your answers. In fact the code was completely fine. I just executed the code with phantomjs and didn't put phantom.exit() in the end. Thats why the process didn't stopped.

jo87casi
  • 141
  • 8