Sum of Found Indexes

Published by bangyen in

Create a function which takes in an array of numbers and a number to find. Return the sum of every index in the array which matches the chosen number.

Examples

sum_found_indexes([0, 3, 3, 0, 0, 3], 3) ➞ 8
# The number 3 was found at indexes 1, 2 and 5.
# 8 = 1 + 2 + 5

sum_found_indexes([1, 2, 3, 4, 5, 6], 3) ➞ 2

sum_found_indexes([100, 100, 100, 100, 100], 100) ➞ 10

sum_found_indexes([5, 10, 15, 20], 2) ➞ 0

Notes

0 can be the result if no number in the list matches or if the only matching element is at index 0.

Watch a quick demo on how Edabit works.