64. A face questions seeking 1 + 2 + ... + n (JavaScript) --- seeking 1 + 2 + ... + n, requires multiplication and division can not be used, for, while, if, else, switch, case and the like keywords conditional statement (A B:? C)

One, Title Description

Seeking 1 + 2 + ... + n, requires multiplication and division can not be used, for, while, if, else, switch, case and keywords such as conditional statement (A B:? C).

Second, the example

Input: n = 3
Output: 6

Third, the problem-solving ideas

Because the specific calculation can not be used, I use && operator, && A B, where A qualified, enforcement B.

Fourth, the code

/**
 * @param {number} n
 * @return {number}
 */

var sumNums = function(n) {
    var sum = n;
    (n>0)&&(sum+=sumNums(n-1))
    return sum
};

Fifth, the results

Here Insert Picture Description

Published 64 original articles · won praise 22 · views 9010

Guess you like

Origin blog.csdn.net/qq_39897978/article/details/104747846