Im trying to make a weird online program but im having trouble.
Here is my code:
function name() {
var titles = ["titles", "titles", "titles", "titles", "titles", "titles"];
var randFor = Math.random();
randFor = 7 * randFor;
randFor = Math.floor(randFor);
var finished = [];
var arraySpot = 0;
var newVal;
var newValSpot = 0;
newVal = finished[newValSpot];
for (i = 0; i < randFor; i++) {
var randArray = Math.random();
randArray = 7 * randArray;
randArray = Math.floor(randArray);
finished.push[arraySpot] = titles[randArray];
arraySpot++;
newValSpot++;
newVal = " " + finished[newValSpot];
}
document.getElementById("gen").innerHTML =newVal;
}
Basically, what im trying to do is take an array of words, and convert it to a variable and display it in this tag:
<div id="gen"></div>
But every time it gives me "undefined" with a space in front of it?
Any help is appreciated, sorry for being such a noob in JS.
UPDATE I have revised the code, i no longer get "undefined" but now i get nothing?
function name() {
var titles = ["titles", "titles", "titles", "titles", "titles", "titles"];
var randFor = Math.random();
randFor = 7 * randFor;
randFor = Math.floor(randFor);
var newVal;
newVal = title[0];
for (i = 0; i < randFor; i++) {
var randArray = Math.random();
randArray = 7 * randArray;
randArray = Math.floor(randArray);
newVal = " " + titles[randArray];
}
document.getElementById("gen").innerHTML = newVal;
}