Quotes help make search much faster. Example: "Practice Makes Perfect"

Monday, December 10, 2012

Codecademy: Codeyear Modifying values in nested arrays


//sample solution


var capitals = [ ["berlin", "parIs", "MaDRiD"],
                 ["monTEvideo", "BrASiLia"],
                 ["dElhi", "toKYo", "BeiJing"],
                 ["CanBerra"],
                 ["kiGaLi", "pretoRIA"] ];

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

// Our recursive function
function fixLetterCase(array, x, y) {
  if (y === capitals.length) {
    return;
  }
  else if (x === capitals[y].length) {
    return fixLetterCase(array, 0, y + 1);
  }
  else {
    array[y][x] = capitalize(array[y][x]);
    return fixLetterCase(array, x + 1, y);
  }
}

fixLetterCase(capitals, 0, 0);

console.log(capitals);

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts