Tutorial by Examples: er

The filter() method creates an array filled with all array elements that pass a test provided as a function. 5.1 [1, 2, 3, 4, 5].filter(function(value, index, arr) { return value > 2; }); 6 [1, 2, 3, 4, 5].filter(value => value > 2); Results in a new array: [3, 4, 5] Fi...
To iterate through a list you can use for: for x in ['one', 'two', 'three', 'four']: print(x) This will print out the elements of the list: one two three four The range function generates numbers which are also often used in a for loop. for x in range(1, 6): print(x) The res...
"123.50".to_i #=> 123 Integer("123.50") #=> 123 A string will take the value of any integer at its start, but will not take integers from anywhere else: "123-foo".to_i # => 123 "foo-123".to_i # => 0 However, there is a difference when ...
1/2 #=> 0 Since we are dividing two integers, the result is an integer. To solve this problem, we need to cast at least one of those to Float: 1.0 / 2 #=> 0.5 1.to_f / 2 #=> 0.5 1 / Float(2) #=> 0.5 Alternatively, fdiv may be used to return the floating point result of di...
Consider a database with the following two tables. Employees table: IdFNameLNameDeptId1JamesSmith32JohnJohnson4 Departments table: IdName1Sales2Marketing3Finance4IT Simple select statement * is the wildcard character used to select all available columns in a table. When used as a substitute f...
The JSONSerialization class is built into Apple's Foundation framework. 2.2 Read JSON The JSONObjectWithData function takes NSData, and returns AnyObject. You can use as? to convert the result to your expected type. do { guard let jsonData = "[\"Hello\", \"JSON\"]&q...
An enum provides a set of related values: enum Direction { case up case down case left case right } enum Direction { case up, down, left, right } Enum values can be used by their fully-qualified name, but you can omit the type name when it can be inferred: let dir = Dire...
Logical OR (||), reading left to right, will evaluate to the first truthy value. If no truthy value is found, the last value is returned. var a = 'hello' || ''; // a = 'hello' var b = '' || []; // b = [] var c = '' || undefined; // c = undefined var d = 1 |...
The math module contains the math.sqrt()-function that can compute the square root of any number (that can be converted to a float) and the result will always be a float: import math math.sqrt(9) # 3.0 math.sqrt(11.11) # 3.3331666624997918 math.sqrt(Decimal('6.25')) ...
Integers in PHP can be natively specified in base 2 (binary), base 8 (octal), base 10 (decimal), or base 16 (hexadecimal.) $my_decimal = 42; $my_binary = 0b101010; $my_octal = 052; $my_hexadecimal = 0x2a; echo ($my_binary + $my_octal) / 2; // Output is always in decimal: 42 Integers are 3...
A traditional for-loop A traditional for loop has three components: The initialization: executed before the look block is executed the first time The condition: checks a condition every time before the loop block is executed, and quits the loop if false The afterthought: performed every time a...
import random randint() Returns a random integer between x and y (inclusive): random.randint(x, y) For example getting a random number between 1 and 8: random.randint(1, 8) # Out: 8 randrange() random.randrange has the same syntax as range and unlike random.randint, the last value is no...
jQuery is the starting point for writing any jQuery code. It can be used as a function jQuery(...) or a variable jQuery.foo. $ is an alias for jQuery and the two can usually be interchanged for each other (except where jQuery.noConflict(); has been used - see Avoiding namespace collisions). Assumi...
Protocols may define associated type requirements using the associatedtype keyword: protocol Container { associatedtype Element var count: Int { get } subscript(index: Int) -> Element { get set } } Protocols with associated type requirements can only be used as generic constraints:...
var re = /[a-z]+/; if (re.test("foo")) { console.log("Match exists."); } The test method performs a search to see if a regular expression matches a string. The regular expression [a-z]+ will search for one or more lowercase letters. Since the pattern matches the string,...
git remote add upstream git-repository-url Adds remote git repository represented by git-repository-url as new remote named upstream to the git repository
Setting a specific Seed will create a fixed random-number series: random.seed(5) # Create a fixed state print(random.randrange(0, 10)) # Get a random integer between 0 and 9 # Out: 9 print(random.randrange(0, 10)) # Out: 4 Resetting the seed will create the same &qu...
x > y x < y These operators compare two types of values, they're the less than and greater than operators. For numbers this simply compares the numerical values to see which is larger: 12 > 4 # True 12 < 4 # False 1 < 4 # True For strings they will compare lexicographical...
#include <stdio.h> /* increment: take number, increment it by one, and return it */ int increment(int i) { printf("increment %d by 1\n", i); return i + 1; } /* decrement: take number, decrement it by one, and return it */ int decrement(int i) { printf("...
#include <stdio.h> enum Op { ADD = '+', SUB = '-', }; /* add: add a and b, return result */ int add(int a, int b) { return a + b; } /* sub: subtract b from a, return result */ int sub(int a, int b) { return a - b; } /* getmath: return the appropriate m...

Page 11 of 417