It appears that your intention is to input one variable into a function and return another variable that has not been defined yet. Then, by inputting this undefined variable back into the function, you want to define it and have the first input as the output, resulting in two outputs.
In theory, achieving this is impossible due to the presence of undefined variables. However, with a touch of creativity, I believe I have come up with a solution (a bit of a hack, but it should get the job done):
var i = 1;
var output1;
var output2;
function swap(input) {
function func1(input) {
output2 = input;
i++;
}
function func2(input) {
output1 = input;
i = 1;
alert(String(output1) + "\n" + String(output2));
}
if (i === 1) {
func1(input);
}
else if (i === 2) {
func2(input);
}
}
while(true) {
swap(prompt("Enter your first input (this will be your second output):"));
swap(prompt("Enter your second input (this will be your first output):"));
}
The swap
function alternates between the values 1 and 2 in the variable i
, allowing it to keep track of first or second inputs and their respective outputs. The user's input for the prompt boxes serves as the parameter for the swap
function. While the code may seem rudimentary, there is more complexity at play under the hood. By collecting all input data and reversing the order upon output, my program manages to provide the desired functionality. To an end-user unfamiliar with JavaScript intricacies, this approach would appear seamless.
This method should accommodate various data types, including objects
, strings
, numbers
, and arrays
. Give it a try; I hope it proves helpful!