All the standard built-in objects in LC2 are shown below.


Array


array0[element1,element2,…]; array0[n]=element(n+1)
array1[array0,element1,…]

The array object is a global object used to construct arrays: they are advanced, list-type objects. Take the array in the following for example:

process main () {
Smartisan = ["T1","T2","M1"];// define an array named "Smartisan"
smartphones = [Smartisan,"iPhone","Mi"];//define an array named "smartphones"
log Smartisan[0];//output T1
log smartphones[0][2];//output M1
}


length

length(array)
array.length

returns the length of a array.

log length(smartphones); //Output  3
log smartphones.length;//Output 3

for…in/of

for(var in array)
for(var of array)

traverse an array. You can also view the usage of for...in/of in instruction keyword.

for (x in smartphones){
    log x;  //Output 0,1,2
}
for (x of smartphones){
    log x;  //Output ["T1","T2","M1"],iPhone,Mi
}

indexOf

indexOf(array,element)

indicates the position of a specified element.

log indexOf(smartphones,"Smartisan"); //Output 0
log smartphones.indexOf("iPhone"); //Does not support this syntax,output -1.


Bool


bool([variable,number,RegExp])

a boolean object wrapper which could convert a variable/number/RegExp to a Boolean format.

bool(3.14) // Output  true
bool(3>4) // Output  false
bool(3-3) // Output false
bool(/[a-z]/) // Output true


Date


Creates a JavaScript Date instance that represents a single moment in time. Date objects are based on a time value that is the number of milliseconds since 1 January, 1970 UTC.


now

now();

returns the number of milliseconds elapsed from January 1st 1970, 00:00:00 UTC to the current time.

//Set 2017-1-1,00:00 as current time
log now(); //Output 1483200000

format

format(“time”,”display-format”)
format(now(),”display-format”)

returns specified time in a certain format, and could be used with the now above together. More details about format usage please click here.

//Output formated time.
log format("May 11 2012","fullDate"); //Output:Friday,May 11,2017
log format("May 11 2012","isoDate"); //Output:2012-05-11

//Assuming January 1, 2017, 0:00 to be the current time. Output formated current time.
log format(now(),"yyyy-mm-dd");//Output:2017-01-01
log format(now(),"yyyy-m-d");//Output:2017-1-1
log format(now(),"mm-d");//Output:01-1
log format(now(),"dddd,mmmm dS,yyyy,h:MM:ss TT");//Output:Sunday,January 1st,2017,0:00:00 AM
log format(now(),"isoDateTime");//Output:2017-01-01T00:00+0800


Math


abs

abs([number])

returns the absolute value of a number, that is abs(x)=|x|.

abs(-3.14) // Output  3.14
abs('') // Output  0
abs() // Output null
abs('string') // Output null

ceil

ceil([number])

returns the smallest integer greater than or equal to a given number.

ceil(3.14) // Output  4
ceil(-3.14) // Output  -3

floor

floor([number])

returns the largest integer less than or equal to a given number.

floor(3.14) // Output  3
floor(-3.14) // Output  -4

max

max([number1],[number2],…)

returns the maximum of a set of numbers.

max(3+2,number('3'+'2'))  // Output  32

min

min([number1],[number2],…)

returns the minimum of a set of numbers.

min(3+2,number('3'+'2'))  // 输出  5

random

random()

returns a float pseudo-random number in the range [0, 1), that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale it to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

//Output a random number in a specified range.
random() * (max - min) + min 


Number


number(“string”)

number is a wrapper object allowing you to work with numerical values. A Number object is created using the number() constructor.

number('3.14') // Output  3.14
number('3'+'2') // Output  32
number('') // Output  0


String


string([variable,number])

The global object string is a constructor for a string or a sequence of characters. Values could also be converted or generated into strings with it.

log length(string(3.14)); //Output  4


length

length(“string”)

represents the length of a string.

length("Lemonce") //Output  7

charAt

charAt(“string”,[letterPosition:number])

returns the specified character from a string.Number counts from zero.

charAt('Lemonce',5)  //Output c

indexOf

indexOf(“wholeString”,”chosenLetter”)

returns the index within the calling string object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.

indexOf('Lemonce','L')  //Output  0
indexOf('Lemonce','e')  //Output  1
indexOf('Lemonce','l')  //Output  -1

substr

substr(“string”,[startLetter:number],[length:number])

returns the characters in a string beginning at the specified location through the specified number of characters.

substr('Lemoncase',2,3)  //Output 'mon'
substr('Lemoncase',2)  //Output 'moncase'

trim

trim(“string”)

removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

trim('  Lemon case  ')  //Output 'Lemon case'