1

This is my code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Lab 5A : Fibbonacci numbers using Javascript</title>
</head>
<body>
<h1>Fibonacci Number Generation</h1>
<h2>Using Javascript</h2>
<hr />
<script type="text/javascript">

var n = prompt("Enter the number of terms : ", "") ;
var fib0 = 0, fib1 = 1;
var i;
var tmp ;
for ( i=2; i<=n; i++ )
{
var tmp = fib0;
fib0 = fib1;
fib1 = tmp + fib1;
}
document.write(n > 0 ? fib1 : 0);

</script>
</body>
</html>

I am getting the following error :

XML Parsing Error: not well-formed Location: file:///C:/Users/s13rw81/Desktop/Lab%20Programs/IWT%20Lab/Lab5A.xhtml 
Line Number 17, Column 16:
//for ( i=2; i<=n; i++ )
---------------^
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272

1 Answers1

1

I must concur with epascarello here but nevertheless:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Lab 5A : Fibbonacci numbers using Javascript</title>
</head>
<body>
<h1>Fibonacci Number Generation</h1>
<h2>Using Javascript</h2>
<hr />
<script type="text/javascript">
//<![CDATA[
var n = prompt("Enter the number of terms : ", "") ;
n = parseInt(n);
// check input here not in the document.write
var fib0 = 0, fib1 = 1;
var i;
var tmp; 
for (i=2; i<=n; i++ ) { 
  tmp = fib0;
  fib0 = fib1;
  fib1 = tmp + fib1;
}
alert(fib1);
//]]>
</script>
</body>
</html>

The problem is the bracket that is the lower-than character in Javascript but the opening character for an HTML element. You can put the whole script into a CDATA bracket to avoid fumbling with every occurance of a <. Please follow Xufox' links to learn more.

deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20
  • be good to show html5 alternative also. You didn't explain why you concur – charlietfl Oct 28 '15 at 01:44
  • I concur in his wondering about the use of XHTML in these days. But I also know about old teachers, legacy code, pointy-haired bosses etc. Doing it in HTML-5 is to far from you actual question if I understand teh rules here corectly, so please ask a new one. – deamentiaemundi Oct 28 '15 at 02:15
  • @deamentiaemundi this code worked but not flawlessly though. you missed closing the meta tag. also i am supposed to generate n terms, so when i tried to modify this code i noticed the alert(fib1) alerts a 5 on entering the number of terms as 5, if you replace the alert with a document.write it prints nothing. :( the brackets problem was solved. thanks –  Oct 29 '15 at 11:34
  • guys how to you generate and display the series using an alert ? –  Oct 29 '15 at 11:39
  • I sweare I've put a `/` before the closing bracket in the `meta` element!? Sorry for that. Soooo, you modified my code and it stoped working and that is my fault? No, wait, the result is correct (see e.g.: http://oeis.org/A000045), so what is your problem? Do you want to get the full series up to an including the n'th term? Just gather them inside the loop in an array say `a` with e.g.: a.`push()` and print that array with `a.join(",")`. – deamentiaemundi Oct 29 '15 at 16:32
  • 1
    @deamentiaemundi `` will not validate as XHTML by the way, even with the slash. Use the full form (as a http-equiv). – Mr Lister Oct 30 '15 at 16:07
  • 1
    @Whistler81`document.write` in an XHTML file is an abomination. – Mr Lister Oct 30 '15 at 16:08
  • @MrLister `xmllint` said it was ok? Ah, forgot that it does *not* load the DTD automatically, sorry. The errors are different, though, it *needs* the attribute `content` and does not even know `charset` but on the other side: the shortform is allowed. – deamentiaemundi Oct 30 '15 at 18:06