Created
June 19, 2018 10:39
-
-
Save nikolayvitaev/3c2dc7791397d57e7fd84b488c780c57 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/zizozevugu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var fruits=["Apples", "Oranges", "Grapes", "Bananas"] | |
var fruitweight=[2, 3, 1, 20]; //weight of each element above | |
var totalweight=eval(fruitweight.join("+")); //get total weight (in this case, 10) | |
var weighedfruits=new Array(); //new array to hold "weighted" fruits | |
var currentfruit=0 | |
while (currentfruit<fruits.length){ //step through each fruit[] element | |
for (i=0; i<fruitweight[currentfruit]; i++) | |
weighedfruits[weighedfruits.length]=fruits[currentfruit] | |
currentfruit++ | |
} | |
var randomnumber=Math.floor(Math.random()*totalweight) | |
document.write(weighedfruits[randomnumber]) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var fruits=["Apples", "Oranges", "Grapes", "Bananas"] | |
var fruitweight=[2, 3, 1, 20]; //weight of each element above | |
var totalweight=eval(fruitweight.join("+")); //get total weight (in this case, 10) | |
var weighedfruits=new Array(); //new array to hold "weighted" fruits | |
var currentfruit=0 | |
while (currentfruit<fruits.length){ //step through each fruit[] element | |
for (i=0; i<fruitweight[currentfruit]; i++) | |
weighedfruits[weighedfruits.length]=fruits[currentfruit] | |
currentfruit++ | |
} | |
var randomnumber=Math.floor(Math.random()*totalweight) | |
document.write(weighedfruits[randomnumber])</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fruits=["Apples", "Oranges", "Grapes", "Bananas"] | |
var fruitweight=[2, 3, 1, 20]; //weight of each element above | |
var totalweight=eval(fruitweight.join("+")); //get total weight (in this case, 10) | |
var weighedfruits=new Array(); //new array to hold "weighted" fruits | |
var currentfruit=0 | |
while (currentfruit<fruits.length){ //step through each fruit[] element | |
for (i=0; i<fruitweight[currentfruit]; i++) | |
weighedfruits[weighedfruits.length]=fruits[currentfruit] | |
currentfruit++ | |
} | |
var randomnumber=Math.floor(Math.random()*totalweight) | |
document.write(weighedfruits[randomnumber]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment