Evertjan.
3/23/2015 8:58:00 PM
"Evertjan." <exxjxw.hannivoort@inter.nl.net> wrote on 23 mrt 2015 in
comp.lang.javascript:
> "Evertjan." <exxjxw.hannivoort@inter.nl.net> wrote on 23 mrt 2015 in
> comp.lang.javascript:
>
>> jonas.thornvall@gmail.com wrote on 23 mrt 2015 in comp.lang.javascript:
>>
>>>> ========================
>>>> var a = [3,0,7,0,1,0,0,0]
>>>>
>>>> while (a[a.length-1]==0) a.pop();
>>>>
>>>> alert(a);
>>>> ========================
>>>>
>>> Thank you, i will try.
>>
>> Since pop() returns what is popped,
>> you could try this less efficient,
>> but perhaps also enjoyable way:
>>
>> ====================
>> var a = [3,0,7,0,1,0,0,0];
>> var b = a.slice(); // copy array a to b
>>
>> while (b.pop() == 0) {a.pop()};
>>
>> alert(a);
>> ====================
>
> Or push the popped nonzero element back:
>
> =================
> var a = [3,0,7,0,1,0,0,0], temp;
>
> while ( (temp = a.pop()) == 0) {};
>
> a.push(temp);
>
> alert(a);
> =================
Or you could use regex,
if the values are single digits:
===============
var a = [1,0,4,0,1,0,0,0];
a = a.join('').replace(/0*$/,'').split('');
alert(a);
===============
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)