RELIM Data Structure and Algorithms Information Sheet 2
RELIM Data Structure and Algorithms Information Sheet 2
Data
- an item of information. This data comes in all sorts of forms, such as numbers,
text, dates
and so on.
Data can come in many different forms, or what we term types. Some of the data types
that JavaScript handles you’ll recognize from the world outside programming, for
example, numbers and text. Other data types are a little bit more abstract and are used
to make programming easier.
JavaScript on the other hand, is a weakly typed language and is a lot more forgiving
about how we use different types of data. When we deal with data, we often don’t need
to specify what type of data it is, JavaScript will work it out for itself.
Numerical Data
Text Data
We tell JavaScript that text is to be treated as text and not as code simply by enclosing
it inside quote marks (“).
Example:
“Hello World” ‘Hello’
“A” ‘B’
Escape Character
Boolean Data
The use of yes or no, positive or negative, and true or false is commonplace in the
‘real’ world. The idea of true and false is also fundamentals to digital computers
themselves; they don’t understand maybes, only true and false. In fact, he concepts of
yes or no is so useful it has its own data types in JavaScript: the Boolean data type. The
Boolean type has two possible values: true for yes and false for no.
The purpose of Boolean data in JavaScript is just the same as in the world outside
programming; it allows us to answer questions and make decisions based on the answer.
Data can be stored either permanently or temporarily. All permanent record and
information might be stored in what we called database.
Database
- is use to kept or store permanent record.
There are other cases where we don’t want to permanently store data, but simply want to
keep a temporary note of it, so rather than use a database, we need to use something
called variable.
Variable
- can be used to store temporary data that can be varied.
The names of variable must follow certain rules:
You are allowed to use numbers in your variable names, but the names must not begin
with
99variables
my%Variable
theGood&theB
ad Valid names
include:
myVariables99
myPercent_Variable
the_Good_and_the_Bad
Before you can use a variable, you should declare its existence to the computer using
the var keyword. This warns the computer that it needs to reserve some memory for
your data to be stored in later. To declare a new variable called myFirstVariable you would
write:
var myFirstVariable;
The semicolon at the end of the line is not part of the variable name, but instead is used
to indicate to JavaScript the end of a line of code. Once declared, a variable can be used
to store any type of data.
You put data into your variables, a process called assigning values to your variables, by
using the equals sign (=).
Example:
myFirstVariable = 101;
The = sign has a special name when used to assign values to a variable; it’s called the
assignment operator.
Try It Out – Declaring Variables
<HTML> OUTPUT
<BODY>
var myFirstVariable;
myFirstVariable = "Hello";
alert(myFirstVariable);
myFirstVariable = 54321;
alert(myFirstVariable);
</SCRIPT>
</BODY>
</HTML>
var myVariable;
var myOtherVariable;
myOtherVariable = 22;
the we can use the following line to assign myVariable with the same value as
myOtherVariable
myVariable = myOtherVariable;
<HTML> OUTPUT
<BODY>
var myVariable;
var myOtherVariable;
myOtherVariable = 22;
myVariable =
myOtherVariable;
alert(myVariable);
</SCRIPT>
</BODY>
</HTML>
<HTML>
<BODY>
alert(string1);
alert(string2);
string2 = string1;
alert(string1);
alert(string2);
alert(string1);
alert(string2);
</SCRIPT>
</BODY>
</HTML>
We’ve seen how to declare variables, and how they can store information, but so far
we’ve not done anything really useful with this. What variables allow us to do is
temporarily hold information that we can use for processing in mathematical
calculations, in building up text messages, or processing words that the user has
entered.
Numerical Calculations
Expression
- valid combination of operators, constant and variables.
Try It Out – Calculations
<HTML>
<BODY>
var firstNumber =
15; var
secondNumber = 10;
var answer;
answer = 15/10;
alert(answer);
</SCRIPT>
</BODY>
</HTML>
Operator Precedence
Not all operators are created equal; some have a higher precedence
<HTML>
<BODY>
<SCRIPT LANGUAGE = JavaScript> OUTPUT
var myVariable;
myVariable = 1 + 1 *
2; alert(myVariable);
</SCRIPT>
</BODY>
</HTML>
Precedence order
1. Exponentiation
2. Multiplication and Division
3. Addition and Subtraction
Example1 Example2
<HTML> <HTML>
<BODY> <BODY>
<SCRIPT LANGUAGE= JavaScript> <SCRIPT LANGUAGE=
JavaScript>
var myVariable;
myVariable = 50 + 20 – 10 / 2 * 3; var myVariable;
alert(myVariable); myVariable = 4 + 2 * 3 – 6 * 5;
alert(myVariable);
</SCRIPT>
</BODY> </SCRIPT>
</HTML> </BODY>
</HTML>
Example3 Example4
<HTML> <HTML>
<BODY> <BODY>
<SCRIPT LANGUAGE= JavaScript> <SCRIPT LANGUAGE=
JavaScript>
var myVariable;
myVariable = (15 - 4) *(60 / 10 + 4 * 12 - 50) var myVariable;
+ 60; alert(myVariable); myVariable = 10 * (5 + 10 * 5 /
2); alert(myVariable);
</SCRIPT>
</BODY> </SCRIPT>
</HTML> </BODY>
</HTML>
<HTML>
<BODY>
alert(degCent);
</SCRIPT>
</BODY>
</HTML>
Prompt Function
- works in a similar way to an alert( ) function, except that as well as displaying a
message, it also contains a text box in which the user can enter a value.
These two pieces of information must be specified in the given order, and separated by a
comma. If you don’t want a default value to be contained in the input box when the prompt
box opens, you should use an empty string (" ") for the second piece of information.
Sample Problem
There are 1000 grams in one kilogram. Create a program that read in weight in grams and
output the equivalent weight in kilograms.
<HTML>
<BODY>
</SCRIPT>
</BODY>
</HTML>