Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I would like to change this forloop to a while statement:

var arr3=new Array();
var arr4=new Array();

for(i=0;i<arr3.length;i++){
if(arr3[i]!=undefined && indexarr[i]!=undefined){
arr4.push(arr3[i]);
indexarr1.push(indexarr[i]); } }
Posted
Updated 5-Mar-14 12:39pm
v3
Comments
Sergey Alexandrovich Kryukov 5-Mar-14 10:33am    
What do you mean "how"? By getting familiar with syntax and doing tiny programming work. Why, by the way?
—SA

Learn to do it yourself: Comparing For and While[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Mar-14 10:42am    
Fair enough, 5ed.
—SA
Peter Leow 5-Mar-14 10:49am    
Thanks Sergey.
JavaScript
var arr3=new Array();
var arr4=new Array();
var i = 0;
 
while(i<arr3.length)
{
    if(arr3[i]!=undefined && indexarr[i]!=undefined)
    {
        arr4.push(arr3[i]);
        indexarr1.push(indexarr[i]); 
    } 
 i++;
}

Is it so ??

-KR
 
Share this answer
 
v3
Comments
petra.elhashem 7-Mar-14 16:09pm    
the i++ should be outside if as in for loop it was always incrementing regardless if the condition was true or false
Krunal Rohit 7-Mar-14 22:30pm    
Yeah, My mistake..
Thanks

-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900