JavaScript wouldn't display results of code

151 Views Asked by At

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)

2

There are 2 best solutions below

0
On

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
0
On

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