-1

Well im new to javascript I have done C++ C# VB. But this javascript book is hard to understand, it is nothing but exercises no full code to look at. Anyways I am doing a simple project but I dont understand where I am going wrong. The CDATA is where I believe I am going wrong. This is my second project and the first was easy it wasnt until the CDATA and Var that I get Stuck. I know this code is outdated but I am following the book to learn basic JS and working my way up. Heres the code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Largest Islands</title>
<meta http-equiv="content-type" content="text/html; charset=iso-
8859-1" />
</head>
<body>
<h1>Largest Islands</h1>
<script type="text/javascript">
/* <![CDATA[ */
var island1Name = "Greenland";
var island2Name = "New Guinea";
var island3Name = "Borneo";
var island4Name = "Madagascar";
var island5Name = "Baffin";
var island1Size = 2175600;
var island2Size = 790000;
var island3Size = 737000;
var island4Size = 587000;
var island5Size = 507000;
document.write("<p>The largest island
in the world is " + island1Name
+ " with " + island1Size + " miles.</p>");
document.write("<p>The second largest island
in the world is " + island2Name
+ " with " + island2Size + " miles.</p>");
document.write("<p>The third largest island
in the world is " + island3Name
+ " with " + island3Size + " miles.</p>");
document.write("<p>The fourth largest island
in the world is " + island4Name
+ " with " + island4Size + " miles.</p>");
document.write("<p>The fifth largest island
in the world is " + island5Name
+ " with " + island5Size + " miles.</p>");
/* ]]> */
</script>
</body>
</html>

its a very simple project I just need some help figuring out where I am going wrong. Thank you in advance

user2069296
  • 3
  • 1
  • 5

4 Answers4

0

Running it locally, the JavaScript error console makes it very clear where the problem is. It even gives you line numbers.

document.write("<p>The largest island
in the world is " + island1Name...

You can't have a line break in the middle of a JavaScript string.

document.write("<p>The largest island in the world is " + island1Name...

works. You'll need to fix all of them, and start using the development tools available to you in every major browser.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
0

I think the problem comes from breaking string lines

Instead of

document.write("<p>The largest island
in the world is " + island1Name

try

document.write("<p>The largest island" 
+ "in the world is " + island1Name
mika
  • 1,411
  • 1
  • 12
  • 23
0

Your problem is that multiple line strings like this are not allowed in JavaScript:

document.write("<p>The second largest island
in the world is " + island2Name

If you get rid of those new lines in the wrong places, the code works. That line would now look like this:

document.write("<p>The second largest island in the world is " + island2Name

Example: http://jsbin.com/igowel/1/edit

To continue working with JavaScript, get used to using the console and developer tools. For Chrome, take a look at this Q&A: How do I use the JavaScript Console in Google Chrome?

Community
  • 1
  • 1
Cymen
  • 14,079
  • 4
  • 52
  • 72
  • Thank you. That's weird because my book told me to put the break there – user2069296 Feb 13 '13 at 18:52
  • To be honest, it looks like a really old book. While it's great to start with vanilla JavaScript, when getting started something like jQuery is really handy. If the book is more than roughly 2-3 years old, you may want to get a more up to date book like jQuery in Action (actually, I'd suggest that anyway). – Cymen Feb 13 '13 at 20:24
0

I think your problem is Line breaks here. Your code:

document.write("<p>The largest island
in the world is " + island1Name
+ " with " + island1Size + " miles.</p>");

Should be :

document.write("<p>The largest island in the world is " + island1Name
+ " with " + island1Size + " miles.</p>");

If you start a string in a line and you don't end on the same it'll cause line break throwing Error. Besides I'd suggest you to use firefox for development along with firebug where you can debug you scripts.

Akshay Khandelwal
  • 1,570
  • 11
  • 19