0

I tried creating a simple JavaScript file based on my adaption of source code from MDN.

My JavaScript code (loughshore_clubs.js) is as follows

<!--
var Club = “Ballinderry” ;

function ClubType(name){
if (name == “Ardboe”){
return name ;
} else{
 return “I'm not from “+ name + “.”;
}
}
var clubs {myClub: ClubType(“Ardboe”), club2: ClubType(Club), 
club3:  
ClubType(“Moortown”)}

console.log(clubs.myClub); //Ardboe
console.log(clubs.club2); //Ballinderry
console.log(clubs.club3); //Moortown

/-->

And the HTML source (test.html) is

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
        <meta http-equiv="content-type" content="text/html; 
charset=utf-8">
    <title></title>
    <meta name="generator" content="LibreOffice 4.2.4.2 (Linux)">
    <meta name="created" content="20150514;0">
    <meta name="changed" content="20150514;211357234273120">
    <style type="text/css">
    <!--
        @page { margin: 2cm }
            p { margin-bottom: 0.25cm; color: #000000; line-height:  
120%     }
        a:link { so-language: en-US }
    -->
    </style>
</head>
<body>
<script src="scripts/loughshore_clubs.js" />
</body>
</html>

What's the matter? One thing I do realise is that I should avoid saving HTML files using LibreOffice and stick with Bluefish. (which I have on Mac o/s X Yosemite)

  • 1
    why is your js file commented out ? – lonewolf217 May 14 '15 at 22:13
  • Your JavaScript is chock full of syntax errors. Also what's with the HTML style comments at the top and bottom? For instance, you're missing an `=` after `var clubs`, `var clubs = {key: value}` – azium May 14 '15 at 22:13
  • I hope you know why you should never use word processing software like LibreOffice to write code of any kind. These types of software will not save your files in plain text, they apply all sorts of formatting to your files that any code parser/compiler will fail on. You also do not have to use Bluefish, there are always other options out there to write code in. – JDWardle May 14 '15 at 22:23

2 Answers2

2

Remove the first and last lines of your script. HTML comment tags make no sense in a .js file.

Then replace each of your characters with a proper ".

You're also missing an = between clubs and { here: var clubs {myClub:...

After these changes you should have:

var Club = "Ballinderry";

function ClubType(name){
    if (name == "Ardboe") {
        return name;
    } else {
        return "I'm not from " + name + ".";
    }
}

var clubs = {
  myClub: ClubType("Ardboe"),
  club2: ClubType(Club), 
  club3: ClubType("Moortown")
};

console.log(clubs.myClub); //Ardboe
console.log(clubs.club2); //Ballinderry
console.log(clubs.club3); //Moortown
Paul
  • 139,544
  • 27
  • 275
  • 264
0

This should work:

var Club = "Ballinderry" ;

function ClubType(name){
if (name == "Ardboe"){
return name ;
} else{
 return "I\'m not from "+ name + ".";
}
}
var clubs = {
  myClub: ClubType("Ardboe"),
   club2: ClubType(Club),
   club3: ClubType("Moortown")
 };

console.log(clubs.myClub); //Ardboe
console.log(clubs.club2); //Ballinderry
console.log(clubs.club3); //Moortown

You're right, you should stop saving code with LibreOffice, because it changed all your " to . I recommend using atom

And you didn't have an = when declaring the clubs variable.

Once again, get atom, and then download the linter package and use JShint. That should get you in the habit of writing nice code. I use it myself. Tweet to me if you need more help, I started out two months ago and I just completed the backend for my first Node.js app.

Edit: The other answer beat me to it, he should get the vote. :P

Amin Shah Gilani
  • 8,675
  • 5
  • 37
  • 79