Abdul
Abdul

Reputation: 21

<script> tag is not working i am trying to run javascript script in script tag in html file

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>

Upvotes: 2

Views: 19372

Answers (2)

Christos
Christos

Reputation: 53958

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.

Upvotes: 2

Rion Williams
Rion Williams

Reputation: 76547

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>

Upvotes: 1

Related Questions