how to declare five variables and declare each variable on its own line
I need a little bit more of a understanding with this. In the example I have I know I am doing something wrong because I can not pull it up in a windows screen. This is only one of the five variables I have. Another question do I have to put the javascript.css or can it just be the <script>
I am not understanding the different use with the js.css
or just using <script>
thanks.
var stock[0] ="Cisco";
var changenet[0] ="0.155 up 0.72%";
var lastsale[0] =$21.775;
document.write("<p><strong>stock[0]<strong>: " + stock[0] +"Cisco" +
changenet[0] +" Up"+ lastsale[0] +"to buy is at.<\/p>");
ok I am going to show you what I have been working on for hours and still can not get it to pull up on a web page...
<!DOCTYPE html PUBLIC"-//W3C//DTD Xhtml 1.0 Strict//EN"
"http://www.w3.org.TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Project 4-1</title>
</head>
<body>
<script type="text/javascript">
&l开发者_如何学JAVAt;!--HIDE FROM INCOMPATIBLE BROWERS
var stock[0] ="Cisco";
var changenet[0] ="0.155 up 0.72%";
var lastsale[0] =$21.775;
document.write("<p><strong>stock[0]<strong>: " + stock[0] +"Cisco" + changenet[0] +" Up"+ lastsale[0] +"to buy is at.</p>");
var stock[1] ="Microsoft";
var changenet[1] ="0.085 down 0.78%";
var lastsale[1] =$27.61;
document.write("<p><strong>stcok1<strong>: " + stock[1] +"Microsoft" + changenet[1] +" down"+ lastsale[1] +"to buy it at.</p>");
var stock[2] ="intel";
var changenet[1] ="0.085 down 0.78%";
var lastsale[2] =$21.40;
document.write("<p><strong>stock2<strong>: " + stock[2] +"Intel" + changenet[2] +" up"+ lastsale[2] +"to buy it at.</p>");
var stock[3] ="NVIDIA";
var changenet[2] ="0.0212 up 0.10%";
var lastsale[3] =$24.908;
document.write("<p><strong>stock3<strong>: " + stock[3] +"NVIDIA" + changenet[3] +" down"+ lastsale[3] +"to buy it at.</p>");
var stock[4] ="apple";
var changenet[4] ="4.1301 down 1.20%";
var lastsale[4] =$340.1898;
document.write("<p><strong>stock4<strong>: " + stock[4]+"apple" + changenet[4] +" down"+ lastsale[4] +"to buy it at.</p>");
var stock[5] ="EA";
var changenet[5] ="0.4 down 2.60%";
var lastsale[5] =$17.62;
document.write("<p><strong>stock5<strong>: " + stock[5] +"EA" + changenet[5] +" down"+ lastsale[5] +"to buy it at.</p>");
//STOP HIDING FROM INCOMPATIBLE BROWERS-->
</script>
</body>
</html>
You need to learn better syntax: Thau's JavaScript Tutorial (beware it's a little outdated)
you don't need the [0] if stock, changenet, and lastsale are not arrays. If they are arrays then you should put the var only when you define the array for the first time.
without using arrays:
var stock = "Cisco",
changenet = "0.155 up 0.72%",
lastsale = 21.775; //amount in dollars
document.write("Stock: " + stock + ", Changenet: " + changenet + ", Last Sale: " + lastsale);
with using arrays:
var stock = [],
changenet = [],
lastsale = [];
stock[0] = "Cisco";
changenet[0] = "0.155 up 0.72%";
lastsale[0] = 21.755;
document.write("Stock: " + stock[0] + ", Changenet: " + changenet[0] + ", Last Sale: " + lastsale[0]);
If you plan to do calculations I would recommend doing something else for the changenet variable, like making it 2 different variables, one with the decimal number, and one for the perentage.
var stock[0]
is weird, and probably incorrect Javascript syntax.
To declare an array, use either: var myArray = new Array();
(older style) or var myArray = [];
Eg, try:
var stock = [], changenet = [], lastsale = [];
stock[0] ="Cisco";
changenet[0] ="0.155 up 0.72%";
lastsale[0] = "$21.775"; // note that the $ needs to be part of a string!
document.write("<p><strong>stock[0]<strong>: " + stock[0] +"Cisco" +
changenet[0] +" Up"+ lastsale[0] +"to buy is at.<\/p>");
Or, a shorter solution since you are not actually using variables as arrays in your example:
var stock = "Cisco",
changenet = "0.155 up 0.72%",
lastsale = "$21.775"; // note that the $ needs to be part of a string!
document.write("<p><strong>stock<strong>: " + stock + "Cisco" +
changenet +" Up"+ lastsale +"to buy is at.<\/p>");
Added I just looked again at your document.write statement and I notice that you included your ref to stock
within a string literal. It won't work that way. So you probably mean:
document.write("<p><strong>" + stock + "<strong>: " + stock + "Cisco" +
changenet +" Up"+ lastsale +"to buy is at.<\/p>");
// Instead of
document.write("<p><strong>stock<strong>: " + stock + "Cisco" +
changenet +" Up"+ lastsale +"to buy is at.<\/p>");
Added in response to comment:
well i need to declare 5 variables and each has to be on its on line. Then i have to write a statement for each one.
You can declare variables and initialize them at the same time. Or you can first declare them and then later set them. When you declare them, you don't have to repeat the var statement since the var statement can be used with multiple variables at one time. Eg
var stocks = [], // declaring a new array
stock = "Cisco", // declaring a simple var (stock) and initializing it
changenet; // declaring a simple var without initializing it.
// all of the above are part of one var statement.
// or, the same as the above, but written as multiple statements:
var stocks = []; // declaring a new array
var stock = "Cisco"; // declaring a simple var (stock) and initializing it
var changenet; // declaring a simple var without initializing it.
// note that the statements end with the ;
changenet = "0.155 up 0.72%"; // assigning a value to an existing variable
As guys mentioned, you can declare your variables this way:
var stock = "EA",
changenet = "0.4 down 2.60%",
lastsale = $17.62;
but just be careful, if accidentally you will end one line with ';', then other variables will be declared as global:
var stock = "EA", // will be local
changenet = "0.4 down 2.60%"; // will be local
lastsale = $17.62; // will be global
one little technique comes to help, you can declare variables this way (and this is my preferable one):
var stock = "EA"
, changenet = "0.4 down 2.60%"
, lastsale = $17.62;
now you at least see your "commas", and if one line ends with ';' then you get JS error:
var stock = "EA"
, changenet = "0.4 down 2.60%";
, lastsale = $17.62; // Syntax Error
Other stuff, about arrays/not arrays and declaring was mentioned by other, so I just wanted to complement their answers
精彩评论