Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <body>
- <form id="frm1">
- 1:
- <input type="number" name="names" max="9" min="0">
- <br> 2:
- <input type="number" name="names" max="9" min="0">
- <br> 3:
- <input type="number" name="names" max="9" min="0">
- <br> 4:
- <input type="number" name="names" max="9" min="0">
- <br> 5:
- <input type="number" name="names" max="9" min="0">
- <br> 6:
- <input type="number" name="names" max="9" min="0">
- <br> 7:
- <input type="number" name="names" max="9" min="0">
- <br>
- <br>
- </form>
- <button onclick="myFunction()">Submit</button>
- <p id="demo"></p>
- <script>
- var array = [];
- function myFunction() {
- var x = document.getElementById("frm1");
- var i;
- for (i = 0; i < x.length; i++) {
- array.push(parseInt(x.elements[i].value, 10));
- }
- document.getElementById("demo").innerHTML = array;
- console.log(f(array, 0));
- }
- //var array = [1,2,0,3,0,2,0]
- function f(array, currentPos) {
- console.log(array)
- console.log('current pos: ' + currentPos)
- stepCount = array[currentPos]
- console.log('step count: ' + stepCount)
- if (currentPos + stepCount >= array.length - 1)
- return true
- if (stepCount === 0)
- return false
- possibleSteps = array.slice(currentPos + 1, currentPos + 1 + stepCount)
- console.log("possible steps: " + possibleSteps)
- actualStep = 0
- valueOfStep = 0
- possibleSteps.forEach((element, index) => {
- if (element + index >= valueOfStep) {
- actualStep = index
- valueOfStep = element + index
- }
- });
- console.log('actual step ' + (actualStep + 1))
- console.log()
- return f(array, currentPos + actualStep + 1)
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement