Javascript variable names in loop -
I have a function that creates 6 HTML input boxes to store my score values, the total score box 1 of
var hole 1 = prompt ("enter 1 end"); Var Hole 2 = Prompt ("Enter Hole 2 score"); Var Hole 3 = Prompt ("Enter Hole 3 Score"); Var Hole 4 = Prompt ("Enter Hole 4 Score"); Var Hole 5 = Prompt ("Enter 5 Ends"); The complete work of addScore ():
addScore function (var ni = document.getElementById ('myDiv'); Var numi = document.getElementById ('theValue'); Var num = (document.getElementById ('theValue'). Value -1) + 2; Numi.value = num; Var newdiv = document.createElement ('div'); Var divIdName = 'my' + number + 'div'; Newdiv.setAttribute ('id', divIdName); // Prompts the user for his score Distinguish 1 = Prompt ("Enter 1 End"); Var Hole 2 = Prompt ("Enter Hole 2 score"); Var Hole 3 = Prompt ("Enter Hole 3 Score"); Var Hole 4 = Prompt ("Enter Hole 4 Score"); Var Hole 5 = Prompt ("Enter 5 Ends"); // var masksocco box = '& lt; Input type = "text" placeholder = "hole 1" id = "hole1" /> & Lt; Input type = "text" placeholder = "hole 2" id = "holes 2" /> & Lt; Input type = "text" placeholder = "hole 3" id = "hole 3" /> & Lt; Input type = "text" placeholder = "hole 4" id = "hole 4" /> & Lt; Input type = "text" placeholder = "hole 5" id = "hole 5" /> & Lt; Input type = "text" placeholder = "total score" id = "total score" /> newdiv.innerHTML = mixer box; Ni.appendChild (newdiv); // Adds all the scores recorded through the hole's total score = parseint (perfotto (holes 1) + parsflot (hole 2) + parsflot (hole 3) + parsflot (hole 4) + parsflight (hole 5)); // In the index index.html addTotal = document.getElementById ('totalscore'), the input boxes of each data value from the total data. Value = totalScore; Addhole1 = document.getElementById ('Hole 1'). Value = hole 1; // takes a quick number used in all holes * variable addhole2 = document.getElementById ('holes2'). Value = hole2; Addhole3 = document.getElementById ('Hole 3'). Value = hole 3; Addhole4 = document.getElementById ('holes4'). Value = hole4; Addhole5 = document.getElementById ('holes5'). Value = hole 5; } // adds all the scores recorded through the hole's total score = parseint (parseflat (holes 1) + parcephlot (hole 2) + parceflot (hole 3) + parcephlot (hole 4) + parcephlot (hole 5)); And it's called index.html:
& lt; Center & gt; & Lt; P & gt; & Lt; A href = "javascript:;" Onclick = "addScore ();" & Gt; & Lt; H1 & gt; Add score & lt; / H1> & Lt; / A & gt; & Lt; / P & gt; How do I name the unique variable name for the hole provincial values (hole 1, hole 2, hole 3, hole 4 etc.) and HTML input box (hole 1, hole 2, hole 3) Can I generate?
Here's an example of what can be done with Array:
var holes = []; (Var i = 1; i & lt; = 5; i ++) {holes.push (prompt ("enter holes + +" + "score:"));} for var totalScore = 0; Var i = 0; i & lt; hole.lumb; i ++) {totalScore + = parseInt (hole [i], 10);} warning ("total score:" + total score); demo:
Comments
Post a Comment