2

Hello I think its a basic question or mistake i believe i want to ask here (my appologies for being ignorant on subject matter) but I want to run this code from on of the java's reference book (JavaScript: The Definitive Guide Book by David Flanagan) in a script tag in an html file. I sure it's probably a very little mistake, below is my code :

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>js book example1</title>
</head>

<body>
    <p>test</p>
    <script>
        document.write("<h2>Table of Fibonacci Numbers</h2>");
        for (i = 0, j = 1, k = 0. fib = 0; i < 50; i++, fib = j + k, j = k, k = fib) {
            document.write("Fibonacci(" + i + ") =" + fib);
            document.write("<br>";)
        }
    </script>
</body>

</html>
2
  • 1
    Tip: use your browser's JavaScript console, it will report any script errors and anything else that requires your attention. Commented Feb 16, 2017 at 20:35
  • Also, avoid document.write - it's a slow and outmoded way of adding content to a webpage (and it won't work as you would expect in callback functions or event-handlers), use the DOM API instead (document.createElement, etc). Commented Feb 16, 2017 at 20:36

2 Answers 2

2

You have two mistypes:

  • k=0. should be k=0,
  • document.write("<br>;") should be document.write("<br>");

document.write("<h2>Table of Fibonacci Numbers</h2>");
for(i=0, j=1, k=0, fib=0; i<50; i++, fib=j+k, j=k, k=fib){
    document.write("Fibonacci(" + i + ") =" +fib);
    document.write("<br>");
}

A very easy to find out what's the problem (for the next time) is to open developer tools (press F12, if you use Chrome) and navigate to the console tab. There you would see the line where the problem is and you could possibly solve it in cases like these immediately.

I followed exactly the above approach, in order to find out what's wrong. I didn't even try to read the code :). The console tab had the following. If you notice at the rightmost part of the image you have the exact line, where the error emerged.

enter image description here

If you now click at the line (js:14), you will see the following:

enter image description here

By correcting this and start from the start you will notice the second error by following the same procedure.

Sign up to request clarification or add additional context in comments.

4 Comments

k=0. should be k=0,
@Phil Thank you very much :) My mistypo this time !
Thanks a million, i knew it was a typo somewhere that i was going crazy, lol , thanks
@Christos, sorry I forgot all about the chrome developer tool , thanks for reminding me, i'll keep in mind next time
1

You currently have two typos within your code that will throw off the syntax and thus cause your code not to work as expected :

// You had a period here instead of a comma (after "k=0"), which will cause
// the remainder of your for loop to not be properly parsed
for (i = 0, j = 1, k = 0, fib = 0; i < 50; i++, fib = j + k, j = k, k = fib) { ... }

and :

// This was previously document.write("<br>";), note the transposed ";)" which should be
// ");"
document.write("<br>";)

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>js book example1</title>
</head>

<body>
  <p>test</p>
  <script>
    document.write("<h2>Table of Fibonacci Numbers</h2>");
    for (i = 0, j = 1, k = 0, fib = 0; i < 50; i++, fib = j + k, j = k, k = fib) {
      document.write("Fibonacci(" + i + ") =" + fib);
      document.write("<br>");
    }
  </script>
</body>
</html>

1 Comment

Thanks i appreciate that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.