You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sorting of an array of numbers using selection sort algorithm
vararray=[];functiongetArrayFromUser(){alert("Enter the elements of the array, separated by commas:");varinput=prompt();varelements=input.split(",");for(vari=0;i<elements.length;i++){array.push(elements[i]);}}functionsortArray(){varsortedArray=[];while(array.length>0){varminValue=array[0];varminIndex=0;for(vari=1;i<array.length;i++){if(array[i]<minValue){minValue=array[i];minIndex=i;}}sortedArray.push(minValue);array.splice(minIndex,1);}returnsortedArray;}getArrayFromUser();varsortedArray=sortArray();console.log("The sorted array is: "+sortedArray);
Identifying a number as prime or not
functionisPrime(n){for(vari=2;i<=Math.sqrt(n);i++){if(n%i===0){returnfalse;}}returntrue;}functiongetNumberFromUser(){alert("Enter a number:");varinput=prompt();returnparseInt(input);}varnumber=getNumberFromUser();varisPrime=isPrime(number);if(isPrime){alert(number+" is a prime number");}else{alert(number+" is not a prime number");}
About
A mini JavaScript Compiler to compile JavaScript code online. It is a free tool to compile JavaScript code online.