#SOLVED - return variable into main Function

62 messages · Page 1 of 1 (latest)

normal crescent
#
  operators.addEventListener("click", function operation(finalFirst) {
    firstHalf = result.innerHTML;
    result.innerHTML = "+";
    finalFirst = Number(firstHalf);
    console.log("First Half: ", finalFirst);
    return finalFirst;
  })
} ```
#

just need to return the variable from the inside function to the outside

proven oracle
#

you can't that's not how event listeners work

normal crescent
#

@proven oracle

#

how can i fix this to get that number out of it?

proven oracle
#

again, you can not. Describe what you are actually trying to achieve

normal crescent
#

i can paste the code but simply

#

type number in calculator

#

2

#

click "+"

#

another number

#

4

#

then click "="

#

but the problem is

#

when i click "+" im storing whatever number was enterred onto the display as a variable

#

but when that reaches "=" it cant get that number from the function

#

xd

proven oracle
#

I can't follow

normal crescent
#

i know it looks complicated

proven oracle
#

please add syntax highlighting

normal crescent
#

how to do that?

proven oracle
#

add "js" after the opening three backticks of your code block

#

in the same line as your backticks. You can edit your posts too

normal crescent
#
 let one = document.getElementById("1");
let two = document.getElementById("2");
let three = document.getElementById("3");
let four = document.getElementById("4");
let five = document.getElementById("5");
let six = document.getElementById("6");
let seven = document.getElementById("7");
let eight = document.getElementById("8");
let nine = document.getElementById("9");
let zero = document.getElementById("0");
let result = document.getElementById("output");
let operator = document.querySelectorAll("operator");
let multiply = document.getElementById("multiply");
let addition = document.getElementById("addition");
let divide = document.getElementById("divide");
let subtract = document.getElementById("subtract");
let equal = document.getElementById("equal");

result.innerHTML = 0;
let firstHalf;
let finalFirst = 0;

function getNumber(x) {
  newString = "none";
  number1 = 0;
  finalNum = 0;
  x.addEventListener("click", function output() {
    xx = Number(x.innerHTML);
    if (
      (result.innerHTML == 0) &
      (result.innerHTML.length < 13) &
      (result.innerHTML != "+")
    ) {
      number1 = result.innerHTML = xx;
      newString = String(number1);
    } else if (
      (result.innerHTML != 0) &
      (result.innerHTML.length < 13) &
      (result.innerHTML != "+")
    ) {
      number1 = result.innerHTML = result.innerHTML + xx;
    } else if (
      (result.innerHTML != 0) &
      (result.innerHTML.length < 13) &
      (result.innerHTML == "+")
    ) {
      result.innerHTML = xx;
      number1 = result.innerHTML;
    } else {
      result.innerHTML = 0;
    }
  });
  return result.innerHTML;
}

function doAddition(operators) {
  operators.addEventListener("click", function operation(finalFirst) {
    firstHalf = result.innerHTML;
    result.innerHTML = "+";
    finalFirst = Number(firstHalf);
    console.log("First Half: ", finalFirst);
    return finalFirst;
  })
}

function final (complete, finalFirst) {
  complete.addEventListener("click", function equals() {
    console.log("first half 2: ", finalFirst);
    secondHalf = result.innerHTML;
    finalSecond = Number(secondHalf);
    console.log("Second Half: ", finalSecond);
    End = finalFirst + finalSecond;
    console.log("First Half: ", finalFirst, " Second Half: ", finalSecond);
    result.innerHTML = End;
  });
}

final(equal, doAddition(addition));
getNumber(one);
getNumber(two);
getNumber(three);
getNumber(four);
getNumber(five);
getNumber(six);
getNumber(seven);
getNumber(eight);
getNumber(nine);
getNumber(zero);```
#

FIXED

#

the highlights

proven oracle
#

you're already working with variables that are scoped beyond your handler. So that shouldn't be an issue. Can you point out where the problem occurs?

normal crescent
#

ok

#
final(equal, doAddition(addition));```
#

i can not return the value from doAddition into the final function

#

doAddition is storing the number of the first half of my equation

proven oracle
#

you can reverse the order of operations and instead call final once your handler has computed the second argument

normal crescent
#

ok

#

i actually dont get it that much

#
function doAddition(operators) {
  operators.addEventListener("click", function operation(finalFirst) {
    firstHalf = result.innerHTML;
    result.innerHTML = "+";
    finalFirst = Number(firstHalf);
    console.log("First Half: ", finalFirst);
    return finalFirst;
  })
}```
proven oracle
#
function doAddition(equal, operators) {
  operators.addEventListener(…, …{
    …
    final(equal, finalFirst)
  })
}
normal crescent
#

let me read it

#

so it would be something like this doAddition(equal, addition);

#

to run it

proven oracle
#

yes

normal crescent
#

leme try

#

OMG

#

IT PRINTED 1+1=2

#

IT WORKS

#

FINALLY

#

I NEED TO ANALYSE THIS

#

thanks LenGPT

#

look

#

@proven oracle you see it works

#

YES YES

#

some guy was telling me i needed to do a callback

#

man that was confusing

#

shall i close the ? or leave open

#

anyways that was it

#

thanks alot

#

ill probably be back later

proven oracle
#

marking it as solved in the initial post would help

normal crescent
#

ok

#

SOLVED - return variable into main Function

willow linden