Javascript: Difference between revisions

From Objectif Client Inc
Jump to navigation Jump to search
Line 124: Line 124:
=== Loop ===
=== Loop ===
<div class="mw-collapsible-content">
<div class="mw-collapsible-content">
==== while ====
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">


var number = 1;
var myNumber = 1;


while (number <= 10) {
while (myNumber <= 10) {
   console.log(number);
   console.log(myNumber);
   number++;
   myNumber++;
}
}


while (true) {
while (true) {
   console.log(number);
   console.log(myNumber);


   if(number ==10 {
   if(myNumber ==10 {
       break;
       break;
   }   
   }   


   number++;
   myNumber++;
}
}
</syntaxhighlight>
==== do ====
<syntaxhighlight lang="javascript">
do {
  myNumber = parseInt(prompt('Enter a number:'));
} while (isNaN(myNnumber));
  console.log(myNumber);
</syntaxhighlight>
</syntaxhighlight>
</div>
</div>
Line 147: Line 160:


<div class="toccolours mw-collapsible mw-collapsed">
<div class="toccolours mw-collapsible mw-collapsed">
=== Basic validation ===
=== Basic validation ===
<div class="mw-collapsible-content">
<div class="mw-collapsible-content">

Revision as of 15:38, 29 October 2017

Basic

alert/prompt/confirm

alert('Windows Popup text');
prompt('Windows Popup Text and return value entered');
confirm('Display ok and Cancel button and return true or false');

Html Tag

<!DOCTYPE html>
<html>
<head>
   <title>Java script Demo</title>
</head>
<body>
   <h1>Titre niveau 1</h1>

   <script>
      alert('your javascript here!');
   </script>

   <script src="js/app.js"></script>

Variables

var myVariable;
var myVariable = 10;
var myVariable = "some string"; var myVariable = 'some string'; var myVariable = 'it \'s easy';
var myBolean = 10 > 1;
var myOppositeBolean = !myBolean;

Comparison

  • Less: <
  • Greater: >
  • equal (value): ==
  • equal (value and type: ===
  • not equal: !=
  • undefined: == 'undefined'
  • function type of variable
alert(typeof myVariable);

Condition

Logic

  • and: &&
  • or: ||

if

var cond1 = 1 > 2 &&  2 > 3;
var cond2 = 1 < 2 | 10 > 1;
var myVariable2 = 0

if(typeof myVariable = 'undefined') {
   alert('This variable has not been declared');
}

if(myVariable2) {
   alert('This variable is empty');
}

if(cond1) {
   alert('cond1 is true');
} else if(cond2) {
   alert ('cond2 is true');
} else {
   alert('cond1 and cond2 are false');
}

switch / case

switch(value) {
   case 'val1':
      alert(' = val1');
   break;

   case 'val2':
      alert(' = val2');
   break;

   default:
      alert('None of previous case')
}

?

alert(( 10 > 1) ? 'true' : 'false');

Loop

while

var myNumber = 1;

while (myNumber <= 10) {
   console.log(myNumber);
   myNumber++;
}

while (true) {
   console.log(myNumber);

   if(myNumber ==10 {
      break;
   }   

   myNumber++;
}

do

do {
   myNumber = parseInt(prompt('Enter a number:'));
} while (isNaN(myNnumber));

   console.log(myNumber);

Basic validation

var number = parseInt(prompt('Enter a number'));

while(isNaN(number)) {
   number = parseInt(prompt('This is not a number: Retry:'));
}