Basic Algorithm Scripting: Finders Keepers: Using 'do while loop'

Hi, here is my solution using a do while loop; incrementing array index ‘i’ after each logical test has been performed. :grinning:

function findElement(arr, func) {
  let i = 0;
  do {
      if( func(arr[i]) === true ) { 
        return arr[i];
      }
  } while(func(arr[i++]) === false); // increase 'i' after 'while'logic test.
  return undefined;
 }

findElement([1, 2, 3, 4], num => num % 2 === 0);

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

Ok, no problemo, I will try to observe the etiquette. :grinning:

@sudaama Can you explain why you think this solution is better then just loop based on array length?
From what I see here your checking function “func()” will be called N*2-1 times, compared to N times if you just compare to array length instead of calling function, assuming that passing element is Nth.
This means that this solution is going to take almost twice more time if func() is heavy on calculations.
Why did you decide to go with this approach?

Hi, thanks for your email. To be quite frank, the time factor was not my primary consideration. I knew that the ‘for loop’ could be used to solve the problem but why unecessarily iterate through all the indices? Anyway, after much deliberation and experimentation I realised if the index was increase after the ‘logic while’ test was executed, that would solve the prob. Regarding the doubling in execution time. I can not comment on the maths behind this as I have not studied this. Regards :wink:

You will not iterate through all the indices, because it will quit from the for loop if you call “return” for the function.
If you are not returning value of function you can just use break; statement like this:

function findElement(arr,func){
  let result = undefined;
  for (let i=0;i<arr.length;i++){
    if ( func(arr[i]) ) {
      result = arr[i];
      break;
    }
  }
  return result;
}

But since we returning result of function, this will break out of loop just the same, because return will stop function:

function findElement(arr,func){
  for (let i=0;i<arr.length;i++){
    if (func(arr[i])) return arr[i];
  }
  return undefined;
}

You also have some unnecessary code complication in your logical checks.
while and if already take “true” or “false” as an argument, so you do not need to compare it value of boolean again. In other words, you can ommit comparing something that is already boolean to another boolean like you do:
while(func(arr[i++]) === false);
this is unnecessary comparison and can be written like this instead:
while( !func(arr[i++]));
! means reverse it, meaning that it will return true if func check is false.
And same thing with if, it can be used like this:

if (true){
 somestuff();
}

Meaning that it will already check if what is between () true or no, you dont need to include that.
if( func(arr[i]) ) is same as if( func(arr[i]) === true).

Thank you for your expert and sound logic. I appreciate your in depth analysis of the problem and for taking the time to explain your reasoning :-):grinning:

This will pass the Tests here in fCC, but placing ‘console.log(num)’ after ‘if-statement’ looks like an infinite loop to me - in the console ‘num’ is counted up to over 100 if you take an array containing only odd numbers…

This one works fine:

function findElement(arr, func) {
  let num = 0;
  while ( num <= arr.length && false === func(arr[num])) {
    num++;
  }
  return arr[num];
}

console.log(findElement([1, 2, 3, 4], num => num % 2 === 0));
console.log(findElement([1, 3, 5, 8, 9, 10], num => num % 2 === 0));
console.log(findElement([1, 3, 5, 9], num => num % 2 === 0));