I try to write a function (called: tally) using recursion (part of the exercise) to scan through an array of numbers and return an object with the numbers as key and the number of instances as value.
Example:
tally([2,3,4,5,5,5,5,5,5,5,6,7,,6,7,6,7,5,4,3,4,5,5,6])
//{2: 1, 3: 2, 4: 3, 5: 10, 6: 4, 7: 3}
I created the framework but i am not sure about the syntax to make it work:
function tally(arr) {
var obj = {}
if (/*check if object ('obj') has a key corresponding to the array element*/) {
//increase key's value by onee
} else {
//add key with value of 1
}
return obj
};
Any hints to complete the recursion function above? Please try to stick to my structure in your answers as much as possible since this is part of an exercise.
objon each call, and can thetallyfunction only accept that onearrargument, or can those be changed?tally.