-2

I'm very new to Javascript, and I could use some help troubleshooting. In the console log it says "upper" and "wordcount" are not defined. The goal of this function is to loop through an array derived from an inputdiv and ask if the array value is present in that array, AND is NOT present in the "wordcount" array, and if it's not, to push it in.

function processtext() {
  var textindiv = document.getElementById("inputdiv").innerHTML;
  var split = textindiv.split(" ");
  var upper = [];
  var wordcount = [];
  for (var i = 0; i < split.length; ++i) {
    upper.push(split[i].toUpperCase());
  }
  var sortedlist = upper.sort();
  var wordcount = new Array;

  for (var i = 0; i < sortedlist.length; ++i) {
    if (sortedlist.indexOf(sortedlist[i]) > -1) {
      if (wordcount.indexOf(sortedlist[i]) == -1) {
        wordcount.push(sortedlist[i]);
      }
    }
  }
}
console.log(upper);
console.log(wordcount);
Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
  • 2
    [What is the scope of variables in JavaScript?](http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Ram Oct 01 '16 at 23:16
  • 2
    By the way, `if (sortedlist.indexOf(sortedlist[i]) > -1)` will always be `true` ;) – Robiseb Oct 01 '16 at 23:16

1 Answers1

2

The variables are defined in the function, not globally.

You may either move the console.log statements into the function, or move the variable declarations out of the function.

user2182349
  • 9,569
  • 3
  • 29
  • 41