how to swap two variables?

September 14, 2010 at 08:57:28
Specs: Vista 32, 2
I think this is simple, but its just me : (
I need to prompt for two names, swap and output it.

The first name is put in the second one's storage and the second name is put in the first one's storage.

i can use only 3 var

This is how far i got. I am able to swap the first variable. Thanks for your help.

// Declare all the variables
var nameOne;
var nameTwo;
var temp;

//Obtain two flower names from the viewer
nameOne = prompt("variable1");
nameTwo = prompt("variable2");

//Switch strings
temp = nameOne;
nameOne = nameTwo;
nameTwo = temp;

//Output two original variables and switched version
document.write("Strings as entered:");
document.write("\nvariable1: " + nameOne);
document.write("\nvariable2: " + nameTwo);
document.write("<br />");

document.write("\nSwitched strings: ");
document.write("\nvariable1: " + storage);
document.write("\nvariable2: " + storage);


output should be like this with strings switched.

Strings as entered:
variable1: Dell
variable2: HP

Switched strings:
variable1: HP
variable2: Dell

See More: how to swap two variables?

Report •

September 14, 2010 at 09:31:19

Report •
Related Solutions

Ask Question