Compare commits

...

3 Commits

Author SHA1 Message Date
d9bba74979 Iterables:
add a [Symbol.Iterable] to make the object iterable so that the object can use for of loop
the [Symbol.Iterable] is a function, it should return a object includesdone and value
Array.from method can return Array.from a arraylike object and iterable object
string can be used in array.from as well
2024-06-18 00:11:16 +02:00
ad51aa4fb9 add some problems 2024-06-16 23:04:25 +02:00
3196f70784 add string.split and arr.join 2024-06-16 21:34:24 +02:00
2 changed files with 106 additions and 1 deletions

View File

@ -48,4 +48,54 @@ console.log(`arr.sort(): ${arr.sort(compare)}`);
console.log(`arr.sort(): ${arr.sort((a,b) => a - b)}`);
let countries = ['Österreich', 'Andorra', 'Vietnam'];
console.log(countries.sort((a,b) => a.localeCompare(b)))
console.log(countries.sort((a,b) => a.localeCompare(b)))
// arr.reverse()
countries = countries.reverse()
console.log(countries)
// arr.split() and arr.join()
let names = "Bilbo, Gandalf, Nazgul";
let nameLists = names.split(', ');
for(let name of nameLists){
console.log(`A message to ${name}`);
}
// [LOG - 6/16/24 21:32:14] "A message to Bilbo"
// [LOG - 6/16/24 21:32:14] "A message to Gandalf"
// [LOG - 6/16/24 21:32:14] "A message to Nazgul"
nameLists = names.split(', ', 2);
for(let name of nameLists){
console.log(`A message to ${name}`);
}
// [LOG - 6/16/24 21:32:14] "A message to Bilbo"
// [LOG - 6/16/24 21:32:14] "A message to Gandalf"
console.log(`nameLists: ${nameLists.join('; ')}`)
// [LOG - 6/16/24 21:33:45] "nameLists: Bilbo; Gandalf"
// arr.reduce(fn) arr.reduceRight(fn)
let numbers = [1,2,3,4,5]
console.log(numbers.reduce(function(sum, current){
return sum + current;
}, 0));
console.log(numbers.reduce((sum, cur) => sum + cur, 0));
console.log(numbers.reduceRight((sum, cur) => sum + cur, 0));
// Array.isArray()
console.log(Array.isArray(countries))
function camelize(str){
words = str.split('-')
for(let i = 1; i < words.length; i++){
words[i][0] = words[i][0].toUpperCase();
console.log(words[i])
}
return words.join('');
}
console.log(camelize("background-color"))
console.log(camelize("list-style-image"))
console.log(camelize("-webkit-translation"))

55
5.5Iterables/t.js Normal file
View File

@ -0,0 +1,55 @@
let range = {
from: 1,
to: 5
}
range[Symbol.iterator] = function(){
return {
current: this.from,
last: this.to,
next(){
if(this.current <= this.last){
return {done: false, value: this.current++};
} else {
return {done: true};
}
}
}
};
for(let num of range){
console.log(num);
}
for(let ch of "tests"){
console.log(ch)
}
let arrayLike = {
0: "Hello",
1: "World",
length: 2
};
// Array.from
let arr = Array.from(arrayLike);
console.log(arr.pop()); // World
arr = Array.from(range);
console.log(arr); //1, 2, 3, 4, 5
arr = Array.from(range, num => num * num);
console.log(arr); //1, 4, 9, 16, 25
let s = "split";
let split_s = s.split('');
let arr_s = Array.from(s);
console.log(split_s);
console.log(split_s[0]);
console.log(split_s[1]);
console.log(split_s.length);
console.log(arr_s);
console.log(arr_s[0]);
console.log(arr_s[1]);
console.log(arr_s.length);