JS Arrays Demo

Example 1

In this example, we use JavaScript to:

Make your choices:


Code:

<script>
var date = new Date();
var mixedArr = ["apple", 42, "hippo", date.getFullYear()];
var strArr = ["tree", "Bob", "bob", "bobby", "treat"];

function doThing(){
	var el = document.getElementsByName("choice");
	var thisChoice;
	for(i=0; i<el.length; i++){
		if(el[i].checked == true){
			thisChoice = el[i].value;
		}
	}
	el = document.getElementById("func");
	var functionChoice = el.value;
	if(functionChoice == "sort"){
		doSort(thisChoice);
	}else if(functionChoice == "join"){
		doJoin(thisChoice)
	}else{//what else?
	
	}
}
function doSort(choice){
	var output;
	if(choice == "mixed"){
		 output = mixedArr.sort();
	}else{
		output = strArr.sort();
	}
	var el = document.getElementById("demo");
	el.innerHTML = "Sorted OUTPUT:<br/> " + output.toString();
}
function doJoin(choice){
	var output;
	if(choice == "mixed"){
		 output = mixedArr.join(" : ");
	}else{
		output = strArr.join(" : ");
	}
	var el = document.getElementById("demo");
	el.innerHTML = "Joined OUTPUT:<br/> " + output.toString();
}
function outputArrays(){
	var el = document.getElementById("orig");
	el.innerHTML = "<h2>Original Arrays</h2>";
	el.innerHTML += "<p>Mixed Types: " + mixedArr.toString() + "</p>";
	el.innerHTML += "<p>Strings: " + strArr.toString() + "</p>";
}
</script>

Output: