悬赏题

第一题


/**
  * 实现一个event类
  * 可以任意添加该类的成员属性或者方法,
  * 实现on方法的逻辑
  * 实现emit方法的逻辑
  * 最终按照下方的调用能够展示正确的结果
  */

class Event{
  on(){
    // 实现该方法逻辑
  }
  emit(){
    // 实现该方法逻辑
  }
}

let ev = new Event();

ev.on('first', () => {
  console.log('first-console-1');
});

ev.on('second', () => {
  console.log('second-console-1');
});

ev.on('first', () => {
  console.log('first-console-2');
});
ev.on('second', () => {
  console.log('second-console-2');
});

ev.emit('first'); // first-console-1  first-console-2
ev.emit('second'); // second-console-1 second-console-2

第二题
判断两个数组每一项是否相等

console.log(exec([0,2,6,9,10],[4,5,7,11,12]));

// 完善exec 最终打印
[ { left: 0, right: 5 },
  { left: 2, right: 4 },
  { left: 6, right: 7 },
  { left: 9, right: 12 },
  { left: 10, right: 11 } ]

第三题
矩阵转换 横纵变换

const initArr = [
  [1, 2, 3],
  [4, 5, 6]
];

console.log(change(initArr))
//打印:
[ 
	[ 1, 4 ], 
	[ 2, 5 ], 
	[ 3, 6 ] 
]

第四题
模拟链式调用

//实现一个类 可以链式调用
Person('li').sleep(1000).sleep(2000).sleepFirst(5000).eat('dinner').sleep(1000).eat('food');


//打印效果如下
//延时5s
sleep before 5000
This is li
//1s后
sleep after 1000
//2s后
sleep after 2000
eat dinner
//1s后
sleep after 1000
eat food

第五题

/*
	函数中两个参数  参数a是字符串 查找参数b是否包含a的字符串 如果包含 返回下标 不包含 返回false
*/

const a = '5678'
const b = '156785678';
console.log(findStr(a,b));//1

const a = '56789'
const b = '156785678';
console.log(findStr(a,b));//false

第六题

/*判断两个数组是否相同*/

const arr1 = [1,2,2,3,3,1]
const arr2 = [2,2,1,1,3,3]
console.log(isCommon(arr1,arr2));//true

const arr1 = [1,2,10,3,3,1]
const arr2 = [2,2,1,1,3,3]
console.log(isCommon(arr1,arr2));//false

第七题

/**实现一个函数 能够统计字符串个数**/
let str = 'aabbbccccdd';
var c = getChar(str);
console.log(c);//{ a: 2, b: 3, c: 4, d: 2 }

答案如下:
第一题

/**
  * 实现一个event类
  * 可以任意添加该类的成员属性或者方法,
  * 实现on方法的逻辑
  * 实现emit方法的逻辑
  * 最终按照下方的调用能够展示正确的结果
  */

class Event{
  /* 答案 */
  constructor(){
    this.events = {};
  }
  on(type,listener){
    // 实现该方法逻辑(答案)
    if(!Array.prototype.isPrototypeOf(this.events[type])){
      this.events[type] = [];
    }
    this.events[type].push(listener);
  }
  emit(){
    // 实现该方法逻辑(答案)
    this.events[type].forEach((item) => {
      if(item.onlyOnce){
        if(item.active){
          item(...reset);
          item.active = false;
        }
      }else{
        item(...reset);
      }   
    });
  }
}

let ev = new Event();

ev.on('first', () => {
  console.log('first-console-1');
});

ev.on('second', () => {
  console.log('second-console-1');
});

ev.on('first', () => {
  console.log('first-console-2');
});
ev.on('second', () => {
  console.log('second-console-2');
});

ev.emit('first'); // first-console-1  first-console-2
ev.emit('second'); // second-console-1 second-console-2

第二题

/**
 *
 * @param {Array} lefts 左括号的索引集合
 * @param {*} rights 右括号的索引集合
 */
function exec(lefts,rights){
    const all = lefts.concat(rights);
    const execRes = [];
    lefts.forEach(left => {
        let rightIndex = rights.findIndex(right => {
            let leftNum = findNumsAtSection(left,right,lefts);
            let rightNum = findNumsAtSection(left,right,rights);
            let result = findNumsAtSection(left,right,all)
            return isOdd(result) && (leftNum === rightNum);
        });
        let right = rights[rightIndex];
        rights.splice(rightIndex,1);
        execRes.push({left,right});
    });
    return execRes;
}

