JavaScript Numbers and Expressions - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

JavaScript Numbers and Expressions

Description:

Recall: JavaScript variables can store values of different data types: ... a right triangle by using only the length of the hypotenuse and one of the sides. ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 12
Provided by: jennif113
Category:

less

Transcript and Presenter's Notes

Title: JavaScript Numbers and Expressions


1
JavaScript Numbers and Expressions
  • Recitation, Week 5
  • September 23, 2005

2
Precedence of operators
  • Recall JavaScript variables can store values of
    different data types
  • String, number, boolean (True or false)
  • Operators for numbers
  • Multiplication(), division(/), remainder()
  • have higher precedence than
  • addition (), subtraction (-)

3
Precedence Associativity of operators
  • Example 1
  • x 3 27 5 7 20 ? x 145
  • x 3 (27 5) (7 20) ? x 145
  • Example 2
  • Same type of operators processed from left to
    right
  • x 20 7 5 x ? 8
  • x 20 (7 5) x ? 18
  • Mixing both
  • x 3 6 8 / 2 (10 5) x ? 9

4
Number of operands
  • Previous arithmetic operators are binary
    operators They take 2 operands.
  • Unary operators They take a single operand.
  • Negation(-) It has higher precedence than the
    binary arithmetic operators.
  • t 4
  • u t 6 8
  • z -1 (v u / -t) - 13
  • document.write(t t) ? t 4 (negation
    doesnt affect t)
  • document.write(u u) ? u 52
  • document.write(v v) ? v -13
  • document.write(z z) ? z 0

5
Number of operands
  • Unary operators
  • Increment () Increases the value of its
    operand by 1.
  • x 23
  • x or also x is the same as x x 1 x
    ? 24
  • decrement (--) Decreases the value of its
    operand by 1
  • y 15
  • --y or also y-- is the same as y y 1 y
    ? 1
  • They also have higher precedence over arithmetic
    operations.

6
Increment and decrement
  • Note x and x have different affects when
    they are used on the right hand side of
    assignments
  • First case
  • x 8
  • y x ? y 8
  • Second case
  • x 8
  • y x ? y 9
  • Same situation applies for decrement (--)

7
Alternative Assignment Ways
  • Some shorthand notations
  • x y is the same as x x y
  • -
  • x - y ? x x y
  • x y ? x x y
  • /
  • x / y ? x x / y

8
Number of operands
  • Example
  • x 8
  • y x x
  • x 1 ? x becomes 9 now
  • z (y x) 10
  • document.write(z z) ? z 5

9
Predefined Math functions
  • Math.random()
  • This function generates a random number between 0
    and 1 (1 not included)
  • To map the generated numbers between 0 and X we
    multiply the number by X.
  • To obtain whole numbers following functions are
    useful
  • Math.round(a) gives the integer closest to a
  • Math.floor(a) rounds a down to the closest
    integer to a.
  • Math.ceil(a) rounds a up to the closest
    integer to a,
  • Note If a is already an integer the result of
    these functions is a itself.

10
Math.random
  • Check out this site about obtaining whole numbers
    by using previous functions
  • http//www.shawnolson.net/a/789/
  • Now lets see an example random.htm
  • We used the following pattern to come up with
    integers between 1 and 10
  • rand Math.random()
  • number1 10 rand 1
  • number2 Math.floor(number1)

11
Diagnostic Write Statements
  • We have an example on calculating the area of a
    right triangle by using only the length of the
    hypotenuse and one of the sides.
  • There is a logic error in the program, so we will
    use diagnostic write statements to find the
    error.
  • 1) errored one
  • 2) the one with diagnostic write statements
  • 3) corrected one
Write a Comment
User Comments (0)
About PowerShow.com