-3

I cannot seem to figure this out, very new to JavaScript. I've tried isNaN and typeof maybe I'm just writing the if statements wrong.

This is the code I have

// prompt user to select password length, and what characters to include
function promptUser() {
  var passwordLength = parseInt(prompt('How many characters would you like your password length to contain?'));
  
// is length is not a number return null,
  if (passwordLength = isNaN) {
    alert('Password length must be a number.');
    return null;
  }
// password length must be greater than 8
  if (passwordLength < 8) {
    alert('Password length needs to be at least 8 characters in length');
    return null;
  }
 // length must be less than 128
  if (passwordLength > 128) {
    alert('Password length cannot exceed 128 characters in length.');
    return null;
}

I was expecting the

if (passwordlength == //(or === tried both)// isNaN) 

to check if it is a number or not but it only spits out alert('Password length must be a number.'); and ignores the return null; no matter what is entered.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

3 Answers3

-1

You are Setting your passwordLength to isNan every time:

if (passwordLength = isNaN) {
    alert('Password length must be a number.');
    return null;
  }

Use:

if isNaN(passwordLength)  {
    alert('Password length must be a number.');
    return null;
  }

A single Equals = is a assignment operator and not a comparative operator.

Therefore your Code will go into this IF statement every time.

Please change this to a ==, or === for this code to run better.

Here is some reading on the topic for you: https://www.w3schools.com/js/js_comparisons.asp

Sweet Chilly Philly
  • 3,014
  • 2
  • 27
  • 37
  • 1
    setting it equal or strictly equal only presents the problem of the code always alerting that the input must be a number even if it is a number. – Sam williams Aug 07 '23 at 02:05
  • 1
    You're right about `=` being the incorrect operation, however, changing it to `==` or `===` in this case will not help. The correct way to check if something holds the value of `NaN` is to use a method such as [`Number.isNaN()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN) (which is different to [`isNaN()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN)), or one of the more robust solutions presented [here](https://stackoverflow.com/q/175739/5648954) – Nick Parsons Aug 07 '23 at 02:05
  • They’re trying to check if the parsed number is a number or not, your answer suggests that using`==` or `===` will fix their code, but that’ll always skip the if I’m that case (ie: it’ll introduce the opposite issue where the if statement is never executed) – Nick Parsons Aug 07 '23 at 08:14
-1

You may be using isNaN wrong. Try isNaN(passwordLength) instead. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
Orifjon
  • 915
  • 8
  • 25
  • More about `NaN`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN – Orifjon Aug 07 '23 at 02:05
  • this fixed it thank you very much, couldnt for the life of me find a resource telling me what I was doing wrong – Sam williams Aug 07 '23 at 02:12
-2

This part if (passwordLength = isNaN) contains a typo. the "equal to" comparison operator is '==' or '===' (strictly equal).

Also you should compare it with null, not isNaN.

Assuming that you are using HTML for input, you will be way better off to use the <input type="number"*gt; and set the minimum and maximum values.

  • While you're correct about `=` being incorrect, changing this so that it checks against `null` and not `isNaN` won't help much, seeing that `parseInt()` will return `NaN`, not `null` when it's unable to parse the string to a number. Changing the operator to `==` or `===` also won't work, as `isNaN` is a function, which isn't what you would want to compare `passwordLength` against – Nick Parsons Aug 07 '23 at 02:03