lundi 18 mai 2015

Why doesn't my 'for' statement loop and the if statement inside it execute?

I have a function that has a for statement that repeats until a variable (i) is equal to localStorage.length and inside the for loop there is an if statement that checks if a localStorage entry starts with a # then runs a function. When I call this function nothing that is in the for loop runs.

function refresh() {
  var i = 0;

  console.info("The LocalStorage Length is: " + localStorage.length);
  console.info("i is: " + i);
  console.log("i == localStorage.length is:" + i == localStorage.length);

  for (i = 0; i == localStorage.length; i++) {
    console.info("i is: " + i);

    current = localStorage.key(i);
    if (current.substr(0, 1) == "#") {
      noteArea.innerHTML = +createHtml(current.substr(1), localStorage.getItem(current)); //Adds the html to the page
      console.log(current.substr(1) + " - Note displayed."); //Logs the note title
    } else {
      console.log("No notes saved.");
    }

    console.info("i == localStorage.length is:" + i == localStorage.length);
  }
}


function createHtml(name, desc) {
  var html = "<div class = 'note'> <h4 class = 'note-title'>" + name + "</h5> <p class = 'note-desc'>" + desc + "</p> </div> <br/>";

  console.log(html);
  return html;
}

Aucun commentaire:

Enregistrer un commentaire