Sam
Sam

Reputation: 35

Recursion with names

I'm a little lost in general, but if someone could please briefly explain why this code works that would be great!

// Our array of messy words
var capitals = ["berlin", "parIs", "MaDRiD"];

// Capitalize function
function capitalize(word) {
    return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}

// Our recursive function
function fixLetterCase(array, i) {
    // Base case
    if (i === array.length) {
        return;
    }
    // Action
    array[i] = capitalize(array[i]);
    // Recursive case
    return fixLetterCase(array, i + 1);
}

// Here is our function call
fixLetterCase(capitals, 0);

console.log(capitals);

Upvotes: 0

Views: 252

Answers (6)

Dan Tao
Dan Tao

Reputation: 128337

I assume you're talking about the recursive part. It's really just an exotic way to iterate over the array.

Conventionally you might do this with a for loop, following the pattern (forgive the totally weird pseudo-code, my own gross invention):

for ([initialization]; [condition]; [modification])
  [function]

With recursion you can do it as:

[function [initialization]]
  if [!condition]
    break
  [function [modification]]

I personally feel there's not much benefit to the recursive version in this case, as it's much less idiomatic and thus less obvious how it works; and it offers no performance benefit.

Upvotes: 1

James Hill
James Hill

Reputation: 61812

While I understand this is not an answer to the question, I thought it might help to see a non-recursive version of the logic:

// Our array of messy words
var capitals = ["berlin", "parIs", "MaDRiD"];

// Simple function to capitalize word
function fixLetterCase(array) {
    for(var i = 0; i < array.length; i++) {
        array[i] = array[i].charAt(0).toUpperCase() + array[i].slice(1).toLowerCase();
    }
}

fixLetterCase(capitals);
console.log(capitals);​

Here's a working fiddle to play with.

Upvotes: 1

Dennis
Dennis

Reputation: 14475

// Our recursive function
function fixLetterCase(array) {
    for (var i = 0, length = array.length; i < length; i++) {
        array[i] = capitalize(array[i]);
    }
}

Would probably be a better, more readable, more performant solution. This case does not need recursion at all, a loop should be more effective. You don't need that many function calls and you don't need to evaluate the array's length with every function call.

Upvotes: 1

Matt Urtnowski
Matt Urtnowski

Reputation: 2566

This really doesn't need to be written recursivly, you could do the same thing with

// Our array of messy words 
var capitals = ["berlin", "parIs", "MaDRiD"]; 

// Capitalize function 
function capitalize(word) { 
    return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); 
} 

// Our recursive function 
function fixLetterCase(array, i) { 

    // Action 
    array[i] = capitalize(array[i]); 
} 

// Here is our function call
for(var i = 0; i < capitals.length; i++)
{
     fixLetterCase(capitals, i); 
}

console.log(capitals); 

The recursive part is only iterating through the capital words and escapes when you reach the end of the array. Hopefully reading it like this will make it more clear.

Upvotes: 0

Simeon Visser
Simeon Visser

Reputation: 122376

The function fixLetterCase is called with i equal to zero. When fixLetterCase runs for the first time, it calls capitalize on the first word in the array. In the recursive call, it increments i with 1 so that means it will capitalize the next word in the array. It will keep doing this until it reaches the end of the array.

That's why the function works: the index of the word that needs to be capitalized is incremented each time and in the last call, when the index is equal to the length of the array, the 'recursion' ends.

Note that this is not a typical use of recursion because there's nothing returned from the recursive calls. Also note that this could be written much more straightforwardly as a for loop.

Upvotes: 0

Rob W
Rob W

Reputation: 349062

function fixLetterCase(array, i) {
    // At some point, this is true, so the algorithm stops
    if (i === array.length) { return; }
    // At this point, the function did not returned. The function logic continues.

    // Replace the array key with a new value = the return value of captialize()
    array[i] = capitalize(array[i]);

    // i increments by one, and the function itself is called again.
    return fixLetterCase(array, i + 1);
}

// Initialize the function, with starting offset ("counter") i=0
fixLetterCase(capitals, 0);

Upvotes: 1

Related Questions