javascript Рекурсія

rec
function flatten (arr) {
    const tempArr = [];
    
    (function deep(arr) {
        arr.forEach(item => Array.isArray(item) ? deep(item) : tempArr.push(item))
    })(arr);

    return tempArr;
}

function flatten (arr) {
    return arr.flat(Infinity);
}

const flatten = (arr) => arr.flat(Infinity);

flatten([1, 2, 3, [4, 5]]); // [1, 2, 3, 4, 5]
flatten([1, [2, [3, 4], [[5]]]]); // [1, 2, 3, 4, 5]
flatten([[1], [2], [3]]); // [1,2,3]
flatten([[[[1], [[[2]]], [[[[[[[3]]]]]]]]]]); // [1,2,3]

javascript Javascript的基础知识

index.js
let name = 'Haris';
let number = 5.5;

console.log(`${name} is ${number} years old!`);

if (true && true) {
  let name = 'Enver';
  
} else if (true || false) {
  if (true  === true) {
    // Execute something
  }else if (true !== false) {
    // Execute something
  }else {
    // Execute something
  }
}else {
  // Execute something
}

javascript OOPS_vs_OOLO.js

OOPS_vs_OOLO.js
// // OOPS Style
function Animal(species){
  this.species = species;
}

Animal.prototype.speak = function(){
  console.log(`I'm an animal belonging to the ${this.species} species`)
}

function Human(species,name,age){
  Animal.call(this,species)
  this.name = name;
  this.age = age;
}

Human.prototype = Object.create(Animal.prototype);
Human.prototype.constructor = Human 
Human.prototype.greet = function(){
  Animal.prototype.speak.call(this)
  console.log(`Hello, my name is ${this.name} age I'm ${this.age} years old`);
}

let devon = new Human("Homo Sapiens","Devon Reid",18);
devon.greet()

// OOLO Style

const Animal = {
  init(species){
    this.species = species;
  },
  speak(){
    console.log(`I'm an animal belonging to the ${this.species} species`)
  }
}

const Human = {
  create(species,name,age){
    this.init(species)
    this.name = name;
    this.age = age;
  },
  greet(){
    this.speak()
    console.log(`Hello, my name is ${this.name} age I'm ${this.age} years old`);
  }
}
Object.setPrototypeOf(Human, Animal)
const devon = Object.create(Human)
devon.create("Homo Sapiens","Devon Reid",18);
devon.greet()

javascript factorsSums.js

factorsSums.js
function factorsSum(num) {
    let res = [1],half = Math.floor(num / 2),i, j;
    num % 2 === 0 ? (i = 2, j = 1) : (i = 3, j = 2);
    for (i; i <= half; i += j) {
        if(num % i === 0) res.push(i)
    }
    return res.reduce((a,b)=>a+b);
}

console.log(factorsSum(36)) // 55

javascript javascript获取标题元素

title_tag.js
let title = document.getElementsByTagName('title')[0].innerHTML; // collect the text from the title element

javascript javascript检查元素是否出现在间隔中

element_appears_interval.js
let checkExist = setInterval(function() {
       if ($('#the-canvas').length) {
          console.log("Exists!");
          clearInterval(checkExist); // if it appeared, stop the loop
       }
    }, 100); // check every 100ms

javascript 独特的数组js

保存自https://dmitripavlutin.com/javascript-array-from-applications/

u.js
function unique(array) {
  return Array.from(new Set(array));
}

unique([1, 1, 2, 3, 3]); // => [1, 2, 3]

javascript Array.fill

保存自https://dmitripavlutin.com/javascript-array-from-applications/

u.js
const length = 3;
const resultA = Array.from({ length }, () => ({}));
const resultB = Array(length).fill({});

resultA; // => [{}, {}, {}]
resultB; // => [{}, {}, {}]

resultA[0] === resultA[1]; // => false
resultB[0] === resultB[1]; // => true

javascript 测试

保存自https://dmitripavlutin.com/javascript-array-from-applications/

u.js
const length = 3;
const init   = 0;
const result = Array(length).fill(init);

fillArray2(0, 3); // => [0, 0, 0]

javascript JavaScript的测试

JavaScript的测试

funcionesTesting_2.js
// funcionesTesting.js

// -
const esperoQue = ( dato ) => {
  return {
    seaIgualQue: ( esperado ) => {
      if ( dato !== esperado ) {
        throw new Error( `${ dato } es distinto que ${ esperado }` )
      }
    }
  }
}

// -
const prueba = ( titulo, funcion ) => {
  try {
    funcion()
    console.log( `✅  · ${ titulo }` )
  } catch ( error ) {
    console.error( `❌  · ${ titulo }` )
    console.error( error )
  }
}

module.exports = { esperoQue, prueba }