0

This is my code for checking a register page. It says that variables i and j are already defined, although it's local variables and not global. What could I do in this situation? How can I make the vars locally and not globally? Help is really appreciated, I am a new student in Computer Science.

function checkName() { // בודק על
    var n = document.getElementById("FullName").value;
    var len = n.length;
    var no = "!@#$%^&*()-_+=\'|][}{><./,;:?,";
    var num = "0123456789";
    if (n == "") {
        document.getElementById("errName").innerHTML = "רשום את השם בבקשה";
        return false;
    }
    for (var i = 0; i < n.length; i++) {
        if (n.charAt(i) == " ") {
            len--;
        }
    }
    if (len < 2) {
        document.getElementById("errName").innerHTML = "לא הגיוני שם עם אות אחת";
        return false;
    }
    for (var i = 0; i < n.length; i++) {
        if (n.charAt(i) >= "a" && n.charAt(i) <= "z") {
            document.getElementById("errName").innerHTML = "לא הגיוני שם עם אותיות באנגלית";
            return false;
        }
    }
    for (var i = 0; i < no.length; i++) {
        for (var j = 0; j < n.length; j++) {
            if (no.charAt(i) == n.charAt(j)) {
                document.getElementById("errName").innerHTML = "אסור תווים מיוחדים";
                return false;
            }
        }
    }
    for (var i = 0; i < num.length; i++) {
        for (var j = 0; j < n.length; j++) {
            if (num.charAt(i) == n.charAt(j)) {
                document.getElementById("errName").innerHTML = "אסור מספרים";
                return false;
            }
        }
    }
    for (var i = 0; i < 3; i++) {
        if (n[i] == ' ') {
            document.getElementById("errName").innerHTML = "אסור רווחים בהתחלה";
            return false;
            if (n[i + 1] == ' ') {
                document.getElementById("errName").innerHTML = "אסור רווחים בהתחלה";
                return false;
            }
        }
    }
    document.getElementById("errName").innerHTML = "";
    return true;
}
Daniel Puiu
  • 962
  • 6
  • 21
  • 29
  • simply add `var i, j;` and, in your for loops, replace: `var i = 0` with `i = 0` and `var j = 0` with `j = 0`. What it is telling you, is that you are re-declaring `var i = 0` in every for loop, while the variabile `i` is declared in the first one already. same happens with j. It's not related to a "global" variable, but rather a local conflict due to the same name in several different for cycles. You can add those declaration right after your `var num` line. – briosheje May 06 '18 at 18:52
  • Alright, guys, I declared I, J at the start and removed the vars and it works now, thanks. – Itamar Shafir May 06 '18 at 18:58
  • Redeclaration with var should not throw. I don't understand how above code would throw. I am assuming some not further mentioned linter, JSHint came up in google. – ASDFGerte May 06 '18 at 19:11
  • 1
    Declaring the same variable twice is not actually an error, anyway. It is just a bad practice that can make your code confusing. (Presumably you posted this because your IDE highlighted it as a problem.) – Stuart May 06 '18 at 19:12
  • "*It says that variables 'i' and 'j' are already defined*" - who is saying that and where? Are you using a linter? – Bergi May 06 '18 at 20:01
  • I am using Web Developer 2010 – Itamar Shafir May 06 '18 at 20:02

3 Answers3

0

Javascript has function scope, so defining any variable in a function with var will make it exist throughout the function.

In each for loop, you redefine i with var i = 0. For example:

for (var i = 0; i < n.length; i++) {
    if (n.charAt(i) == " ") {
        len--;
    }
}
...

for (var i = 0; i < num.length; i++) {
    for (var j = 0; j < n.length; j++) {
        if (num.charAt(i) == n.charAt(j)) {
            document.getElementById("errName").innerHTML = "אסור מספרים";
            return false;
        }
    }
}

You can add a var i; at the top of the function instead, and simply say i=0 in your loops, so you don't define it multiple times.

Consider looking into using let in newer JS versions.

ᔕᖺᘎᕊ
  • 2,971
  • 3
  • 23
  • 38
0

Try to use new ES6 let instead of var to declare a variables in your functions/project. This will solve your issue with global name scope. You can read more here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

Miami_3
  • 82
  • 7
0

You need to look into function scoping. As var has scope of function in which it is defined.

So in your case variable i has access inside checkName function. Using let will help you out in above scenario.