Q:

Getting "TypeError: undefined is not a function" with functional programming

I'm trying to solve the following issue using nested functions, so the result I am looking for is 11, but instead it is coming up as an error that the countWordsInReduce function is undefined.  That function works fine by itself, but for some reason when using it with the reduce function I have, there is an issue.  Any idea how I would use this correctly inside the reduce function?  Any help would be appreciated.
function reduce(array, start, func){
    current = start;
    for (var i = 0; i < array.length; i++){
        current = func(current, array[i]);
    }
    return current;
}

var countWordsInReduce = function(array, start){
    var count = start;  
    count +=  array.join(", ").split(" ").length;
    return count;
}

word_array = ["hello there this is line 1", "and this is line 2"];
reduce(word_array, 0, countWordsInReduce)

A:

Here is a working version I got after messing around with it a little. The problem is that you were passing the current array index to your countWordsInReduce function.
What the countWordsInReduce function should actually do is accept as the first parameter the next element of the array, and the second parameter is the current running total.
So the first time you call countWordsInReduce, you are passing the first string, with a running total of 0. The second time you call it, you are passing the second string, with a running total of 6. And then it will add the length of the second string to that and come out with the answer of 11.
So basically your reduce function is looking at the array as a whole, and the countWordsInReduce function is just processing it piece by piece.
function reduce(array, start, func) {
    var current = start;
    for (var i = 0; i < array.length; i++) {
        current = func(array[i], current);
    }
    return current;
}

var countWordsInReduce = function (element, base) {
    var count = base;
    count += element.split(" ").length;
    return count;
};

var word_array = ["hello there this is line 1", "and this is line 2"];
reduce(word_array, 0, countWordsInReduce);

