Task on JS - StasBalan/check.github.io GitHub Wiki
(1).plus(2).minus(1).mull(2)
Number.prototype.plus= function (value) {
return this + value;
}
Number.prototype.minus = function (value) {
return this - value;
}
Number.prototype.mull = function (value) {
return this * value;
}
console.log((1).plus(2).minus(1).mull(2));
fact(5)
function fact(n) {
if(n!=1) {
return n * fact(n-1);
}else{
return n;
}
}
console.log(fact(1));
{a:{b:{c:{d:{e:null}}}}}
var arr = ['a', 'b', 'c', 'd', 'e'];
function newObj() {
return arr.reduceRight((acam, value) =>{
return {[value]: acam};
}, null);
}
console.log(newObj());
reverse(12334) //output 43321
function reverse(numb) {
let res = 0;
function f(num){
let b = num % 10;
let c = (num - b) / 10;
res += b;
if(c !== 0 ) {
res*=10;
return f(c);
}
return res;
}
return f(numb);
}
reverse(12334);
myFunc(add)(1)(2) && myFunc(mul)(3)(2)
function myFunc(callback) {
return function(a) {
return function(b) {
return callback(a, b);
}
}
}
function add(a, b) {
return a + b;
}
function mul(a, b) {
return a * b;
}
console.log(myFunc(add)(1)(2)); //3
console.log(myFunc(mul)(3)(2));
ДЕРЕВО
var sum = 0;
function getSum(obj) {
sum += obj.valueNode;
if(obj.next !== null) {
for(var i = 0; i < obj.next.length; i++) {
getSum(obj.next[i]);
}
}
return sum;
}
let tree = {
valueNode: 1,
next: [
{
valueNode: 3,
next: [
{
valueNode: 8,
next: null
}
]
},
{
valueNode: 2,
next: null
}
]
};
console.log(getSum(tree));
generator.next()
function makeGenerator(arr) {
var index = 0;
var obj = {
next() {
index++;
if(index <= arr.length) {
console.log(arr[index-1]);
}else {
console.log('complete');
}
}
}
return obj;
}
var generator = makeGenerator([1, 2, 3]);
generator.next();
generator.next();
generator.next();
generator.next();
generator.next();
generator.next();
sum(1)(2)(4)(5)
function sum (a) {
var current = a;
function foo(b) {
current += b
return foo;
}
foo.toString = function() {
return current;
}
return foo;
}
console.log(sum(1)(2)(4)(5));
sum(5)(-1)(2)()
function sum(a) {
var currentSum = a;
return function (b) {
if(b) {
return sum(currentSum + b);
}else {
return currentSum;
}
}
}
console.log(sum(1)(2)());
console.log(sum(5)(-1)(2)());
ЗАПРОС К СЕРВЕРУ
function get(url) {
return new Promise((res, rej) => {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.addEventListener('load', function() {
if(xhr.status < 400) {
res(xhr.response);
}else {
rej(new Error('Failed: ' + xhr.statusText));
}
});
xhr.addEventListener('error', function() {
rej(new Error('network Error'));
});
xhr.send();
});
}
get('https://jsonplaceholder.typicode.com/posts?userId=1')
.then(text => {
const block = JSON.parse(text);
add(block);
})
.catch(error => {console.log(error)});
function add(block) {
console.log(block);
for(let i = 0; i < block.length; i++){
var sd = block[i].title;
console.log(sd);
var div = document.getElementById('muIP');
div.innerHTML += <li>${sd}</li>
;
}
}
polindrom(str)
var str = 'asdsa';
function polindrom(str) {
var str1 = str.split('').reverse().join('');
if(str === str1) {
console.log('Poli');
}else {
console.log('net');
}
}
polindrom(str);
a.duplicate()
const a = [1, 2, 3];
Array.prototype.duplicate = function(a) {
return this.concat(this);
}
console.log(a.duplicate());