Return the array index as required

Given an array of integers [a,b,c…] , and the integer d, if a+b=d, return the array indices of a and b.

There is only one answer for each type of input. You cannot use the same element in an array twice.

Example:

Input,5,6,9 [1], 7

Output [0, 2]

Input,4,5,9,2 [3], 9

Output [1, 2]

Write a function that satisfies these requirements.

function fn(arr, num) {
            for (let i = 0; i < arr.length; i++) {
                for (let j = i + 1; j < arr.length; j++) {
                    if (arr[i] + arr[j] === num) {
                        return [i, j]
                    }
                }
            }
        }
        const res = fn([1.5.6.9].7)
        console.log(res);
Copy the code

Second, calculate the number of wechat movement steps

Known data of Xiaoming’s wechat movement steps from 2020-08-16 to 2020-09-15 are as follows:

// steps[]. Steps []. Timestamp Timestamp const steps = [{step: 1753, timestamp: 1597507200000}, {step: 1637, timestamp: 1597593600000 }, { step: 6216, timestamp: 1597680000000 }, { step: 6466, timestamp: 1597766400000 }, { step: 12998, timestamp: 1597852800000 }, { step: 7731, timestamp: 1597939200000 }, { step: 7115, timestamp: 1598025600000 }, { step: 3603, timestamp: 1598112000000 }, { step: 1659, timestamp: 1598198400000 }, { step: 3582, timestamp: 1598284800000 }, { step: 6979, timestamp: 1598371200000 }, { step: 4567, timestamp: 1598457600000 }, { step: 7639, timestamp: 1598544000000 }, { step: 9150, timestamp: 1598630400000 }, { step: 8565, timestamp: 1598716800000 }, { step: 6544, timestamp: 1598803200000 }, { step: 6316, timestamp: 1598889600000 }, { step: 15523, timestamp: 1598976000000 }, { step: 8254, timestamp: 1599062400000 }, { step: 13552, timestamp: 1599148800000 }, { step: 6163, timestamp: 1599235200000 }, { step: 6338, timestamp: 1599321600000 }, { step: 8508, timestamp: 1599408000000 }, { step: 4002, timestamp: 1599494400000 }, { step: 7501, timestamp: 1599580800000 }, { step: 5922, timestamp: 1599667200000 }, { step: 9635, timestamp: 1599753600000 }, { step: 7067, timestamp: 1599840000000 }, { step: 3878, timestamp: 1599926400000 }, { step: 4088, timestamp: 1600012800000 }, { step: 773, timestamp: 1600099200000 } ]Copy the code

Write a function that subtotals the number of steps in one of those time periods and returns the result

Example:

Input: 2020-09-01, 2020-09-07

Returns: 64654

        function fn(date1, date2) {
            const time = steps.filter(it= > {
                return it.timestamp >= +new Date(date1) && it.timestamp <= +new Date(date2)
            })
            return time.reduce((sum, it) = > sum + it.step, 0)}const time = fn('2020-09-01 00:00:00'.'the 2020-09-07 23:59:59')
        console.log(time);
Copy the code