Similar presentations:
Web Programming and Problem Solving
1. Web Programming and Problem Solving
Nazarbayev UniversityLesson
Programming Basics
Presented by Click to enter name
2. Getting Started with JavaScript
Usually, JavaScript is used in web pages… fornow, we will just concentrate on basics of the
language
A JavaScript Interpreter:
http://www.webtoolkitonline.com/javascripttester.html
3. A First Program
Try the following program:var a;
var b;
var c;
a = 13;
b = 200;
c = a + b;
document.write(c);
4. Values and Variables
In programs, variables are labels thatrepresent places where data is stored
The data that is stored inside a variable is
called its value
What are the
variables and their
values in this
program?
var a;
var b;
var c;
a = 13;
b = 200;
c = a + b;
document.write(c);
5. Declaration
To tell the computer to set aside storagespace for a variable, you should declare the
variable
In JavaScript, you
declare variables
using the var
keyword
var a;
var b;
var c;
a = 13;
b = 200;
c = a + b;
document.write(c);
6. Assignment
To change the value stored in a variable, youassign it a new value using the equals sign
var a;
var b;
var c;
Examples of
assignments
a = 13;
b = 200;
c = a + b;
document.write(c);
7. Expressions
Expressions are things that can have a valueExamples:
Variables
Numbers and other
literals
Expressions joined
together by operators
(e.g., +, -, *, /)
What expressions
do you see in this
program?
var a;
var b;
var c;
a = 13;
b = 200;
c = a + b;
document.write(c);
8. Statements
Statements are the elements in code thatperform individual tasks in sequential order
Think “a line of code”
Often end with a semicolon
Examples:
Declarations
Assignments
Function calls
What are the statements
in this program?
var a;
var b;
var c;
a = 13;
b = 200;
c = a + b;
document.write(c);
9. Working with Numbers
Arithmetic operations (+, -, *, /) can be usedOrder of operations: * and / take precedence
over + and –
Exercise: What values do these produce?
100 + 4 * 11
(100 + 4) * 11
115 * 4 – 88 / 2
10. Exercise 1
Add code to the following program to convertFahrenheit to Celsius using the formula:
(F – 32) * 5/9 = C
var fahr = 100;
var cels;
// add your code here
document.write(cels);
11. Exercise 2
Add code to the following program tocalculate the average of a, b, and c
var
var
var
var
a = 10;
b = 20;
c = 25;
avg;
// add your code here
document.write(avg);
12. Challenge Exercise
Add code to the following program tocalculate both roots of a quadratic equation
var
var
var
var
a =
b =
c =
x1,
2;
-4;
-6;
x2;
var d = ???;
var sqrtd = Math.sqrt(d);
x1 = ???;
x2 = ???;
document.write(x1 + ", " + x2);