Question

This code about area of a circle calculation can't run, I don't know what is wrong?

    <script> 

    var r=20; 
    var Pi=3.14; 
    var P=(Math.pow(r,2)*Pi); 
    document.write("r=20cm<br>" "'π'=3.14<br>" "P=r<sup>2</sup>'π'<br>" "P=" + (P) + "cm<sup>2</sup>"); 

    </script> 

The code should in the end return this: r=20 Pi=3.14 P=r^2π P=result (here it should be 1256) cm^2.

Était-ce utile?

La solution

Check your browser console, and you would see that it would display a message "Unexpected string".

document.write("r=20cm<br>" "'π'=3.14<br>" "P=r<sup>2</sup>'π'<br>" "P=" + (P) + "cm<sup>2</sup>");

Was the problem. You can only supply one string to be written. You would have to make those multiple strings a single one by adding + in between.

document.write("r=20cm<br>" + "'π'=3.14<br>" + "P=r<sup>2</sup>'π'<br>" + "P=" + (P) + "cm<sup>2</sup>");

should work.

Autres conseils

You are missing the + operators!

document.write("r=20cm<br>" + "'π'=3.14<br>" + "P=r<sup>2</sup>'π'<br>"+ "P=" + (P) + "cm<sup>2</sup>"); 

On a practical note, why would you want to concatenate strings like that? Why can't you just :

document.write("r=20cm<br>'π'=3.14<br>P=r<sup>2</sup>'π'<br>P=" + (P) + "cm<sup>2</sup>"); 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top