0323高途

156 阅读1分钟
var x = 10
function a(y) {
  var x = 20
  return b(y)
}
function b(y) {
  console.log(x + y)
}
a(20) 
// 30
Function.prototype.a = () => {
  console.log(1)
}
Object.prototype.b = () => {
  console.log(2)
}
function A() { }
const a = new A()

a.b() 2 
A.a() 1
a.__proto__  //A.prototype
A.prototype.__proto__  //Object
console.log(1)
setTimeout(() => {
  console.log(2)
})
Promise.resolve().then(() => {
  console.log(3)
})
setImmediate(() => {
  console.log(4)
})
new Promise(resolve => {
  console.log(5)
  resolve()
  console.log(6)
}).then(() => {
  console.log(7)
})
Promise.resolve().then(() => {
  console.log(8)
  Promise.resolve().then(() => {
    console.log(9)
  })
})

//1 5 6 3 7 8 9 2 4
实现转换方法,把原始 list 转换成树形结构
const list = [
  { key: 1, data: 'A', parentKey: 0 },
  { key: 2, data: 'B', parentKey: 0 },
  { key: 3, data: 'C', parentKey: 1 },
  { key: 4, data: 'D', parentKey: 1 },
  { key: 5, data: 'E', parentKey: 2 },
  { key: 6, data: 'F', parentKey: 3 },
  { key: 7, data: 'G', parentKey: 2 },
  { key: 8, data: 'H', parentKey: 4 },
];
const result = convert(list);
function convert(list) {
    // your code
    const res = [];
    const map = list.reduce((res, v) => ((res[v.key] = v), res), {});
    for (const item of list) {
        if (item.parentKey === 0) {
            res.push(item);
            continue;
        }
        if (item.parentKey in map) {
            const parent = map[item.parentKey];
            parent.children = parent.children || [];
            parent.children.push(item);
        }
    }
    return res;
}

// [
//   {
//     key: 1,
//     data: 'A',
//     parentKey: 0,
//     children: [
//       { key: 3, data: 'C', parentKey: 1, children: [{ key: 6, data: 'F', parentKey: 3 }] },
//       { key: 4, data: 'D', parentKey: 1, children: [{ key: 8, data: 'H', parentKey: 4 }] },
//     ],
//   },
//   {
//     key: 2,
//     data: 'B',
//     parentKey: 0,
//     children: [
//       { key: 5, data: 'E', parentKey: 2 },
//       { key: 7, data: 'G', parentKey: 2 },
//     ],
//   },
// ];

平衡点问题 一个数组中的元素,如果其前面的部分等于后面的部分,那么这个点的位序就是平衡点。 比如列表numbers = [1, 3, 5, 7, 8, 25, 4, 20],25前面的元素总和为24,25后面的元素总和也是24,那么25就是这个列表的平衡点。

function get(arr) {
    let sum = 0,
        sumleft = 0,
        sumright = 0;
    for (let i = 0; i < arr.length; i++) {
        sum += arr[i];
    }
    for (let j = 1; j < arr.length; j++) {
        sumleft += arr[j - 1];
        sumright = sum - sumleft - arr[j];
        while (sumleft === sumright) {
            return j;
        }
    }
}

js bridge相关