reading-notes


Project maintained by Razan-am Hosted on GitHub Pages — Theme by mattgraham

JAVASCRIPT

Decisions and loops

  1. EVALUATIONS :You can analyze values in your scripts to determine whether or note they match expected results.

  2. DECISIONS :The results from evaluations you can decide which path your script should go down.

  3. LOOPS :There are also many occasions where you will want to perform the same set of steps repeatedly

Decisions making

In the script there is sveral places in charge in determine wich line of code should run next.

For examples : There is decesion that must be made so the code can take one of the different pathes and each one of these pathes are made of set of tasks.Thats means for each situation you have to write different code. In order to determine wich path to take you’ll set a condition and depending on the result the path will be choosen.

Comparison operators:

there are two componant to decision :

  1. Evaluating condition: an expression is evaluated which is returns avalue .

  2. Conditional statments: aconditional statment says what to do in a given situation.

Evaluating condition

For example:

Image

Logical operators:

Comparision operators returns a single value (True or false),the logical operators allows you to compare the result of more than one comparision operator .

image

If Statments:

The if statment checks and evaluates the result of a condition with true or false .

Example : var pass = 50; var score = 75; var msg; // Pass mark // Current score // Message // Select message to write based on score if (score >= pass) { msg = ‘Congratulations, you passed!’; } else { msg = ‘Have another go!’;

TYPE COERCION & WEAK TYPING:

Datatype Purpose
string Text
number Number
Boolean true or false
nul 1 Empty value
undefined Variable has been declared but not yet assigned a value

If you use a data type JavaScript did not expect, it tries to make sense of the operation rather than report an error.

Loops

The commoun types of loops:

  1. While :when you don’t know how many times the code should run
  2. For :if you need to run a code in a specific numbers of times

image

For example var i = l ; var msg = ‘ ‘ ; // Set counter to 1 // Message // Store 5 times tabl e in a variable

while (i < 10) { msg += i + ‘ x 5 = ‘ + (i * 5) + ‘
’; i++; {

image

EXAMPLE of using DECISIONS & LOOPS together in Javascript

// Unit of table var table = 3; // Type of calculation (defaults to addition) var operator= ‘addition’; // Set counter to 1 var i = 1; // Message var msg = ‘ ‘ ;

// If the operator variable says addition if (operator=== ‘addition’) // While counter is less than 11 while (i < 11) { Calculation msg += i + ‘ + ‘ + table + ‘ = ‘ + (i +table)+ ‘
’; // Add 1 to the counter i++; } // Otherwise else { // Whi le counter is less than 11 while ( i < 11) { // Calculation msg += i + ‘ x ‘ + table + ‘ = ‘ + (i *table) + ‘
‘; // Add 1 to the counter i++; }

}

// Write the message into the page

Overall about DECISIONS & LOOPS

References:

@Jon Duckett/JAVASCRIPT