Java Script one-liners

1. alert(20-2*Math.pow(3,2)+4/2*3);
2. alert(Math.sqrt(2));
3. var x = 3; var y = x*x + 2*x + 10; alert(y);
4. alert("Tärningen är kastad!\nUtfall: " + Math.floor(6*Math.random()+1));
5. alert(true && false);
6. alert(false || true);
7. alert(!false);
8. 
var karnstridsspetsar=true;
if(karnstridsspetsar) 
{ 
  alert("Ligg ner & njut!"); 
} 
else 
{ 
alert("Gå till jobbet."); 
}

var karnstridsspetsar=true; if(karnstridsspetsar) { alert("Ligg ner & njut!"); } else { alert("Gå till jobbet."); }

9. 
var lisa=170, sophie=150; 
if(lisa > sophie) 
{ 
  alert("Sophie är skyldig Lisa " + (lisa - sophie)/2 + " kr"); 
} 
else 
{ 
  alert("Lisa är skyldig Sophie " + (sophie - lisa)/2 + " kr"); 
}

var lisa=170, sophie=150; if(lisa > sophie) { alert("Sophie är skyldig Lisa " + (lisa - sophie)/2 + " kr"); } else { alert("Lisa är skyldig Sophie " + (sophie - lisa)/2 + " kr"); }

10. 
var lisa=parseFloat(prompt("Lisa lagt ut:", 0)), sophie=parseFloat(prompt("Sophie lagt ut:", 0)); 
if(lisa > sophie) 
{ 
  alert("Sophie är skyldig Lisa " + (lisa - sophie)/2 + " kr"); 
} 
else 
{ 
  alert("Lisa är skyldig Sophie " + (sophie - lisa)/2 + " kr"); 
}

var lisa=parseFloat(prompt("Lisa lagt ut:", 0)), sophie=parseFloat(prompt("Sophie lagt ut:", 0)); if(lisa > sophie) { alert("Sophie är skyldig Lisa " + (lisa - sophie)/2 + " kr"); } else { alert("Lisa är skyldig Sophie " + (sophie - lisa)/2 + " kr"); }

11. var namn=prompt("Vad heter du?", ""); alert("HEJ " + namn.toUpperCase() + "!");

12. 
var s = "", y, x = 0; 
while(x <= 2) 
{ 
  y = x*x+2*x+10; s += x + ", " + y + "\n"; 
  x += 0.1; 
}; 
alert(s);

var s = "", y, x = 0; while(x <= 2) { y = x*x+2*x+10; s += x + ", " + y + "\n"; x += 0.1; }; alert(s);

13. 
var s = "", y, x; 
for(x=0; x <= 2; x += 0.1) 
{ 
  y = x*x+2*x+10; 
  s += x + ", " + y + "\n"; 
}; 
alert(s);

var s = "", y, x; for(x=0; x <= 2; x += 0.1) { y = x*x+2*x+10; s += x + ", " + y + "\n"; }; alert(s);

14. 
var s = ""; 
for(var i=0, y, x=0; i <= 20; ++i, x = i/10) 
{ 
  y = x*x+2*x+10; 
  s += x + ", " + y + "\n"; 
}; 
alert(s);

var s = ""; for(var i=0, y, x=0; i <= 20; ++i, x = i/10) { y = x*x+2*x+10; s += x + ", " + y + "\n"; }; alert(s);

15. 
var s = "", expr=prompt("Uttryck:", "x*x+2*x+10"); 
for(var i=0, y, x=0; i <= 20; ++i, x = i/10) 
{ 
  y = eval(expr); s += x + ", " + y + "\n"; 
}; 
alert(s);

var s = "", expr=prompt("Uttryck:", "x*x+2*x+10"); for(var i=0, y, x=0; i <= 20; ++i, x = i/10) { y = eval(expr); s += x + ", " + y + "\n"; }; alert(s);

16. 
for(var i=10; i>0; --i) 
  alert(i); 
alert("BOOM!");

for(var i=10; i>0; --i) alert(i); alert("BOOM!");

17. 
var s="Multiplikationstabellen:\n"; 
for(var i=0; i < 10; ++i) 
{ 
  for(var j=0; j < 10; ++j) 
    s += i + "*" + j + "=" + i*j + " "; 
  s += "\n"
} 
alert(s);

var s="Multiplikationstabellen:\n"; for(var i=0; i < 10; ++i) { for(var j=0; j < 10; ++j) s += i + "*" + j + "=" + i*j + " "; s += "\n"} alert(s);

18. 
function y(x) 
{ 
   return x*x+2*x+10; 
} 
var x=0; 
while(x != null && x.length != 0) 
  x = prompt("y(" + x + ") = " + y(x) + ", new x or Cancel to quit:", "");

function y(x) { return x*x+2*x+10; } var x=0; while(x != null && x.length != 0) x = prompt("y(" + x + ") = " + y(x) + ", new x or Cancel to quit:", "");

19. 
function rndHex() 
{ 
  return Math.floor(256*Math.random()).toString(16); 
} 

function rndBgColor() 
{ 
  document.bgColor = "#" + rndHex() + rndHex() + rndHex(); 
} 
rndBgColor();

function rndHex() { return Math.floor(256*Math.random()).toString(16); } function rndBgColor() { document.bgColor = "#" + rndHex() + rndHex() + rndHex(); } rndBgColor();

20. 
var colors = new Array("Red", "Green", "Blue", "Cyan", "Magenta", "Yellow", "Black"), 
    color = colors[Math.floor(colors.length*Math.random())]; 
alert(color + " is next..."); 
void(document.bgColor = color);

var colors = new Array("Red", "Green", "Blue", "Cyan", "Magenta", "Yellow", "Black"), color = colors[Math.floor(colors.length*Math.random())]; alert(color + " is next..."); void(document.bgColor = color);


Copyright © 1998 by Mikael Bonnier, Lund, Sweden