6 kyu

Find within array

2,524 of 3,595kylehill

Description:

We'll create a function that takes in two parameters:

  • a sequence (length and types of items are irrelevant)
  • a function (value, index) that will be called on members of the sequence and their index. The function will return either true or false.

Your function will iterate through the members of the sequence in order until the provided function returns true; at which point your function will return that item's index.

If the function given returns false for all members of the sequence, your function should return -1.

var trueIfEven = function(value, index) { return (value % 2 === 0) };
findInArray([1,3,5,6,7], trueIfEven) // should === 3
true_if_even = lambda value, index: value % 2 == 0
find_in_array([1,3,5,6,7], true_if_even) # --> 3
isEven = { value, index -> value % 2 == 0 }
Kata.findInArray([1,3,5,6,7], isEven) == 3
true_if_even = lambda { |value, index| value.even? }
find_in_array([1,3,5,6,7], true_if_even) # --> 3
Arrays
Functional Programming
Fundamentals

Stats:

CreatedJul 25, 2013
PublishedJul 26, 2013
Warriors Trained6270
Total Skips591
Total Code Submissions15167
Total Times Completed3595
JavaScript Completions2524
Python Completions1006
Groovy Completions37
Ruby Completions53
Total Stars70
% of votes with a positive feedback rating84% of 523
Total "Very Satisfied" Votes389
Total "Somewhat Satisfied" Votes104
Total "Not Satisfied" Votes30
Ad
Contributors
  • kylehill Avatar
  • suic Avatar
  • 10XL Avatar
  • B1ts Avatar
  • Blind4Basics Avatar
  • cliffstamp Avatar
  • albertogcmr Avatar
  • ejini战神 Avatar
Ad