Posts

Showing posts with the label T-JAVASCRIPT

if…else and switch in javascript

JavaScript code can   calculate, compare, and determine true and false conditions , things start looking a lot more interesting.  the power of choice into your scripts with   conditional statements . These statements enable your script to evaluate given conditions on the basis of which to execute one or more actions. More specifically, you're going to learn how to use: if   statements; if ... else   statements; if ... else if ... else   statements; switch   statements. In the process, you're also going to learn how to use   shortcut assignment operators . if Statement The   if   keyword is used in JavaScript to perform a basic test for the   truth or falsity of a given condition and execute a piece of code accordingly . Here's its structure: //if the condition in brackets is true if ( condition ) { //do some stuff here } Let's translate the code Translated into English, the   if statement   above says: "

while loop in java script

Use the same HTML document from the previous example, but delete all the JavaScript between the <script> ... </script> tags. Now, type in the following JavaScript code: <script type="text/javascript"> //Grab the html div by its id attribute //and store it in a var named container var container = document.getElementById("wrapper"); //create the var containing the counter //and give it an initial value of 0 var numImages = 0; //start the loop: while the number of images is less than or //equal to 10 (loop starts at 0 not 1, so type 9 not 10), //keep cycling and increase the counter by 1 while (numImages <= 9) { //this is the block of code to be executed: //build the string to insert in the HTML document //and store it in the newImage var // Take note:make sure the img src corresponds to a real image file var newImage = '<img src="loo

JavaScript with loops

Your JavaScript scripts can do all sort of clever stuff like calculating discounts, comparing values, and making choices. However, don't forget we're dealing with machine language, and machines are very good at doing some things us humans would keep a mile off if only we could. One really boring thing most of us would happily avoid doing is repetitive stuff. You might find it hard to believe this, but JavaScript loves repetition. So much so that it has a special construct for it: the   loop . Here's what you will learn in this lesson: the   for loop ; the   while loop ; the   do ... while loop . In the process, you will also learn how to: use   getElementById(element Id)   to manipulate an HTML element with JavaScript; generate   random numbers with random() and floor(number) ; insert   HTML mark-up dynamically with innerHTML . This is going to be a meaty lesson. Get ready for it! Loops At times you will have to repeat some piece of code a number

JavaScript Code with Functions

This is where   JavaScript functions   come into play. In this lesson, you will learn: what   functions   are and how they are used in JavaScript; how to   retrieve information from functions ; how to   give information to functions ; what   variable scope   is all about. In the process you will also learn how to: get today's date dynamically; change the value of an HTML button element dynamically. Functions: what they are and what they are for In previous lessons you used JavaScript functions to perform some actions quickly and easily like generating random numbers, writing something on a web page, etc. In fact, JavaScript has a great many built-in functions like   write(), alert(), getElementById(), random(), floor() , and several others. Wherever you spot a pair of round brackets there's likely a function in action. JavaScript also gives you the option to craft your own functions. A function is a way of packaging your JavaScript commands so you ca