I am new to JavaScript and I would like to get three random elements out of my array, which need to be different. I already managed to get three different elements, then I tried to take one element only once via the split method. But apparently this doesn't really work, there are probably a lot of mistakes since this is one of my first scripts. Also it sometimes says "undefined".
/
HTML:
<span id="tot1"></span>, <span id="tot2"> und </span> und <span id="tot3"></span>
Javascript:
function getNumber() {
random = Math.floor(Math.random() * students.length);
students.splice(random,1);
return random;
}
students = new Array("Paul", "Jan", "Fabian D.", "Fabian K.", "Lennard",
"Robin", "Angelique", "Joyce", "Sarah", "Ajlin",
"Enes", "Leon", "Boran", "Joshua")
getNumber();
tot1 = students[random];
getNumber();
tot2 = students[random];
getNumber();
tot3 = students[random];
document.getElementById('tot1').innerHTML = tot1;
document.getElementById('tot2').innerHTML = tot2;
document.getElementById('tot3').innerHTML = tot3;
I am new to JavaScript and I would like to get three random elements out of my array, which need to be different. I already managed to get three different elements, then I tried to take one element only once via the split method. But apparently this doesn't really work, there are probably a lot of mistakes since this is one of my first scripts. Also it sometimes says "undefined".
http://jsfiddle/t4mtpm50/
HTML:
<span id="tot1"></span>, <span id="tot2"> und </span> und <span id="tot3"></span>
Javascript:
function getNumber() {
random = Math.floor(Math.random() * students.length);
students.splice(random,1);
return random;
}
students = new Array("Paul", "Jan", "Fabian D.", "Fabian K.", "Lennard",
"Robin", "Angelique", "Joyce", "Sarah", "Ajlin",
"Enes", "Leon", "Boran", "Joshua")
getNumber();
tot1 = students[random];
getNumber();
tot2 = students[random];
getNumber();
tot3 = students[random];
document.getElementById('tot1').innerHTML = tot1;
document.getElementById('tot2').innerHTML = tot2;
document.getElementById('tot3').innerHTML = tot3;
Share
Improve this question
edited Nov 23, 2014 at 17:43
user57508
asked Nov 23, 2014 at 16:39
Simon MathewsonSimon Mathewson
7316 silver badges20 bronze badges
6
- 1 tot1 = students[random] when that statement hits, random is undefined. random is a local variable inside the getNumber function but once that function exits, that local variable is no longer accessible. Should be tot1 = students[getNumber()]; or var RandomNumber = getNumber(); tot1 = students[RandomNumber]; – frenchie Commented Nov 23, 2014 at 16:45
-
1
@frenchie Without a
var
or similar keyword to declarerandom
as local, it will leak out as a global. stackoverflow./questions/1470488 – Jonathan Lonowski Commented Nov 23, 2014 at 17:03 -
normally you access
return
variables like this:var my_return_var = getNumber();
like @JonathanLonowski mentioned, your vars behave like global vars which can easily lead to problems in larger codes. – low_rents Commented Nov 23, 2014 at 17:10 - if my solution doesnt work for you , please let me know. – ProllyGeek Commented Nov 23, 2014 at 17:12
- 1 Wele to StackOverflow! Please see "Should questions include “tags” in their titles?", where the consensus is "no, they should not". – user57508 Commented Nov 23, 2014 at 17:43
4 Answers
Reset to default 7By using .splice()
, the randomly selected name is actually being removed before it's retrieved, making random
instead refer to the next name in the collection:
var students = [ "Paul", "Jan", "Fabian D." ];
var random = 1;
console.log(students[random]); // "Jan"
students.splice(random, 1);
console.log(students); // [ "Paul", "Fabian D." ]
console.log(students[random]); // "Fabian D."
The "it sometimes says 'undefined'" happens when random
tries to refer to the last element:
var students = [ "Paul", "Jan", "Fabian D." ];
var random = 2;
console.log(students[random]); // "Fabian D."
students.splice(random, 1);
console.log(students); // [ "Paul", "Jan" ]
console.log(students[random]); // undefined, random is now out-of-bounds at 2
You could put to use the return value of .splice()
, which is a collection of the removed elements, redefining getNumber()
to instead return an element rather than an index:
function getStudent() {
var random = Math.floor(Math.random() * students.length);
return students.splice(random, 1)[0]; // return the 1st and only removed element
}
var tot1 = getStudent(); // "Fabian D."
var tot2 = getStudent(); // "Enes"
var tot3 = getStudent(); // "Joyce"
http://jsfiddle/6gox6L1t/
I think this would be a nicer, cleaner and shorter solution:
function getStudent(students) {
var random = Math.floor(Math.random() * (students.length));
var my_student = students.splice(random,1);
return my_student;
}
var students = new Array("Paul", "Jan", "Fabian D.", "Fabian K.", "Lennard",
"Robin", "Angelique", "Joyce", "Sarah", "Ajlin",
"Enes", "Leon", "Boran", "Joshua");
document.getElementById('tot1').innerHTML = getStudent(students);
document.getElementById('tot2').innerHTML = getStudent(students);
document.getElementById('tot3').innerHTML = getStudent(students);
you pass the current students-array to the getStudent()
function so you avoid having global variables in your script.
splice
returns the element removed, so you got your student-name there and return it.
edit2: I was wrong about the -1
on the .length
. 1
is an exclusive result of Math.random()
, so you have been right with that.
http://jsfiddle/northkildonan/38rf42f7/1/
edit: this method pletely avoids using global variables in your script.
try this :
students = new Array("Paul", "Jan", "Fabian D.", "Fabian K.", "Lennard", "Robin", "Angelique", "Joyce", "Sarah", "Ajlin",
"Enes", "Leon", "Boran", "Joshua")
var arr = []
while(arr.length < 3){
var randomnumber=Math.ceil(Math.random()*students.length-1)
var found=false;
for(var i=0;i<arr.length;i++){
if(arr[i]==randomnumber){found=true;break}
}
if(!found)arr[arr.length]=randomnumber;
}
console.log(arr.valueOf());
tot1 = students[arr[0]];
tot2 = students[arr[1]];
tot3 = students[arr[2]];
document.getElementById('tot1').innerHTML = tot1;
document.getElementById('tot2').innerHTML = tot2;
document.getElementById('tot3').innerHTML = tot3;
please note that length should be decrmented by 1 , because index starts at 0.
http://jsfiddle/t4mtpm50/6/
Another option is to shuffle the whole list of students and then pick the first three from the shuffled set. (The shuffle function here is adapted from https://stackoverflow./a/6274398/567595.)
function shuffle(array) {
var i, temp, j;
for (i = array.length; i; i--) {
j = Math.floor(Math.random() * i);
temp = array[i - 1];
array[i - 1] = array[j];
array[j] = temp;
}
}
students = ["Paul", "Jan", "Fabian D.", "Fabian K.", "Lennard",
"Robin", "Angelique", "Joyce", "Sarah", "Ajlin",
"Enes", "Leon", "Boran", "Joshua"];
shuffle(students);
document.getElementById('tot1').innerHTML = students[0];
document.getElementById('tot2').innerHTML = students[1];
document.getElementById('tot3').innerHTML = students[2];
http://jsfiddle/4o897kwa/1