2

This is a very basic issue i have as i am new to javascript but i cannot move forward until i understand this little thing.

i have this function..

function stringOrNot() {
  var input = prompt('Input something here');

  if (typeof input != 'string') {
    alert("That is not a string!");

  } else {
    alert("That is a string!");
  }
}

stringOrNot();

Question
What do i need to input to get the alert "That is not a string"?
Which kind of inputs are not strings?

Whatever i input i get 'That is a string!' returned.

also this function...

function stringOrNot() {
  var input = prompt('Input something here');

  if (typeof input != 'string') {
    alert("That is not a string!");
  } else (typeof input == 'string'){
    alert("That is a string!");
  }
}

stringOrNot();

this returns a console error

"SyntaxError: Unexpected token '{'. Parse error."

Why can i not write the if and else conditionals like this?

Please could someone explain the answers to these little problems so i can move on! thank you in advance and forgive me, i am very new to coding.

:: )

Hi again.. UPDATE and another QUESTION.

The reason I had the initial question is because of a codeschool.com function exercise which concluded as this...

function countE(){ var phrase = prompt("Which phrase would you like to examine?");

if (typeof(phrase) != "string"){
  alert("This is not a valid entry!");
  return false;
} else {

  var eCount = 0;
  for (var i = 0; i < phrase.length; i++){
      if (phrase.charAt(i) === 'e' || phrase.charAt(i) === 'E')
      eCount++;
      }
    }

      alert(eCount);
      return true;

}

countE()

So.. I wanted to test what is not a string, I wanted to get the alert "This is not a valid entry!".

But, if a prompt only returns a string then why is this << if (typeof(phrase) != "string") >> included in the function?

1
  • 1
    It will always return a string, as that's what prompt returns. And for you second question it's } else if (typeof input ==.. Commented Nov 11, 2016 at 22:46

3 Answers 3

3

what do i need to input to get the alert "That is not a string"? which kind of inputs are not strings?

The result of the prompt always is a string. So there isn't any input that you could provide to the prompt that could result in a different type. The prompt always returns a string.

For a more formal approach, please have a look here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Christos, I will reference MDN more often now. Please can you answer my further question?
1

For stringOrNot() you have a condition in your else statement. If you wanted to have a condition, use else if () {}.

function stringOrNot() {

var input = prompt('Input something here');

if (typeof input != 'string') {
    alert("That is not a string!");

  } else if (typeof input == 'string'){
    alert("That is a string!");
  }
}

Check below for more information

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else

EDITED FOR QUESTION 2:

You include the check for the prompt because the it can also return a null value (when the user clicks exit). So when the user clicks Cancel, the "This is not a valid entry!" will appear.

2 Comments

i figured out my issue. I use Safari. in Safari pressing esc or cancel alerts '0'. In Chrome - 'This is not a valid entry!' is alerted on esc or cancel. Do you know how I can get safari to work properly with this? Thanks
You could also add a check to return "This is not a valid entry" if eCount is 0.
1

what do i need to input to get the alert "That is not a string"? which kind of inputs are not strings?

Things that are not strings include numbers, objects and booleans.

The return value of the prompt function will always be a string though. The purpose of the prompt function is to get a user entered string.

why can i not write the if and else conditionals like this?

if takes a condition. else is what happens if the condition is not met.

You are providing a condition to else, which doesn't make sense.

You can use another if statement though:

} else if (something) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.