'How can I fill my array in JavaScript depending on user input?
How can I fill my array depending on user input instead of changing this array manualy in JavaScript?
How can I link my form in HTML to my javaScript ?
let move = ["L", "L", "R", "F", "N"];
let lastElement = move.slice(-1);
console.log(lastElement);
function finalPosition(move) {
let countUp = 0, countDown = 0;
let countLeft = 0, countRight = 0;
let l = move.length;
for (let i = 0; i < l; i++) {
if (move[i] == 'F')
countUp++;
else if (move[i] == 'B')
countDown++;
else if (move[i] == 'L')
countLeft++;
else if (move[i] == 'R')
countRight++;
}
document.write("Final Position: (" + (countRight - countLeft) + ", " + (countUp - countDown) + "," + lastElement + ")");
}
finalPosition(move);
Solution 1:[1]
Ok two questions in one:
-
How can I fill my array depending on user input instead of changing this array manualy in JavaScript?
Use
.push()
to add a value to your Array. For more info see here. -
How can I link my form in HTML to my javaScript?
Add an id to the form.
Then load the form with:
const theForm = document.getElementById("theFormId");
Then you can work with the form in JavaScript.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | RobC |