Modify a given array by replacing each element with the sum or product of their digits based on a given condition | Function to modify the given array as per the given conditions ; Traverse the given array arr [ ] ; Initialize the count of even and odd digits ; Initialize temp with the current array element ; For count the number of even digits ; Increment the odd count ; Otherwise ; Divide temp by 10 ; Performe addition ; Performe multiplication ; Otherwise ; Driver Code
function evenOdd ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { let even_digits = 0 ; let odd_digits = 0 ; let temp = arr [ i ] ; while ( temp ) { if ( ( temp % 10 ) & 1 ) odd_digits ++ ; else even_digits ++ ; temp = parseInt ( temp / 10 ) } if ( even_digits > odd_digits ) { let res = 0 ; while ( arr [ i ] ) { res += arr [ i ] % 10 ; arr [ i ] = parseInt ( arr [ i ] / 10 ) ; } document . write ( res + " " ) ; } else if ( odd_digits > even_digits ) { let res = 1 ; while ( arr [ i ] ) { res *= arr [ i ] % 10 ; arr [ i ] = parseInt ( arr [ i ] / 10 ) } document . write ( res + " " ) ; } else document . write ( arr [ i ] + " " ) ; } } let arr = [ 113 , 141 , 214 , 3186 ] ; let N = arr . length ; evenOdd ( arr , N ) ;