Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit</strong>: You've changed the code so that there is no <code>i</code> anymore, so that part of the answer below doesn't apply anymore. But what you've said about the code that you've presented is simply not credible in two different ways:</p> <ol> <li><p>You've said that <code>prev[1] = man_val;</code> followd by <code>alert(prev[1]);</code> shows <code>undefined</code>, but that's simply not true of the code you've shown.</p></li> <li><p>You've said that <code>prev.push</code> causes an error saying <code>push</code> is not a function, but in the code you've shown, <code>prev</code> is definitely an array, and arrays definitely have <code>push</code>.</p></li> </ol> <p>My guess is that you have something you haven't shown which is either shadowing <code>prev</code> or assigning a non-array to it. But as you haven't shown that code, what you say about the code simply doesn't jibe with the code.</p> <p>Hopefully the below is of some use in whatever your actual situation is.</p> <hr> <blockquote> <p><code>count += 1;</code></p> <p><code>prev[1] = man_val;</code></p> <p><code>alert(prev[i]);// Problem is here</code></p> </blockquote> <p>You're using <code>i</code> there, whereas in the line above you're using <code>1</code>. You haven't shown any code declaring or initializing <code>i</code>, so we have no idea what value it may have, but apparently it doesn't have the value <code>1</code>.</p> <p>If you really want to push to the array, the usual thing would be to use <code>push</code> or assign to <code>prev[prev.length]</code>. So you rcode could be:</p> <pre><code>prev.push(man_val); alert(prev[prev.length - 1]); </code></pre> <p>or</p> <pre><code>prev[prev.length] = man_val; // This will increment `prev.length` alert(prev[prev.length - 1]); // ...which is why we're using -1 here </code></pre> <p>But if you do have an <code>i</code> (and you must somewhere, or trying to read a value from it like that would cause a <code>ReferenceError</code>) and you want to be using it, you want to be consistent with how you use it. For instance:</p> <pre><code>prev[i++] = man_val; alert(prev[i - 1]); </code></pre> <p>I'm assuming <code>i</code> starts at <code>0</code> there.</p>
 

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