➕︎ Aa ➖︎ |Dark 1 ● Dark 2 ● Light|Hack ● Fira Code|Back to PT Mono✖
// CPCS 223 Analysis & Design of Algorithms
// Javascript demo 1 - global scope code
// 2020, Dr. Muhammad Al-Hashimi
// code defined outside functions
// use var keyword to create a global variable
var xdata = [35,82,97,10,27]; // create/intialize an array
var size = xdata.length; // .length gets array size, as expected
var i, j;
var temp;
// don't need to declare a type for variables
// they are auto typed depending on what you put in them
for (i = 0; i < size-1; i++)
for (j = size-1; j > i; j--)
if ( xdata[j] < xdata[j-1] )
{
temp = xdata[j];
xdata[j] = xdata[j-1];
xdata[j-1] = temp;
}
// output sorted list to browser (instead of console
// or a window-based system)
// simply print array directly like a variable
document.write( "Output sorted array: ", xdata );