Loosely Typed Language, Comparisons - Javascript | CMSC 102, Study notes of Computer Science

Material Type: Notes; Professor: Padua-Perez; Class: INTRO INFO TECHNOLOGY; Subject: Computer Science; University: University of Maryland; Term: Fall 2006;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-n3k
koofers-user-n3k 🇺🇸

5

(1)

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
JavaScript (Loosely Typed
Language)
JavaScript is a loosely typed language
You do no need to specify a type for a variable
A variable can assume different types of values (not all at the same
time)
Code Snippet where variable value first assumes a string value and then a
numeric value
var value, input, ratePerHour = 8.0;
value = "University of Maryland College Park";
document.writeln("Check will be sent to: " + value + "<br>");
input = prompt("Enter number of hours:");
value = input * ratePerHour;
document.writeln("Total amount is: " + value);
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Loosely Typed Language, Comparisons - Javascript | CMSC 102 and more Study notes Computer Science in PDF only on Docsity!

JavaScript (Loosely Typed Language) ^ JavaScript is a loosely typed language^ ^ You do no need to specify a type for a variable^ ^ A variable can assume different types of values (not all at the sametime) ^ Code Snippet where variable value first assumes a string value and then anumeric value^ var value, input, ratePerHour = 8.0;value = "University of Maryland College Park";document.writeln("Check will be sent to: " + value + "
");input = prompt("Enter number of hours:");value = input * ratePerHour;document.writeln("Total amount is: " + value);

JavaScript (Data Types) ^ We have seen integer, float, and string values ^ New type: boolean type^ ^ Assumes the value true or false^ ^ Variable declaration and initializationvar found = true;var attending = false; ^ Some mathematical functions and constants you can use whileworking with numbers^ ^ Math.abs() – Absolute value (Example: Math.abs(-10))^ ^ Math.max() – Maximum of two values (Example: Math.max(10,20))^ ^ Math.sqrt() – Square root (Example: Math.sqrt(4))^ ^ Math.random() – Random value between 0 and 1.0 (Example:Math.random())^ ^ Math.PI – mathematical constant pi

JavaScript (If Statement)^ ^ If statement – Control statement which allows JavaScript to make decisions^ ^ It has two forms^ ^ First Form^ if (expression)

statement // executed if expression is true ^ Second Form^ if (expression)

statement1 // executed if expression is trueelsestatement2 // executed if expression is false ^ If you want to execute more than one statement then use a set of { } toenclose the statements ^ Example

(See Example1.html)

JavaScript (while statement)^ ^ while statement – Control statement which allowsJavaScript to repeat a set of statements^ ^ Basic Form^ while (expression)

statement

// executed as long as expression is true

^ If you want to execute more than one statement thenuse a set of { } to enclose the statements ^ Example

(See Example2.html)

JavaScript (Combination of Statements) ^ You can combine if, while and do while statements inseveral ways^ ^ You can have an several nested if statements^ ^ You can have if statements within while statements^ ^ You can have while statements within if ^ Example

(See Example4.html)

^ Example of nested loops  Infinite loop ^ Condition where the controlling expression in a loop neverbecomes false

Designing Solutions Using Pseudocode ^ So far we have focus on the syntax and semantics of JavaScriptconstruct ^ As the complexity of problems increases you need a strategy fordesigning solutions to such problems. ^ Several alternatives exist to come up with a solution to a problem(which we can then turned into a computer program)^ 1.^ Flowchart:

Graphical representation of the solution, where symbols represent input/output operations, conditionals, anditeration statements. 2. Pseudocode:

English-like description of the set of steps required to solve a problem.

This is the alternative we want

to use.

It represents our design! ^ When you write pseudocode you focus on determining the stepsnecessary to solve a problem without worrying about JavaScriptlanguage syntax issues.

Pseudocode Example

entries = read()prev = read()i = 1valid = truewhile (i < entries && valid) {curr = read()if (curr < prev) {

valid = false;} prev = curri = i + 1 } print(“Sequence is:”)print(valid) ^ Run always a trace table to verify your pseudocode is correct ^ Could you implement the above code without actually knowing what it does??

Suggestions for Implementation ^ Make sure you have written pseudocode ^ Do not wait until the last minute

  • Code implementation could

be unpredictable.  Incremental code development

  • Fundamental principle in

computer programming. Write a little bit of code, and make sureit works before you move forward.  Don’t make assumptions

  • If you are not clear about a

language construct write a little program to familiarize yourselfwith the construct  Good Indentation

  • From the get-go use good indentation as it

will allow you to understand your code better

JavaScript (Cascaded If Statement Idiom) ^ You can combine if statements to handle different cases ^ This approach to organized if statements in order to handle different cases is calledthe Cascaded If Statement ^ Cascaded If statement general form^ If (expr1)// Statement is executed if expr1 is trueelse if (expr2)// Statement is executed if expr2 is trueelse if (expr3)// Statement is executed if expr3 is trueelse// If none of the above expressions is true ^ Notice it is not a JavaScript statement. ^ Once one of the cases is executed no other case will be executed ^ You can use { } to enclose more than one statement ^ Example

(See Example5.html)

JavaScript (Functions) ^ Function - An entity that completes a particular task for us. It cantakes values necessary to complete the particular task and it canreturn values. ^ Examples of JavaScript functions^ ^ document.writeln^ ^ Alert() ^ You can define your own functions. ^ Let’s see an example of a function that computes a letter gradebased on a numeric grade. ^ Example

(See Example6.html) ^ How is the function executed?^ ^ Arguments are passed to the function^ ^ The letter grade is computed^ ^ Result is returned via the

return

statement