// In terms of light cloning, It copies the value not the address for string type.
// If it is an array, it copies address, so, basically working differently
let a = 'abcd';
let b = a; // light clone
a.replace('a', '1'); // a = '1bcd' b = 'abcd'
let a = [1, 2, 3, 4];
let b = a; //light clone
a.pop(); // a = [1,2,3] b=[1,2,3]
.replace()
.replace(a,b) is exchange first letter of first argument to second argument. For convert every matching letter, put the letter inside regexp, / /g
.
'a b c d e f g'.replace(/ /g, ''); //abcdefg
'+A+B+C'.replace(/\+/g, '1'); // 1A1B1C
'A---B'.replace(/-/g, '0'); // A000B
'A:@#$@B'.replace(/[^a-z]/g, ''); // AB
let encrypted = ' + -- + - + - ';
let replaced = encrypted.replace(/ /g, '').replace(/\+/g, '1').replace(/-/g, '0');
console.log(replaced); // 1001010
There are a lot of ways to convert strings to numbers, but parseInt()
is recommended.
Plus, It enables to change the binary number to normal number by parseInt(n, 2)
console.log(typeof +'4'); // number
console.log(typeof Number('4')); // number
console.log(typeof parseInt('4')); // number
// using for verify number, typeof is not distinguishable to parseInt() or Number() stuff.
console.log(isNaN('4')); // false
let encrypted = ' + -- + - + - ';
let numberedFromBinary = parseInt(
encrypted.replace(/ /g, '').replace(/\+/g, '1').replace(/-/g, '0'),
2
);
console.log(numberedFromBinary); // 74
String.fromCharCode()
method considers number as an index of UTF-16 character so that it enables to change numbers to UTF-16 letter.
let encrypted = ' + -- + - + - ';
let numberToString = String.fromCharCode(
parseInt(encrypted.replace(/ /g, '').replace(/\+/g, '1').replace(/-/g, '0'), 2)
);
console.log(numberToString); // J
substr()
and substring()
are slightly different from each other.
The relation is similar to splice()
and slice()
.
const letters = 'abcdefg';
const divider = letters.indexOf('c');
const dividerPlusTwo = letters.indexOf('e');
letters.substring(divider); //cdefg
letters.substring(0, divider); //ab
// same as slice, it need end index number
letters.substring(divider, dividerPlusTwo); //cd
letters.substr(divider); //cdefg
letters.substr(0, divider); //ab
// same as splice, it need end count from start index
letters.substr(divider, 2); //cd
String.indexOf()
& String.lastIndexOf()
method can figure out the index number from the value.
indexOf()
starts to searching index from the right side of the from index
(second parameter), on the other hand, lastIndexOf()
checks the left side of from index
.
let someString = 'ksekksetk';
someString.indexOf('k'); // 0
someString.lastIndexOf('k'); // 8
// start searching from the index number 1. in here,
// searching start from 's'
someString.indexOf('k', 0); // 0
someString.indexOf('k', 1); // 3
someString.indexOf('k', 3); // 3
someString.lastIndexOf('k', 8); // 8
someString.lastIndexOf('k', 7); // 4
someString.lastIndexOf('k', 4); // 4
// if there is nothing matched, return -1
someString.indexOf('K'); // -1
someString.lastIndexOf('K'); // -1
Math.ceil(2.1); // 3
Math.floor(2.9); // 2
Math.round(2.5); // 3
Math.round(2.4); // 2
Math.sqrt(16); // 4
Number
methods for init number type valueWhen you looking for minimum or maximum value, you can initialize the number, not 0 but Number.MAX_SAFE_INTEGER
or Number.MIN_SAFE_INTEGER
.
Generally, an initial value of minimum value is Number.MAX_SAFE_INTEGER
, and the maximum is Number.MIN_SAFE_INTEGER
// this is maximum safe integer
let min = Number.MAX_SAFE_INTEGER; // 9007199254740991
let max = Number.MIN_SAFE_INTEGER;
split()
instead for ... of
when coutingIf you looking for the count of numbers for a certain letter, a loop is not the best way. You can split the sentence by the letter you looking for, and easily figure it out.
let words = 'heasdfheasdfh';
//find 'h' letter count, you can simply use split()
words.split('h'); // ['', 'easdf', 'easdf','']
words.split('h').length; // 4
// h count?
words.split('h').length - 1;
eval
for calculating number from stringlet stringCalculation = '12 + 5';
eval(stringCalculation); // 17
splice()
.splice()
method is used for finding element by index then delete, the first argument stands for index, the second argument means element count for delete from the searched index.
['a', 'b', 'c', 'd', 'e'].splice(3, 1); // ['a', 'b', 'c', 'e']
['a', 'b', 'c', 'd', 'e'].splice(2, 2); // ['a', 'b', 'e']
// splice should be careful to use, the order can make a huge difference in result.
['a', 'b', 'c', 'd', 'e'].splice(1, 1); // ['a', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e'].splice(3, 1); // ['a', 'b', 'c']
['a', 'b', 'c', 'd', 'e'].splice(3, 1); // ['a', 'b', 'c', 'e']
['a', 'b', 'c', 'd', 'e'].splice(1, 1); // ['a', 'd', 'e']
There are numerous way to generate array. Here is ‘fill()’ and ‘Array.from()‘.
// if you want to generate from nothing
Array.from({ length: 3 }, () => 0); // [0, 0, 0]
// if you want to generate array identically same with other array length but different value
let origin = [1, 2, 3, 4, 5];
// this generates new array
Array.from(origin, (x) => (x = 0)); // [0,0,0,0,0]
Array.from(origin, () => 0); // [0,0,0,0,0]
// edit existed array
origin.fill(0); // [0,0,0,0,0]
// for generate new array, use light clone
[...origin].fill(0); // [0,0,0,0,0]
++
for easing while loop// legacy convention fro while loop
// result : 0 1 2
let cnt = 0;
while (cnt < 3) {
console.log(cnt);
cnt++;
}
// Short version for while loop
// it runs console.log(cnt) first, then cnt++
// result: 0 1 2
let cnt = 0;
while (cnt < 3) console.log(cnt++);
// normal
let arr1 = [0, 3, 2, 4, 5, 6];
arr1.sort();
console.log(arr1); // [0,2,3,4,5,6]
// light copy
let arr2 = [0, 3, 2, 4, 5, 6];
let sort1 = arr.slice().sort();
let sort2 = [...arr2].sort();
console.log(arr2); // [0, 3, 2, 4, 5, 6]
console.log(sort1); // [0,2,3,4,5,6]
console.log(sort2); // [0,2,3,4,5,6]
// it's all about yes or no.
// usually for checking out subset style
// most of them is all about level and sum
// this example if looking for least count to the target
let target = 20;
let counts = [13, 4, 5, 4, 3, 1, 5, 9];
function findLeastCountTarget(t, c) {
let answer = Number.MAX_SAFE_INTEGER,
n = c.length;
function DFS(L, sum) {
if (L >= n) return;
if (sum + c[L] > t) {
answer = Math.min(answer, L);
} else {
DFS(L + 1, sum + c[L]);
DFS(L + 1, sum);
}
}
DFS(0, 0);
return answer;
}
console.log(findLeastCountTarget(target, counts));