Skip to content

Instantly share code, notes, and snippets.

@nikolayvitaev
Created June 19, 2018 10:39
Show Gist options
  • Save nikolayvitaev/3c2dc7791397d57e7fd84b488c780c57 to your computer and use it in GitHub Desktop.
Save nikolayvitaev/3c2dc7791397d57e7fd84b488c780c57 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/zizozevugu
<!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>
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