Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't get why you embedded the arrays three deep. I cleaned up some of the code and made the names more understandable (at least to me).</p> <p>Regardless, when you were on the last value in the array, count++ didn't exist. Also, don't use count++ as this will increment your count var. Don't use ++ to simplify unless you truly know what you're doing and want to increment. Also, tricky shortcuts will confuse people trying to read your code, so try to be as explicit as possible. (There are exceptions to this statement, as in, you don't need to write for a person who has never coded before)</p> <p>Here is working javascript:</p> <pre><code>var result = new Array(); var count = 0; function move(direction) { if(direction == 'next') { var box1 = document.getElementById("txt1").value; //why did you wrap these as arrays? var box2 = document.getElementById("txt2").value; // if (result.length == count){ if (box1 == '' || box2 == '') { alert('you need to put in a value'); } else { result.push([box1, box2]); //why did you wrap individual numbers in arrays? document.getElementById("txt1").value = ''; document.getElementById("txt2").value = ''; } } else { try{ result[count][0] = document.getElementById("txt1").value; result[count][1] = document.getElementById("txt2").value; if(result[count+1]) { // need this because if on last value in the array, count+1 will not exist yet document.getElementById("txt1").value = result[count+1][0]; //do not do ++. this will increment count here. don't be tricky with ++ document.getElementById("txt2").value = result[count+1][1]; //because it will confuse others and lead to off by 1 errors } else { document.getElementById("txt1").value = ''; document.getElementById("txt2").value = ''; } } catch(err) { alert(err.description); } } count++; } if (direction == 'prev') { if(count &lt;= 0){ alert("no more elements"); } else { var prev_val1 = result[count - 1][0]; var prev_val2 = result[count - 1][1]; document.getElementById("txt1").value = prev_val1; document.getElementById("txt2").value = prev_val2; count--; } } document.getElementById("txtresult").value = result; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload