DAY35:Playing with digits


Posted by birdbirdmurmur on 2023-08-17

題目連結

https://www.codewars.com/kata/5552101f47fc5178b1000050

解法

function digPow(n, p) {
    let arr = String(n).split('')
    let sum = 0

    for (let i = 0; i < arr.length; i++) {
        sum += (Math.pow(Number(arr[i]), (p + i)))
    }

    return sum % n == 0 ? sum / n : -1
}

筆記

先拆開n的每一位 導入陣列
設定sum
每一次迭代sum += arr[i]的(p+i)次方

迭代後sum能被原本的n整除 就正確


#javascript #Codewars







Related Posts

Explore-Arrays 101

Explore-Arrays 101

Google Cloud Source Repositories 使用紀錄

Google Cloud Source Repositories 使用紀錄

redis 套件的 Property 'on' does not exist on type 'RedisClientType'

redis 套件的 Property 'on' does not exist on type 'RedisClientType'


Comments