Find sum of iterations of variable in for loop

前端 未结 3 1182
谎友^
谎友^ 2021-01-17 00:31
for (i = 0; i <= 1000; i++) {
    if ( i % 3 === 0){
        console.log(i);
    }
    if ( i % 5 === 0){
        console.log(i);
    }

}

I wan

3条回答
  •  伪装坚强ぢ
    2021-01-17 01:13

    If you want to avoid counting multiple of 15 twice in your sum :

    int sum = 0;
    for (i = 0; i <= 1000; i++) {
        if ( i % 3 === 0 ||  i % 5 === 0){
            sum += i; // or sum = sum + i
        }
    }
    

提交回复
热议问题