0

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;
   }
 }
2
  • 3
    return them as an object? return { step1: step1, step2: step2 }; Commented Dec 6, 2014 at 0:37
  • I'm going to have to agree, I think ... object is the route to go. Your question needs a bit of clarity. Are you looking to return both objects with the function or the strings you defined above? Commented Dec 6, 2014 at 0:38

2 Answers 2

3

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;
   }
Sign up to request clarification or add additional context in comments.

5 Comments

That's an array though.
Not sure why you wouldn't use the obj = { key: value } format but ok.
/I just did it so you have something to complain about. Nah I just like to type more chars. (notice I abbreviated "Characters")
great, I will return an object from each function, but now how do I call and get the function to fill.., "Mix " + (Step1 productType) + " with " + (Step1 peroxide) + ". " + (Step1 timing) Using a " + (Step2 productType) + " , mix " + (Step2 toneCalc) + ", with " + (Step2 peroxide) + ". " + (Step2 timing)
Sheri, you can add 'var peroxide = peroxide();' then o 'peroxide.step1' and 'peroxide.step2' to fill
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.