Section 1

Preview this deck

Math.floor(Math.random() * 10) + 1

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (39)

Section 1

(39 cards)

Math.floor(Math.random() * 10) + 1

Front

// returns a number between 1 and 10

Back

The Compare Function

Front

The purpose of the compare function is to define an alternative sort order. The compare function should return a negative, zero, or positive value, depending on the arguments: function(a, b){return a-b}

Back

Math.floor()

Front

Math.floor(x) returns the value of x rounded down to its nearest integer: Example Math.floor(4.7); // returns 4

Back

concat()

Front

concat() joins two or more strings: Example var text1 = "Hello"; var text2 = "World"; var text3 = text1.concat(" ", text2); The concat() method can be used instead of the plus operator

Back

Math.abs

Front

Math.abs(x) returns the absolute (positive) value of x: Example Math.abs(-4.7); // returns 4.7

Back

Math.pow(x, y)

Front

Math.pow(x, y) returns the value of x to the power of y: Example Math.pow(8, 2); // returns 64

Back

substring()

Front

substring() is similar to slice(). The difference is that substring() cannot accept negative indexes. Example var str = "Apple, Banana, Kiwi"; var res = str.substring(7, 13); The result of res will be: Banana

Back

splice()

Front

The splice() method can be used to add new items to an array: Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2, 0, "Lemon", "Kiwi"); The first parameter (2) defines the position where new elements should be added (spliced in). The second parameter (0) defines how many elements should be removed. The rest of the parameters ("Lemon" , "Kiwi") define the new elements to be added.

Back

unshift()

Front

The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements: Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift("Lemon"); // Adds a new element "Lemon" to fruits The unshift() method returns the new array length. Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift("Lemon"); // Returns 5

Back

toString()

Front

toString() returns a number as a string. All number methods can be used on any type of numbers (literals, variables, or expressions): Example var x = 123; x.toString(); // returns 123 from variable x (123).toString(); // returns 123 from literal 123 (100 + 23).toString(); // returns 123 from expression 100 + 23

Back

slice()

Front

slice() extracts a part of a string and returns the extracted part in a new string. The method takes 2 parameters: the starting index (position), and the ending index (position). This example slices out a portion of a string from position 7 to position 13: If you omit the second parameter, the method will slice out the rest of the string Example var str = "Apple, Banana, Kiwi"; var res = str.slice(7, 13); The result of res will be: Banana

Back

Converting Arrays to Strings

Front

The JavaScript method toString() converts an array to a string of (comma separated) array values. Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits.toString(); Result Banana,Orange,Apple,Mango

Back

Math.ceil()

Front

Math.ceil(x) returns the value of x rounded up to its nearest integer: Example Math.ceil(4.4); // returns 5

Back

reverse()

Front

The reverse() method reverses the elements in an array. You can use it to sort an array in descending order: Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); // Sorts the elements of fruits fruits.reverse(); // Reverses the order of the elements

Back

push()

Front

The push() method adds a new element to an array (at the end): Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.push("Kiwi"); // Adds a new element ("Kiwi") to fruits The push() method returns the new array length: Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; var x = fruits.push("Kiwi"); // the value of x is 5

Back

pop()

Front

The pop() method removes the last element from an array: Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.pop(); // Removes the last element ("Mango") from fruits The pop() method returns the value that was "popped out": Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; var x = fruits.pop(); // the value of x is "Mango"

Back

String Length

Front

Returns the length of a string str.length

Back

join()

Front

The join() method also joins all array elements into a string. It behaves just like toString(), but in addition you can specify the separator: Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits.join(" * "); Result Banana Orange Apple * Mango

Back

toFixed()

Front

returns a string, with the number written with a specified number of decimals: Example var x = 9.656; x.toFixed(0); // returns 10 x.toFixed(2); // returns 9.66 x.toFixed(4); // returns 9.6560 x.toFixed(6); // returns 9.656000

Back

Math.random()

Front

Math.random() returns a random number between 0 (inclusive), and 1 (exclusive): Example Math.random(); // returns a random number

Back

Using splice() to remove elements

Front

With clever parameter setting, you can use splice() to remove elements without leaving "holes" in the array: Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(0, 1); // Removes the first element of fruits

Back

Math.sqrt(x)

Front

Math.sqrt(x) returns the square root of x: Example Math.sqrt(64); // returns 8

Back

charAt()

Front

returns the character at a specified index (position) in a string: Example var str = "HELLO WORLD"; str.charAt(0); // returns H

Back

Converting to Upper Case

Front

A string is converted to upper case with toUpperCase(): Example var text1 = "Hello World!"; // String var text2 = text1.toUpperCase(); // text2 is text1 converted to upper

Back

Numeric Sort

Front

You must use a compare function because sort() only applies to strings var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return a - b}); Use the same trick to sort an array descending: Example var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return b - a});

Back

Deleting Array Elements

Front

Since JavaScript arrays are objects, elements can be deleted by using the JavaScript operator delete: Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; delete fruits[0]; // Changes the first element in fruits to undefined

Back

indexOf()

Front

returns the index of the last occurrence of a specified text in a string: var str = "Please locate where 'locate' occurs!"; var pos = str.lastIndexOf("locate"); lastIndexOf()

Back

Math.round()

Front

Math.round(x) returns the value of x rounded to its nearest integer: Example Math.round(4.7); // returns 5 Math.round(4.4); // returns 4

Back

concat() arrays

Front

The concat() method creates a new array by merging (concatenating) existing arrays: Example (Merging Two Arrays) var myGirls = ["Cecilie", "Lone"]; var myBoys = ["Emil", "Tobias", "Linus"]; var myChildren = myGirls.concat(myBoys); // Concatenates (joins) myGirls and myBoys

Back

Math.floor(Math.random() * 10)

Front

Returns number between 0 and 9

Back

replace()

Front

replace() replaces a specified value with another value in a string: By default, the replace() function replaces only the first match Example str = "Please visit Microsoft!"; var n = str.replace("Microsoft", "W3Schools");

Back

sort()

Front

The sort() method sorts an array alphabetically: Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); // Sorts the elements of fruits

Back

shift()

Front

Shifting is equivalent to popping, working on the first element instead of the last. The shift() method removes the first array element and "shifts" all other elements to a lower index. Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.shift(); // Removes the first element "Banana" from fruits The shift() method returns the string that was "shifted out": Example var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.shift(); // Returns "Banana"

Back

search()

Front

searches a string for a specified value and returns the position of the match: var str = "Please locate where 'locate' occurs!"; var pos = str.search("locate");

Back

toPrecision()

Front

toPrecision() returns a string, with a number written with a specified length: Example var x = 9.656; x.toPrecision(); // returns 9.656 x.toPrecision(2); // returns 9.7 x.toPrecision(4); // returns 9.656 x.toPrecision(6); // returns 9.65600

Back

Converting to Lower Case

Front

A string is converted to lower case with toLowerCase(): Example var text1 = "Hello World!"; // String var text2 = text1.toLowerCase(); // text2 is text1 converted to lower

Back

Converting String to Array

Front

A string can be converted to an array with the split() method: Example var txt = "a,b,c,d,e"; // String txt.split(","); // Split on commas txt.split(" "); // Split on spaces txt.split("|"); // Split on pipe

Back

lastIndexOf()

Front

returns the index of the last occurrence of a specified text in a string: var str = "Please locate where 'locate' occurs!"; var pos = str.lastIndexOf("locate");

Back

Math.min() and Math.max()

Front

Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments: Example Math.min(0, 150, 30, 20, -8, -200); // returns -200 Math.max(0, 150, 30, 20, -8, -200); // returns 150

Back