By Eran Medan, NICE Actimize
var app = function () { return { func: function (f, range, multipliesOf, modifiers) { var ret = []; for (var i = range.from; i <= range.to; i++) { if (i % multipliesOf === 0) { ret.push(f(i)); } } return ret; } } };var result = app().func(function (x) { return x * x },{from: 1, to: 50}, 2); result[2] = result[0] + result[1]; // modify array element var answer = result[2] * 2 + 2;console.log(answer);
<script></script> tag, then open that HTML file in a browser1 + 1; 2 (1 + 3) * 2; 8 + / * - ( ) % var x = 3; enter:
x * x; 9
x = 4; x * x; 16 var part1 = "Hello";
var part2 = "World";
var full = part1 + " " + part2;
"Hello World" "Hello World")true, false)1, 0.5)
var x = 2; // everything after "//" is ignored on the same line var x = 2;/* now I can write my commenton multiple linesJavaScript code is also ignoredx = 3; */console.log(x); //still 2
if (something is true) {
//then part
} else {
//else part
} if (1 + 1 == 2) {
console.log("one plus one is still two");
} else {
console.log("the world is ending!");
} if( 5 > 3) {console.log("five is greater than three");}
var x = true; //true and false are reserved keywords in JavaScriptvar y = (1 + 2 == 3); //y is equal to true, as 1 + 2 equals to 3var z = 2 < 1; // z is equal to false, as 2 is not less than 1
var x = true && true;// x will be equal to true as both left AND right = truevar y = true || false;// y will be equal to true as either left OR right are true
var x = (true && true) || false;var y = (1 >= 0) || x == !false;if (!x || (y != false)) {console.log("condition is true");} else {console.log("condition is false");}
var x = (true && true) || false;var x = (true) || false;var x = true || false;var x = true;var y = (1 >= 0) || x == !false;
var y = (true) || x == !false;
var y = (true) || x == true;
var y = true || true; // we already found that x is equal to true
var y = true;!x || (y != false)
!true || (true != false)
false || true
true
while (condition is true) {
//keeps running until condition is false or a call to "break"
} while (true) { // will run forever, AKA infinite loop }while (false) { // will never run }
var counter = 0;// read: as long as variable counter is less than 10, repeat this codewhile (counter < 10) {console.log(counter);//read: increment counter by 1. (= is for assignment, not equality)counter = counter + 1;}
var counter = 0;while (counter < 10) {counter += 1; // same as counter = counter + 1;}
var counter = 0;while (counter < 10) {counter ++; // same as += 1}
var i = 0; //initializationwhile (i < 10) { //conditioni++; //increment}
for (initialization; condition; increment) {}
for (var i = 0; i < 10; i++) {}
for (initialization; condition; increment) {// iteration}
var counter = 0;while (true) {if (counter == 10){break; // when counter reaches 10, the loop will end}counter += 1;}
for (var i = 1; i <= 100; i++) {//read: if the remainder of i divided by 3 is 0, i.e. a multiple of 3if (i % 3 == 0) {continue; // then stop this iteration and move to the next one}console.log(i);}
var myArray1 = new Array(); // empty arrayvar myArray2 = new Array(1,2,3); array with elements 1 2 3
var myArray1 = []; // empty arrayvar myArray2 = [1,2,3]; array with elements 1 2 3
var array = ["a", "b", "c"];console.log( array[0] ); //will print "a"console.log( array[1] ); //will print "b"console.log( array[2] ); //will print "c"
var array = [];array[0] = "a";array[1] = "b";array[2] = "c";//array contains ["a", "b", "c"]
var array = [];array.push("a");array.push("b");array.push("c");//array contains ["a", "b", "c"]
var languages = ["JavaScript", "C++", "Erlang"];// let's change "Erlang" to "Java"// "Erlang" is in the 3rd place, but we count from 0, so it's index 2languages[2] = "Java";console.log(languages);// this will print ["JavaScript", "C++", "Java"]
var myArray = [1,2,3];console.log( myArray.length ); //prints 3
var myArray1 = [1,2,3];var myArray2 = [4,5,6];console.log( myArray1.concat(myArray2) ); //prints [1,2,3,4,5,6];
var myMixedArray = ["Strings", true, false, 1, 2.4];var myMatrix = [[1,2,3],[4,5,6],[7,8,9]];console.log(myMatrix[0]); //prints [1,2,3]console.log(myMatrix[1]); //prints [4,5,6]var innerArray = myMatrix[2]; //innerArray = [7,8,9]console.log(innerArray[0]); //prints 7console.log(innerArray[1]); //prints 8console.log(myMatrix[1][0]); //prints 4 - why? myMatrix[1]=[4,5,6]
var myArray = [1,2,3];for(var i = 0; i < myArray.length ; i++){var curElement = myArray[i];console.log("Array element at index " + i + " is " + curElement);}
var myArray = [1,2,3];for(var i in myArray){if (myArray.hasOwnProperty(i)){ //will be explained in future sessionvar curElement = myArray[i];console.log("Array element at index " + i + " is " + curElement);}}
var myObject = new Object(); var myObject = {};var myObject = new Object();myObject.firstName = "John";myObject.lastName = "Doe";
// : instead of = and , instead of ;var myObject = { firstName: "John", lastName: "Doe" }
var myObject = {firstName: "John",lastName: "Doe",emails: ["john.doe@gmail.com", "jdoe@example.com"],address: {line1: "1 Main st.",city: "New York",state: "NY",zip: 10001}}
function square(x) { return x*x; } var square = function(x) { return x*x }function foo() {console.log("No params, no return value");}function bar(x) {console.log("Param is " + x + " no return value");}function add(x, y) {return x + y;}
function square(x) { return x*x;}square(2); //-> square(2){ return 2*2 } -> square(2){ return 4 } -> 4
function add(x, y) { return x + y; }function square(x) { return x * x; }add(square(2), square(3));add(2 * 2, 3 * 3);add(4, 9);4 + 9;13;
// filter array to keep only items for which the predicate returns truefunction filter(array, predicate) {var result = [];for(var i = 0; i<array.length; i++){var element = array[i];if (predicate(element)) result.push(element);}return result;}function isLargerThan3(x) { return x > 3; }filter([1,2,3,4,5], isLargerThan3); //returns [4,5]
// filter array to keep only items for which the predicate returns true function filter(array, predicate) { var result = []; for(var i = 0; i<array.length; i++){ var element = array[i]; if (predicate(element)) result.push(element); } return result; }filter([1,2,3,4,5], function (x) { return x > 3; }); //returns [4,5]
var app = function () { return { //return an object with key "func", and a function value func: function (f, range, multipliesOf, modifiers) { var ret = []; for (var i = range.from; i <= range.to; i++) { if (i % multipliesOf === 0) { ret.push(f(i)); //add items to an array } } return ret; } } };var result = app().func(function (x) { return x * x },{from: 1, to: 50}, 2); result[2] = result[0] + result[1]; // modify array element var answer = result[2] * 2 + 2;console.log(answer);