Can someone please explain how this while loop works?


#1

This function returns a single number after every-other number has been removed. For instance, on the first run, from left to right- 3, 7, and 9 are removed. Then the loop goes on the next iteration from right- to left until finally, just 8 is left.

I don’t understand why only one element (number) is getting pushed to the array. How is this while loop working? Where exactly is the 'filtering taking place? Also, the .reverse function is throwing me off. How does this code know how to go left-to-right, then right-to-left multiple times?

So yeah, any help would be greatly appreciated;-) Thank you

function everyOtherFilter(values){

      while (values.length > 1){
	new_values = [];
	for (var i=1; i < values.length; i = i+2){
		new_values.push(values[i]);
		}
	values = new_values.reverse();
}
return values[0];

}
console.log(everyOtherFilter([3, 5, 7, 8, 9, 2])) = 8


#2

Is it that only one element is pushed to the array, or that only one element is returned?

Is it even necessary to reverse the array?

Here,

which we could write in simplified form,

i += 2

Every other index is skipped…


#3

Thank you for the reply.

So yes, I guess I am wondering why only one element being returned.

And I see your point where the .reverse is unnecessary; the same solution happens regardless.


#4

Is that the code that was given, or did you write the return statement,

return values[0];

?


values as referred to locally, is actually an object in the caller scope (above this function in the scope chain) and is physically bound to the object out there. When we alter or replace the local value, the global object is what gets changed.

Say for instance we pass the function a list that exists in memory.

const solmization = ['do', 're', 'mi', 'fa', 'sol', 'la', 'si', "(d'oh!)"];

everyOtherFilter(solmization);

console.log(solmization);

[
    'do',
    'mi', 
    'sol',
    'si'
]

assuming the code is correct. The return value may be of no use but that matters not since the global object is changed. We’ve filtered out every other value.


#5

That is the code that was given. But even if you return just ‘values’, it comes to [8]; there is only one element left.


#6

Given the above demo, let’s not alter the global and have the function spin off a new array. No problem, and your code pretty much covers it.

function everyOtherFilter(values) {    // assumes an array
    let result = [];
    for ( let i = 0; i < values.length; i += 2) {
        result.push(values[i]);
    }
    return result;
}

P.S. The joke above is the Homerization comprised of the preceding term, which is supposed to be ti, and the famous exclamation once he had seen his mistake.


ibid

“D’oh!” was first added to the Oxford Dictionary of English in 1998 as an Interjection with the definition “(usually [in a manner] mildly derogatory) used to comment on an action perceived as foolish or stupid.”


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.