-3

Here's the code in my JS file:

function arrayContains(array, value){
    return array.indexOf(value) > -1;
}
var possibleOptions = ["male", "female", "m", "f"];
var gender = prompt("What's your gender?","");
while(!arrayContains(possibleOptions, gender))
    gender = prompt("Invalid input. What's your gender?");
var name = prompt("What's your name?","");
var greeting = (gender == "male" || gender == "m") ? "Sup dude." : "Sup girl.";
alert("Hello, " + name + "! " + greeting);

The error I get:

Line: 5
char: 1
Object expected.

I can't figure it out.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • add `\` before `'`. I think `'` is your problem – Prashant Tapase Sep 27 '14 at 10:49
  • 1
    [`your code`](http://jsfiddle.net/6c1qbq5t/) runs fine – Mithun Satheesh Sep 27 '14 at 10:51
  • That code runs without errors: http://jsfiddle.net/cscr3bvu/ – Quentin Sep 27 '14 at 10:52
  • 1
    @mithunsatheesh Microsoft JScript runtime runs this. Does it matter? – Aviv Cohn Sep 27 '14 at 10:52
  • @mithunsatheesh I'm starting it by double-clicking on it in the desktop. Does it matter? – Aviv Cohn Sep 27 '14 at 10:53
  • 1
    @AvivCohn — Yes, it matters. Most JavaScript is written to run in a web browser. We were assuming that is where you were running it. – Quentin Sep 27 '14 at 10:54
  • 1
    [Here is a (sort-of) duplicate question](http://stackoverflow.com/questions/532138/prompt-dialog-in-wsh-using-jscript). There is no `prompt` global in the Windows Scripting Host. – Quentin Sep 27 '14 at 10:54
  • 1
    `alert` and `prompt` are methods of the global window-object(which exists in a browser, but not when you run it via WSH) – Dr.Molle Sep 27 '14 at 11:06
  • @Quentin I see. I'd like to know: what's the standard way to code and run your Javascript code? Create a `.html` file and embed it in there? Or some other way? – Aviv Cohn Sep 27 '14 at 19:43
  • @AvivCohn — JavaScript is a programming language. You can use it in many places. Client side in a web page is just the most common. The choice of which you use will mostly depend on what you need to achieve, not what other people do. – Quentin Sep 27 '14 at 20:34

1 Answers1

1

Two problems:

  1. As mentioned in comments, the w/cscript.exe script host does not support neither prompt() nor alert()
  2. JScript arrays have no .IndexOf() method

You have to roll your own:

function arrayContains(array, value){
    for (var i = 0, e = array.length; i < e; ++i) {
        if (array[i] === value) {
            return true;
        }
    }
    return false;
}

function prompt(p) {
    WScript.Stdout.Write(p + " > ");
    return WScript.StdIn.ReadLine();
}

function alert(s) {
    WScript.Echo(s);
}

var possibleOptions = ["male", "female", "m", "f"];
var gender = prompt("What's your gender?","");
while(!arrayContains(possibleOptions, gender))
    gender = prompt("Invalid input. What's your gender?");
var name = prompt("What's your name?","");
var greeting = (gender == "male" || gender == "m") ? "Sup dude." : "Sup girl.";
alert("Hello, " + name + "! " + greeting);

output (console):

cscript 26073853.js
What's your gender? > neuter
Invalid input. What's your gender? > male
What's your name? > tarzan
Hello, tarzan! Sup dude.

cscript 26073853.js
What's your gender? > female
What's your name? > jane
Hello, jane! Sup girl.
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96