Sheri Trager
Sheri Trager

Reputation: 852

returning multiple values from functions

I have a js file (below) where each function returns 2 results (Step1 and Step2). I need to put these results into a format like...

"Mix " + (Step1 productType) + " with " + (Step1 peroxide) + ". " + (Step1 timing)

Using a " + (Step2 productType) + " , mix " + (Step2 toneCalc) + ", with " + (Step2 peroxide) + ". " + (Step2 timing)

What is the best way to get these results into the formats above?
I know how to do a single value, but not quite sure on multiple functions wrapped in a function.

Thanks

function doubleprocess(type) {

   function productType() {
    var step1;
    var step2;
    if (type == "light") {
        step1 = "lightner";
        step2 = "demi-color";
    } else {
        step1 = "demi-color";
        step2 = "demi-color";
    }
    return step1, step2;
   }


   function toneCalc() {
    var step1;
    var step2;

    if (type == "light") {
        step1 = "light gold";
    } else if (type == "dark") {
        step1 = "80% gold with 20% red"
    } else {
        step1 = "another tone";
    }

    step2 = "neutral";

    return step1, step2;
   }

   function peroxide() {
    var step1;
    var step2;

    if (type == "light") {
        step1 == "20V/6% peroxide";
    } else if (type == "dark") {
        step1 = "something else";
    } else {
        step1 = "Do something.";
    }
    step2 = "Do something else.";
    return step1, step2;
   }

   function level() {
    var step1 = "";
    var step2;

    if (type == "dark") {
        step1 = "Do something.";
    } else {
        step1 = "Do Nothing.";
    };

    step2 = "Do something.";

    return step1, step2;
   }

   function timing() {
    var step1;
    var step2;
    if (type == "light") {
        step1 == "Do something.";
        step2 = "Do something.";
    } else if (type == "dark") {
        step1 = "Do something.";
        step2 = "Do something.";
    } else {
        step1 = "Do something.";
        step2 = "Do something.";
    }
     return step1, step2;
   }
 }

Upvotes: 0

Views: 70

Answers (2)

Liam Schauerman
Liam Schauerman

Reputation: 841

Javascript functions only return a single value. Try returning an object or array.

Upvotes: 1

Sam Battat
Sam Battat

Reputation: 5745

You cannot return multiple variables from a function. However you could return an object.

function peroxide() {
    var step1;
    var step2;

    if (type == "light") {
        step1 == "20V/6% peroxide";
    } else if (type == "dark") {
        step1 = "something else";
    } else {
        step1 = "Do something.";
    }
    step2 = "Do something else.";
    var rtn = new Object();
    rtn.step1 = step1;
    rtn.step2 = step2;
    return rtn;
   }

Upvotes: 3

Related Questions