-3

I'm trying to develop this code but an error occurs :

Move 'var' declarations to the top of the function. for (var i = 0; i < extractPassword.length; i++){

The code:

var extractPassword = (['a', 'z','c']);
var newPass = [];
for (var i = 0; i < extractPassword.length; i++){
    var x = extractPassword[i];
    if ('a' <= x && x <= 'z') {
        newPass.push(0);
    }
}
return newPass;

Can you please help me correct it ?

kmart23
  • 13
  • 1
  • 1
    What exactly is unclear about the error message? (alternatively, I'd suggest not using `var` at all - use at least ES2015 syntax of `const` / `let`) – CertainPerformance Apr 17 '22 at 22:37
  • I tried to declare the variables at the top but I dont understand why isnt working – kmart23 Apr 17 '22 at 22:38
  • No, you are not declaring your variables at the top of the function - change your code so that the variables are declared at the top, and it'll clear the error – CertainPerformance Apr 17 '22 at 22:39
  • I did it this way: var extractPassword = (['a', 'z', 'c']); var newPass = []; var i = 0; var x = ''; for (var i = 0; i < extractPassword.length; i++){ var x = extractPassword[i]; if ('a' <= x && x <= 'z') { newPass.push(0); } } return newPass; – kmart23 Apr 17 '22 at 22:42
  • but the error was still there – kmart23 Apr 17 '22 at 22:42
  • 1
    You are still not declaring the variables at the top of the function. Identify the containing function, then declare the variables right inside it, instead of past other statements - or, like I said, don't use `var`, it's effectively obsolete nowadays – CertainPerformance Apr 17 '22 at 22:43
  • Aside: It's not really clear what `'a' <= x && x <= 'z'` is meant to be doing. – Andy Apr 17 '22 at 22:45
  • Just use `const` instead of `var`. You're receiving a warning (probably from your IDE). You should pretty much never be using `var` any more. Use `const` or `let` and declare the variable in the appropriate scope (as you were doing), but use `const` instead of `var` for the `x` variable in that loop. – jfriend00 Apr 18 '22 at 01:57
  • 2
    @jfriend00 It seems to me to be essentially to be a case of not reading the error message or not understanding what a function is or where the top of a function is located or something like that. Didn't seem like a useful question to have around. I voted to close as not reproducible, but probably should have closed in favor of https://stackoverflow.com/q/4646455. When an answer can be written in 60 seconds that's usually an indicator to take a step back and consider whether question really *should* be answered IMO – CertainPerformance Apr 18 '22 at 02:17
  • 1
    You should either accept the answer or write your own answer, but please don't post the answer into your question. – jabaa Apr 20 '22 at 15:50
  • A conversation about handling this question from a curation perspective has been [moved to chat](https://chat.stackoverflow.com/rooms/244059/discussion-on-question-by-kmart23-move-var-declarations-to-the-top-of-the-func). – Ryan M Apr 20 '22 at 18:13

1 Answers1

-1

If you are trying to declare the x outside use this piece of code

 var extractPassword = (['a', 'z','c']);
var x

var newPass = [];
for (let i = 0; i < extractPassword.length; i++){
     x = extractPassword[i];
    if ('a' <= x && x <= 'z') {
        newPass.push(0);
        }
    }
return newPass;
Icekid
  • 435
  • 3
  • 14