For loop Searching ID's Javascript -
For loop Searching ID's Javascript -
okay little baffled one, i'll show code first , explain.
var number = 1; var maxguess = 6; /*make array of possible words*/ var arr = ["horse", "cow", "blueberry", "apples", "time", "hollow", "pumpkin", "telephone", "computer", "calculator", "tower", "castle"]; /*write shuffler code*/ function shuffle(array) { var currentindex = array.length, temporaryvalue, randomindex ; // while there remain elements shuffle... while (0 !== currentindex) { // pick remaining element... randomindex = math.floor(math.random() * currentindex); currentindex -= 1; // , swap current element. temporaryvalue = array[currentindex]; array[currentindex] = array[randomindex]; array[randomindex] = temporaryvalue; } homecoming array; } /*shuffle array*/ shuffle(arr); /*make storage array user entry*/ var gstore = []; /*get first entry in array, create string, , remove commas*/ var teme = arr.slice(0, 1).tostring().replace(/,/g,''); console.log(teme); (var = 0; <= 9; i++) { if (i > teme.length){ document.getelementbyid(i).innerhtml="x"; } } /*listen button click*/ document.getelementbyid('sendit').addeventlistener('click', process); /*function 'process'*/ function process() { /*get guess user*/ var guess = document.getelementbyid('guess').value.tostring().tolowercase(); /*check index of guess*/ if (gstore.indexof(guess, 0) == -1){ /*store guess*/ gstore.push(guess); /*make storage array letter indicies*/ var indices = []; /*find indicies of guess*/ for(var i=0; i<teme.length;i++) { if (teme[i] === guess) { document.getelementbyid(i).innerhtml = guess.tostring(); } } number++; } if (gstore.indexof(guess, 0) != -1){ } }
alright, hangman game making in class. near bottom of js can see wrote following-
for(var i=0; i<teme.length;i++) { if (teme[i] === guess) { document.getelementbyid(i).innerhtml = guess.tostring(); } }
now, gives me absolutely no errors in js console if leave is. now, error when @ line saying
document.getelementbyid(i).innerhtml="";
when have outside button click function here
console.log(teme); (var = 0; <= 9; i++) { if (i > teme.length){ document.getelementbyid(i).innerhtml=""; } }
to specific, error states
[error] typeerror: null not object (evaluating 'document.getelementbyid(i).innerhtml=" "') global code (clar.js, line 31)
now if throwing error here, worth changing code in location or did not in both instances though sec instance works?
all help much appreciated, , javascript teacher has no thought can input variable when writing
document.getelementbyid(variablename)
so literally of no help me, , find nice able without framework
edit: sorry, forgot html, should help clear air doing
<html> <head> </head> <body> <input type="text" name="" value="" id="guess" /><br> <input type="button" name="" value="submit" id="sendit" /><br /> <script src="./clar.js" type="text/javascript"></script> <span id="0"></span><span id="1"></span><span id="2"></span><span id="3"></span><span id="4"></span><span id="5"></span><span id="6"></span><span id="7"></span><span id="8"></span><span id="9"></span> </body> </html>
your sec loop accessing item out of bounds , indexer bringing null object.
change from:
for (var = 0; <= teme.length; i++)
to:
for (var = 0; < teme.length; i++)
javascript loops dom
Comments
Post a Comment