Javascript data structures A.G.Malamos
Loops for(var i = numberToStart; i < aNumberOfYourChoice; i++ ) { ...code you want to repeat; } var mySetToUse= { set item 1, set item 2, set item 3}; var txt = ""; for (var x in mySetToUse) { ...code you want to repeat; example txt += mySetToUse[x]; }
while (condition) { ...code you want to repeat; } do { ...code you want to repeat; } while (condition);
Array as a stack … push and pop array.push(item1, item2, ..., itemX) var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.push("Kiwi", "lemon"); The result of fruits will be: Banana,Orange,Apple,Mango,Kiwi,lemon Example from: www.w3schools.com
array.pop() <script> var fruits = ["Banana", "Orange", "Apple", "Mango"]; function myFunction() { var x=fruits.pop(); document.getElementById("demo").innerHTML = x; } </script> Will write in HTML Mango <script> var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits; function myFunction() { fruits.pop(); } </script> Will write in HTML Banana, Orange
array.splice(index, howmany, item1, ....., itemX) index Required. An integer that specifies at what position to add/remove items, Use negative values to specify the position from the end of the array howmany Optional. The number of items to be removed. If set to 0, no items will be removed item1, ..., itemX Optional. The new item(s) to be added to the array <script> var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits; function myFunction() { fruits.splice(2, 0, "Lemon", "Kiwi"); } </script> This code will write in HTML Banana,Orange,Lemon,Kiwi,Apple,Mango
Switch case switch(expression) { case n: code block break; default: } switch (new Date().getDay()) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; } switch(expression) { case n: code block break; default: }
Matching text …indexOf() and lastIndexOf() theInitialString.indexOf(aStringToSearchFor, characterNumberTostart); Returns the index of the first character of the string aStringToSearchFor in the theInitialString The lastIndexOf() method returns the position of the last occurrence of aStringToSearchFor in a string theInitialString. Both methods return -1 if the value to search for never occurs. Both methods are case sensitive