#SOLVED - return variable into main Function
62 messages · Page 1 of 1 (latest)
you can't that's not how event listeners work
again, you can not. Describe what you are actually trying to achieve
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
I can't follow
i know it looks complicated
please add syntax highlighting
how to do that?
add "js" after the opening three backticks of your code block
in the same line as your backticks. You can edit your posts too
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
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?
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
you can reverse the order of operations and instead call final once your handler has computed the second argument
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;
})
}```
function doAddition(equal, operators) {
operators.addEventListener(…, …{
…
final(equal, finalFirst)
})
}
let me read it
so it would be something like this doAddition(equal, addition);
to run it
yes
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

marking it as solved in the initial post would help
Same solution for the same problem mentioned 3 days ago though by "some guy"
https://discord.com/channels/239433591950540801/1091420984705302600