6 kyu

Find within array

2,517 of 3,574kylehill

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 Trained6234
Total Skips591
Total Code Submissions15104
Total Times Completed3574
JavaScript Completions2517
Python Completions995
Groovy Completions36
Ruby Completions50
Total Stars68
% of votes with a positive feedback rating84% of 521
Total "Very Satisfied" Votes388
Total "Somewhat Satisfied" Votes103
Total "Not Satisfied" Votes30
Ad
Contributors
  • kylehill Avatar
  • suic Avatar
  • 10XL Avatar
  • B1ts Avatar
  • Blind4Basics Avatar
  • cliffstamp Avatar
  • albertogcmr Avatar
  • ejini战神 Avatar
Ad