Click to See Complete Forum and Search --> : variable help


hitman
01-14-2003, 11:21 AM
I have been looking for some javascript sources on the net to get used to the script.
Can you tell me which value will be stored in a, after all the calculation have been done?
can you give me details of the calculation
thank you

var a = 4 + 6 / 2
var a = Math.round( 0.8 * 2 )
var a = “The” + “cat sat”
var i = 11; var a = 1 + i++;
var a = 9; a = a % 4

khalidali63
01-14-2003, 11:30 AM
value of a after all the processing
a = 1

Khalid

pyro
01-14-2003, 11:31 AM
This will show you

<script type="text/javascript">
var a = 4 + 6 / 2
var a = Math.round( 0.8 * 2 )
var a = "The" + "cat sat";
var i = 11; var a = 1 + i++;
var a = 9; a = a % 4
document.write (a);
</script>

gil davis
01-14-2003, 11:31 AM
var a = 4 + 6 / 2
"/" has precedence. 6/2=3, a=3+4=7
var a = Math.round( 0.8 * 2 )
"()" has precedence. 0.8*2=1.6, a=round(1.6)=2
var a = “The” + “cat sat”
String concatenate. a="Thecat sat"
var i = 11; var a = 1 + i++;
"i++" is after the rest. a=1+11=12, i=i+1=12
var a = 9; a = a % 4
Modular arithmetic leaves remainder. a=9%4=1 (9/4=2r1)

hitman
01-14-2003, 03:09 PM
I don't understand why a=round(1.6)=2.


"i++" is after the rest. a=1+11=12, i=i+1=12

After which rest? I don't understand why do you now put i=i+1=12 , while it was i=11. What does the i++ mean?

---------------------------
quote:
----------------------------------------------------------------------
var a = 9; a = a % 4
----------------------------------------------------------------------

Modular arithmetic leaves remainder. a=9%4=1 (9/4=2r1)


-->Where did you find a 9?Didn't you say that a=12?
How do u calculate 9%4?


I am sorry to be so annoying but I am very new to javascript and i want to learn it.
thanks for your help

fluffycow
01-14-2003, 05:38 PM
well, i++ is the same as i+1, but I never use modulus so I can't help ya there :)

gil davis
01-15-2003, 05:49 AM
Originally posted by hitman
I don't understand why a=round(1.6)=2.When you round a number, you get the closest whole number. 2 is the closest whole number to 1.6.After which rest?I mean after the first calculation that used "i" to get a value for "a". "i" does not change until after the calculation to determine "a".I don't understand why do you now put i=i+1=12 , while it was i=11. What does the i++ mean?"i++" means "add one to the variable 'i' and save it in 'i'", or i = i + 1.Where did you find a 9?Didn't you say that a=12?Are you for real? Each line changes the value that is stored in "a".How do u calculate 9%4?The "%" is the modulus operator. It returns the remainder of the integer division. 9 divided by 4 is not a whole number. 4 goes into 9 two times with a remainder of 1.I am sorry to be so annoying but I am very new to javascript and i want to learn it.
thanks for your helpThese are not really javascript problems, they are basic programming and elementary school math problems.