Won't work on gedit


#1

I copied this code into gedit and saved it as 'Codecademy Rock Paper Scissors.js'. It worked on Codecademy, but as soon as I try to open it, it says that there is an error on line 1, char 1: object expected. What is the problem?


#2

You know that this is javaScript not Java, right? To test it you could write an HTML file and include your code in the script tag. Then you could open the html file in your favorite browser to run the script (maybe have a look at the HTML+CSS and the JQuery track for more informations). Also keep in mind that the console is the web console of your broswer not the displayed page so don't expect to see anything there if you don't explicitly tell it.


#3

It is correct to save javaScript files '.js', yes?
How do I tell it to run the code?


#4

You use it via the script tag, either like this:

<script> Here goes your code </script>

or like this:

<script type="text/javascript" src="myScript.js"></script>

What you can do as well is to press CTRL+U to see the source code of a web page to see how others do this. But better take the tracks before this because the source codes of bigger websites get kind of long with all the ads and stuff.


#5

@haxor789 what would you say about running the script with nodejs instead of in a browser? Same way as one would run Python.

If @allotmentgarden is using gedit then they probably have easy access to node as well

Debian-based as an example just because they're a likely first distro:

# apt-get install nodejs
$ echo "console.log('hello world')" > hello.js
$ nodejs hello.js
hello world

I know JS is meant to run in the browser and all that. But if the code has nothing to do with the browser, then doesn't this make sense?

Edit:
Googling "error on line 1, char 1: object expected" suggests that internet explorer is being used though. nodejs can be installed on windows as well, but it's more trouble than just typing in a single command so then one might as well just stick with the browser