Sharing of the 2018 Ali society's recruitment test questions

Here are the written test questions for Ali in early 2018. Why do you share it now? It's just because you were lazy. Only share questions, no answers.

1. Please use the code to achieve the following effect

function add(num){
    var total = 0; 
    var curryAdd = function(num){
        total = total + num;
        return total;
    }
    return curryAdd;
}

console.log(add(2)(3)); //The result is 5
console.log(add(2)(3)(4)(5)); // The result is 14

2. Please answer the output information of two console.log in the code in the browser console before and after clicking the button
And explain the reasons behind the phenomenon

let count = 0;

class MyComponent extends React.Component{
  constructor(){
    super();
    this.state = {
      count : count
    };
  }
  
  componentWillMount(){
    this.setState({
      count : ++count
    });
    
    this.setState({
      count : ++count
    });
    
    setTimeout(() => {
      this.setState({
        count : ++count
      });

      this.setState({
        count : ++count
      });
    }, 1000);
  }
  
  componentDidMount(){
    this.button.addEventListener('click', this.onClick.bind(this, 'Native browser events'), false);
  }
  
  onClick(info) {
    console.log(info);
    
    this.setState({
      count : ++count
    });
    
    this.setState({
      count : ++count
    });
  }
  
  render() {
    console.log(this.state.count);
    return (
      <div>
        <button type="button" ref={node => this.button = node} onClick={this.onClick.bind(this, 'React Event')}>Generate new count</button>
        <div>Count : {this.state.count}</div>
      </div>
    );
  }
}

ReactDOM.render(<MyComponent />, mountNode);

3. Simple implementation of throttle


function throttle(func, duration) {
        // Write specific implementation here
}

window.addEventListener('scroll', throttle(func, 50), false);

4. Implement a depth first search algorithm (non recursive)

    
function dfs(tree, name){
    // Please implement it here
}

var tree = {
    name : 'China',
    children : [
        {
            name : 'Beijing',
            children : [
                {
                    name : 'Chaoyang crowd'
                },
                {
                    name : 'Haidian District'
                },
                {
                    name : 'Changping District'
                }
            ]
        },
        {
            name : 'Zhejiang Province',
            children : [
                {
                    name : 'Hangzhou City',
                    code : 0571,
                },
                {
                    name : 'Jiaxing City'
                },
                {
                    name : 'Shaoxing City'
                },
                {
                    name : 'Ningbo City'
                }
            ]
        }
    ]
};

var node = dfs(tree, 'Hangzhou City');
console.log(node);    // {name: 'Hangzhou city', code: 0571}

5. Write a simple custom event handler: 1. Have on method bound events, 2. Have off method unbound events


function EventEmitter () {
    // TODO

}

var emitter = EventEmitter();

emitter.on('foo', function(e){
    console.log('listening foo event 1', e);
});

emitter.on('foo', function(e){
    console.log('listening foo event 2', e);
});

emitter.on('bar', function(e){
    console.log('listening bar event', e);
});

// Listen to all events
emitter.on('*', function(e){
    console.log('listening all events');
});

emitter.trigger('foo', {name : 'John'});
emitter.trigger('bar', {name : 'Sun'});
emitter.trigger('*', {name : 'Sun'});
emitter.off('foo');

6. Please use native JS to achieve DOM dragging effect, and try to consider compatibility and other possible situations as much as possible

Tags: Javascript React

Posted on Sun, 01 Dec 2019 07:21:03 -0800 by kpetsche20