Monday, August 16, 2010

I'm not sure how to set this up in html format in Javascript?

Javascript, like most programming languages, provides arithmetic operators for numbers. Operators such as +, -, etc. are used to perform the corresponding arithmetic operations. But most of these operators only compute the result, not store it. That can be done only using the assignment operator, namely =. If you compute the result without storing it, the operation is wasted.





The assignment operator stores the VALUE given on its right hand side in the LOCATION specified by the left hand side. Thus, the left hand side MUST be a variable. The right hand side must be evaluatable to a value. So valid RHS includes 1, a (a previously assigned variable), a + 1 (an expression with previously assigned variables) etc. Note that as the RHS and LHS have different purposes, a = b is NOT the same as b = a.





The two assignments to be done today are to swap and to calculate compound interest. They both are to be done on the same html document. Although they could even be done in the same javascript section, you should have them in two separate scripts so that any problems with one won't interfere with the other. Make sure the two problems are separated with clear headings when seen in the browser.





Problem 2: Swapping contents of two variables


Declare three variables a, b and temp. Read values for a and b from the user using the prompt command.


Show the values of a and b specifying which is which (using document.write).


Using three assignment operations, swap the values of a and b. You will need three assignment statements. You will need to use temp. This three statements must be independent of the actual value of a or b. HINT: Imagine you were holding a red ball in your left hand and a green ball in your right. If there is a table where you can keep just one ball at a time, how would you exchange the balls in your hands?


Show the new value of a and b specifying which is which.








Problem 3: Calculating sales price


Declare three variables itemprice, salestaxrate and saleprice. Read itemprice from the user. Setsalestaxrate to 6.5 on the same line that you declare it.


Using a single assignment statement, compute and store in saleprice the amount that will have to be paid after adding the sales tax. The right hand side of the assignment statement is going to be a somewhat complicated arithmetic expression that will compute the sale price directly from item price and sales-tax rate. It may be useful to work out the expression using pen and paper. You do not need to worry about rounding the tax to the nearest cent.


Show the values of all three variables specifying which is which.I'm not sure how to set this up in html format in Javascript?
[Problem2.html]


%26lt;html%26gt;


%26lt;head%26gt;


%26lt;title%26gt;Problem 2%26lt;/title%26gt;


%26lt;/head%26gt;


%26lt;body%26gt;


%26lt;script language=';javascript'; type=';text/javascript';%26gt;


function output(b)


{


document.write(';%26lt;div style='width:100%;'%26gt;';+b+';%26lt;/div%26gt;';);


}





var a,b, temp;


a = prompt(';Enter A';);


b = prompt(';Enter B';);


document.write(';%26lt;div style='width:150px;'%26gt;';);


output(';Before';);


output(';A == ';+a);


output(';B == ';+b);


temp = a;


a = b;


b = temp;


output(';After';);


output(';A == ';+a);


output(';B == ';+b);


document.write(';%26lt;/div%26gt;';);


%26lt;/script%26gt;


%26lt;/body%26gt;


%26lt;/html%26gt;





[Problem3.html]


%26lt;html%26gt;


%26lt;head%26gt;


%26lt;title%26gt;Problem 3%26lt;/title%26gt;


%26lt;/head%26gt;


%26lt;body%26gt;


%26lt;script language=';javascript'; type=';text/javascript';%26gt;


function output(b)


{


document.write(';%26lt;div style='width:100%;'%26gt;';+b+';%26lt;/div%26gt;';);


}





var itemprice = prompt(';What is the price of the item?';);


var salestaxrate = 6.5;


var saleprice = (salestaxrate*itemprice)+itemprice;


document.write(';%26lt;div style='width:150px;'%26gt;';);


output(';Item Price:';+itemprice);


output(';Sales Tax:';+salestaxrate);


output(';Total Sales Price:';+saleprice);


document.write(';%26lt;/div%26gt;';);


%26lt;/script%26gt;


%26lt;/body%26gt;


%26lt;/html%26gt;

No comments:

Post a Comment