-1

in jsLint i keep getting this 5 errors :

'Unexpected '(space)' case summer.

'Unexpected '(space)' case winter.

'Unexpected '(space)' case fall.

'Unexpected '(space)' case autumn.

'Unexpected '(space)' case spring.

var season = prompt("what is your favourite  season");
switch (season) {
case "summer": 
alert("i love summer too");
break;
case "winter": 
alert("i love winter too ");
break;
case "spring": 
alert("i love Spring  too");
break;
case "fall": 
alert("i love Fall  too");
break;
case "autumn": 
alert("i love autumn  too");
break;
Řě Đã
  • 31
  • 3
  • 1
    It's hard to help with a linting error with your code formatted like that. Most likely there's extra space *somewhere* around your case statements, but we can't know unless the code is shown here as it actually is in file. I would look for extra spaces after your "case" statements, honestly. – Steven Goodman Aug 31 '17 at 20:42
  • 2
    Welcome to StackOverflow. Please read have a look at the [Help center](https://stackoverflow.com/help). In your particular question, you can use the code format to better show your error message. Also, the code that causes this error would be necessary to help you. – Pac0 Aug 31 '17 at 20:44
  • 1
    It is just a code formatting message. If you don't want to see them, either make sure you do not put extra white spaces where they aren't needed; or turn on the option to ignore white space mess – Patrick Evans Aug 31 '17 at 21:02

1 Answers1

0

There's enough going on here that it's probably best to point you to the JSLint instructions to see what's going on for the future. You can find them here: http://jslint.com/help.html

But here's what's up for your question...

TL;DR: Mainly it's complaining that you have trailing spaces after your case statements.

Turn this...

case "summer": // <<< there's a space after the colon

... into this...

case "summer":// <<<< now there's not!

Longer:

You could get around your spacing errors by adding a JSLint Directive to the top of your file. The one you want here is white:true, which indicates that you want to, "Tolerate whitespace mess".

/*jslint: white:true */
var season = prompt("what is your favourite  season");
// ...

But now you'll have a whole slew of new things to fix...

  • Expected 'switch' to be in a function.
  • Undeclared 'prompt'.
  • Undeclared 'alert'.

The first is easy enough; you just wrap in a function.

The second two are there because you haven't told JSLint what context to expect, and it's not sure if alert and prompt are valid calls. You could tell it alert and prompt are globals with another type of directive, the global directive, but remember that...

Use of global variables is strongly discouraged, but unfortunately web browsers require their use.

So to use the globals directive, you also need to set the browser directive in the jslint comment we added earlier.

Believe it or not, this mess, below, already lints! Try it at JSLint.com.

/*jslint white:true, browser:true */
/*global alert, prompt */

function seasonCheck() {
"use strict";
var season = prompt("what is your favourite  season");
switch (season) {
case "summer":
alert("i love summer too");
break;
case "winter":
alert("i love winter too ");
break;
case "spring":
alert("i love Spring  too");
break;
case "fall":
alert("i love Fall  too");
break;
case "autumn":
alert("i love autumn  too");
break;
}
}

But prompt and alert are special cases. Those are functions you'll use during browser development. Instead of setting them up in the global directive, use the devel directive like this...

I'm also going to take off the white:true and clean this thing up so that it's readable... Note that there are no trailing spaces, and that we have the case statements lined up with the switch, which initially seemed odd to me.

/*jslint devel:true */

function seasonCheck() {
    "use strict";

    var season = prompt("what is your favourite season");

    switch (season) {
    case "summer":
        alert("i love summer too");
        break;

    case "winter":
        alert("i love winter too ");
        break;

    case "spring":
        alert("i love Spring  too");
        break;

    case "fall":
        alert("i love Fall  too");
        break;

    case "autumn":
        alert("i love autumn  too");
        break;
    }
}

And now we have a JSLint-happy, cleanly whitespaced piece of code. I'd probably move the alert to the end and assign values to a string in your switch, but JSLint is already happy.

Hope that helps, and gives you a little jump-start for using JSLint.

ruffin
  • 16,507
  • 9
  • 88
  • 138