function findNumsAtSection(start,end,arr){
    let all = 0;
    arr.forEach(item => {
        if(item < end && item > start){
            all ++;
        }
    });
    return all;
}
function isOdd(num){
    return num % 2 === 0;
}

// const str = '[[],[],[]][]'
const str = '[o[j]][]w[[]]fo';
console.log(exec([0,2,6,9,10],[4,5,7,11,12]));

第三题

const initArr = [
  [1, 2, 3],
  [4, 5, 6]
];

function change(initArr) {
  const newArr = [];
  initArr[0].forEach((item, key) => {
    const itemArr = [];
    initArr.forEach(val => {
      itemArr.push(val[key]);
    });
    newArr.push(itemArr);
  });
  return newArr;
}

console.log(change(initArr));

第四题

/*eslint-disable no-console */
function Person(str){
  if(!Person.prototype.isPrototypeOf(this)){
    return new Person(str);
  }
  this.tasks = [];
  this.init(str);
  setTimeout(() => {
    this.run();
  },0);
}
Person.prototype.append = function(task,reset){
  this.tasks.push({task,reset});
};
Person.prototype.insertFront = function(task,reset){
  this.tasks.unshift({task,reset});
};
Person.prototype.run = async function(){
  for(let task of this.tasks){
    await task.task(task.reset);
  }
};
Person.prototype.init = function(str){
  async function task(str){
    console.log(`This is ${str}`);
  }
  this.append(task,str);
};
Person.prototype.setPromise = function(callback){
  return new Promise(reslove => {
    callback(reslove);
  });
};
Person.prototype.sleep = function(time){
  const that = this;
  async function task(time){
    await that.setPromise((reslove) => {
      setTimeout(() => {
        reslove();
      },time);
    });
    console.log(`sleep after ${time}`);
  }
  this.append(task,time);
  return this;
};
Person.prototype.sleepFirst = function(time){
  const that = this;
  async function task(time){
    await that.setPromise(reslove => {
      setTimeout(() => {
        reslove();
      },time);
    });
    console.log(`sleep before ${time}`);
  }
  this.insertFront(task,time);
  return this;
};
Person.prototype.eat = function(eadtd){
  async function task(eadtd){
    console.log(`eat ${eadtd}`);
  }
  this.append(task,eadtd);
  return this;
};
Person('li').sleep(1000).sleep(2000).sleepFirst(5000).eat('dinner').sleep(1000).eat('food');

第五题

/*
	函数中两个参数  参数a是字符串 查找参数b是否包含a的字符串 如果包含 返回下标 不包含 返回false
*/
const a = '5678'
const b = '156785678';
function findStr(a,b){
  if(b.split(a).length>1){
    return b.indexOf(a)
  }else{
    return false
  }
}
console.log(findStr(a,b));//1

const a = '56789'
const b = '156785678';
console.log(findStr(a,b));//false

第六题

/*判断两个数组是否相同*/
function isCommon(arr1,arr2){
  return fn(arr1) ===fn(arr2)?true:false
  function fn(arr){
    let Obj = {}
    arr.forEach(item=>{
      if(!Obj[item]){
        Obj[item] = 1
      }else{
        Obj[item] +=1
      }
    })
    return JSON.stringify(Obj)
  }
}
// const arr1 = [1,2,2,3,3,1]
// const arr2 = [2,2,1,1,3,3]
// console.log(isCommon(arr1,arr2));//true

const arr1 = [1,2,10,3,3,1]
const arr2 = [2,2,1,1,3,3]
console.log(isCommon(arr1,arr2));//false

第七题

/**实现一个函数 能够统计字符串个数**/
let str = 'aabbbccccdd';
// function getChar(str){     //一种解法  
//   let obj = {}
//   str.split('').forEach(item=>{
//       if(obj[item]===undefined){
//         obj[item]=1
//       }else{
//         obj[item] +=1
//       }
//   })
//   return obj
// }
function getChar(str){     //二种解法
  let obj = {}
  for(var i=0;i<str.length;i++){
    if(obj[str[i]]){
      obj[(str[i])] +=1
    }else{
      obj[str[i]] = 1
    }
  }
  return obj
}
var c = getChar(str);
console.log(c);//{ a: 2, b: 3, c: 4, d: 2 }
发布了25 篇原创文章 · 获赞 10 · 访问量 1445

猜你喜欢

转载自blog.csdn.net/weixin_45077178/article/details/103142355
今日推荐