var a = 255;
document.write(a.toString(16)+"<br>");
//Binärt:
document.write(a.toString(2)+"<br>");
// Returns the numeric value:
var str = "186";
document.write(str.valueOf()); Filter
2006-02-14
Konvertera numeriska format
Javascript
2006-02-14
Strängar i javascript
Javascript
// Alla strängar är objekt, så funktionerna anropas med punktnotation
charAt(index)
Returns a string containing the character at the specified index.
charCodeAt(index)
Returns the Unicode value of the character at the specified index.
concat(string)
Concatenates its argument to the end of the string that invokes the method.
The string invoking this method is not modified; rather a new String is returned.
This method is the same as adding two strings with the string concatenation
operator + (e.g., s1.concat( s2 ) is the same as s1 + s2).
fromCharCode(value1, value2, )
Converts a list of Unicode values into a string containing the corresponding characters.
indexOf(substring, startPos)
Searches for the first occurrence of substring, returns the starting index of
substring in the source string or –1 if substring is not found. If the startPos
argument is not provided, the method begins searching from index 0 in the source string.
lastIndexOf(substring, index)
Same as indexOf, but from end of string
slice(start, end)
Returns a string containing the portion of the string from index start through index end.
If the end index is not specified, the method returns a string from the start index to the
end of the source string. A negative end index specifies an offset from the end of the
string starting from a position one past the end of the last character (so, –1 indicates the
last character position in the string).
split(string)
Splits the source string into an array of strings (tokens) where its string argument
specifies the delimiter (i.e., the characters that indicate the end of each token in the source string).
substr(start, length)
Returns a string containing length characters starting from index start in the source string.
If length is not specified, a string containing characters from start to the end of the source string is returned.
substring(start, end)
Returns a string containing the characters from index start up to but not including index end in the source string.
toLowerCase()
Returns a string in which all uppercase letters are converted to lowercase letters. Non-letter characters are not changed.
toUpperCase()
Returns a string in which all lowercase letters are converted to uppercase letters. Non-letter characters are not changed.
toString()
Returns the same string as the source string.
valueOf()
Returns the same string as the source string.
2006-02-14
Binary Search
Javascript
// förutsätter sorterad array
function binarySearch(theArray, key) {
var low = 0; // low subscript
var high = theArray.length - 1; // high subscript
var middle; // middle subscript
while (low <= high) {
middle = (low + high) / 2;
if (key == theArray[middle]) // match
return middle;
else if (key < theArray[middle])
high = middle - 1; // search low end of array
else
low = middle + 1; // search high end of array
}
return -1; // searchKey not found
} 2006-02-14
Sortera arrayer
Javascript
/*
Defaults to string comparison
Method sort takes as its optional argument the name of a function that compares two arguments and returns a value of –1, 0 or 1.
*/
function compareIntegers(value1, value2) {
return parseInt(value1) - parseInt(value2);
}
var a = [ 10, 1, 9, 2, 8, 3, 7, 4, 6, 5 ];
document.writeln("Before: " + a.join(" ") + "<br />");
a.sort(compareIntegers); // sort the array
document.writeln("After: " + a.join(" ")); 2006-02-14
Arrayer
Javascript
var n1 = new Array( 5 ); // allocate 5-element Array
var n2 = new Array(); // allocate empty Array
var colors = new Array("cyan", "magenta", "yellow", "black");
var n1 = [ 10, 20, 30, 40, 50 ];
var n2 = new Array( 10, 20, 30, 40, 50 );
var n3 = [ 2, , , 8 ]; // 1 o 2 is undefined
// Multidimensionella...
var array1 = [ [ 1, 2, 3 ], // first row
[ 4, 5, 6 ] ]; // second row
var array2 = [ [ 1, 2 ], // first row
[ 3 ], // second row
[ 4, 5, 6 ] ]; // third row 