+ 1
Weekend Challenge: Randomly put array elements in containers [JavaScript]
You have: --> 4 HTML divs; --> an array like this ["solo", "learn", "coding", "future"]. Make a program that: => puts each word in one of the div elements randomly; => add the class "special" to the div that contains it.
3 Answers
+ 6
what do you mean mark as special? do you mean <mark></mark>?
+ 4
var a= "<div>";
var b= "</div><div>";
var c= "</div>";
function shuffle(arra1) { var ctr = arra1.length, temp, index; while (ctr > 0) {
index = Math.floor(Math.random() * ctr);
ctr--;
temp = arra1[ctr]; arra1[ctr] = arra1[index]; arra1[index] = temp; } return arra1; }
var myArray = ["solo", "learn", "coding", "future"];
var seq=shuffle(myArray);
document.write(a+seq[0]+b+seq[1]+b+seq[2]+b+seq[3]+c);
var where= seq.indexOf("future");
var tar=document.getElementsByTagName("div")[where];
tar.className="special";
+ 2
No not at all...It's my fault, I wasnt clear...
Now you get it??