0

So I don't know if this can be done or not but I'm have this function and with some of the variables that I get the result from I make other operations, here is the code.

x();
function x() {
  if (y === "yes") {
    var a = parseInt(prompt("Insert Number Here"))
    var b = parseInt(prompt("Insert Number Here"))
    var c = a * b
    var cont = prompt("yes or no").toLowerCase();
    if (cont === "yes") {
      x();
    } else if (cont === "total") {
      alert total();
    }
  } else if (z === "yes") {
    var d = parseInt(prompt("Insert Number Here"))
    var e = parseInt(prompt("Insert Number Here"))
    var f = d * e
    var cont = prompt("yes or no").toLowerCase();
    if (cont === "yes") {
      x();
    } else if (cont === "total") {
      alert total();
    }
  }
function total() {
  c = c|| 0;
  f = f|| 0;
  return `The total is $${c + f}`
}
}

So, when I run this it just gives the result of the last operation made

  • There are multiple errors in your code `continue` is reserved word, `c` is not defined, `alert` is not an operator. – DecPK Sep 23 '21 at 23:41
  • You can return the value so that it can be stored in a variable and be used outside of the function: `var value = x();`. – George Sun Sep 23 '21 at 23:42
  • What is `totalJ1 ... totalJ2`? – navnath Sep 23 '21 at 23:43
  • @HR01M8055 yes sorry, fixed the continue and c but the alert works just fine –  Sep 23 '21 at 23:46
  • @GeorgeSun how can i do it with multiple values in the same function? –  Sep 23 '21 at 23:50
  • 1
    You can return multiple values in an array or object. – Barmar Sep 23 '21 at 23:50
  • 1
    Please [edit] your question to show show how you call the function. Right now, running the code just throws a syntax error. Note that `c` and `f` in the `total` function are undefined; variables have scope. – Heretic Monkey Sep 23 '21 at 23:51
  • @0o_Samuk_o0, you can return an array: `const result = [a, b, c, d]; return result;`. – George Sun Sep 23 '21 at 23:51
  • 2
    You could do it like you are if you make `c` and `f` global variables instead of local variables. BTW, use more meaningful variable names. – Barmar Sep 23 '21 at 23:51
  • You may be interested in [Passing a local variable from one function to another](https://stackoverflow.com/q/10579713/215552) – Heretic Monkey Sep 23 '21 at 23:52
  • the thing is that every time the function runs the values c and f get reset, that's why I need to store them individually and once it gets triggered the `total` function everything gets added up –  Sep 24 '21 at 00:33

0 Answers0