Sum of nth terms of Modified Fibonacci series made by every pair of two arrays | javascript program to find sum of n - th terms of a Fibonacci like series formed using first two terms of two arrays . ; if sum of first term is required ; if sum of second term is required ; fibonacci series used to find the nth term of every series ; as every b [ i ] term appears m times and every a [ i ] term also appears m times ; m is the size of the array
function sumNth ( A , B , m , n ) { var res = 0 ; if ( n == 1 ) { for ( let i = 0 ; i < m ; i ++ ) res = res + A [ i ] ; } else if ( n == 2 ) { for ( let i = 0 ; i < m ; i ++ ) res = res + B [ i ] * m ; } else { var f = Array ( n ) . fill ( 0 ) ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; for ( let i = 2 ; i < n ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; for ( i = 0 ; i < m ; i ++ ) { res = res + ( m * ( B [ i ] * f [ n - 1 ] ) ) + ( m * ( A [ i ] * f [ n - 2 ] ) ) ; } } return res ; } var A = [ 1 , 2 , 3 ] ; var B = [ 4 , 5 , 6 ] ; var n = 3 ; var m = A . length ; document . write ( sumNth ( A , B , m , n ) ) ;