preface

Nuggets team number online, help you Offer impromptu! Click for details

Topic describes

Their thinking

  • Use two arrays to store odd and even numbers
  • Use the for loop to iterate over each element of the input array
  • When the mod of 2 is 0, the target element is an even number, the even number is added to the array, and the odd number is added to the array
  • The final answer is returned by combining the two arrays into one using the extended operator in ES6

The problem solving code

var exchange = function(nums) {
    const arr1 = [];
    const arr2 = [];
    for (let v of nums) {
        if (v % 2= = =0) {
            arr2.push(v);
        } else{ arr1.push(v); }};return [...arr1,...arr2];
};
Copy the code

conclusion

  • This is a problem that can be solved by a simple loop.
  • The key to this problem is the idea of using two arrays to store odd and even numbers.
  • Know how to distinguish odd and even numbers.
  • Knowing how to merge arrays using extended characters is what we’re going to learn.