Posts

Javascript

calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with places after the decimal.

Image
Given an array of integers, calculate the ratios of its elements that are  positive ,  negative , and  zero . Print the decimal value of each fraction on a new line with   places after the decimal. function plusMinus ( arr ) {   let minusNumber = 0   let plusNumber = 0   let ZeroNumber = 0     for ( let i = 0 ; i < arr . length ; i ++ ){       if ( arr [ i ] < 0 ){           minusNumber += 1       } else if ( arr [ i ] > 0 ){           plusNumber += 1       } else {           ZeroNumber += 1       }         }     let   plus = plusNumber / arr . length   let zero = ZeroNumber / arr . length   let minus = ( minusNumber / arr . length )   console . log ( plus . toFixed ( 6 ));   console . log ( minus . toFixed ( 6 )) ...

calculate the absolute difference between the sums of its diagonals.

Image
  Given a square matrix, calculate the absolute difference between the sums of its diagonals. For example, the square matrix   is shown below: function diagonalDifference ( arr ) {   let arrayOne = 0   let arrayTwo = 0 for ( let i = 0 ; i < arr . length ; i ++ ){       const k = ( arr . length - 1 - i )       const array1 = arr [ i ][ i ]       const array2 = arr [ i ][ k ]       arrayOne += array1       arrayTwo += array2   } if ( arrayOne - arrayTwo < 0 ){   return Number (( arrayOne - arrayTwo ) * - 1 ) } else {   return (( arrayOne - arrayTwo ) ) }   }

minimum and maximum values that can be calculated

Image
  Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. function miniMaxSum ( arr ) {   let sum = 0   for ( let i = 0 ; i < arr . length ; i ++ ){       sum += arr [ i ]   }   let minus = []   for ( let j = arr . length ; j >= 0 ; j -- ){       let minusSum = sum       minus . push ( sum - arr [ j ])   }   let smallest = Infinity ;   let greatest = - Infinity ; for ( let i = 0 ; i < minus . length ; i ++ ) {   if ( minus [ i ] < smallest ) {       smallest = minus [ i ];   }   if ( minus [ i ] > greatest ) {       greatest = minus [ i ];   } } console . log ( smallest , greatest ); }

TimeConversion in 24 hour format

Image
Given a time in -hour AM/PM format, convert it to military (24-hour) time. Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock. - 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock. function   convertionTime ( time ) {   const PMTime =   time . toString (). search ( "PM" )     const AmTime =   time . toString (). search ( "AM" )     let timeString =   time . split ( ":" )   if ( PMTime !== - 1 ) {     if ( Number ( timeString [ 0 ]) < 12 ){     timeString [ 0 ] = Number ( timeString [ 0 ]) + 12     }   } else if ( AmTime !== - 1 ){     if ( Number ( timeString [ 0 ]) == 12 ){     timeString [ 0 ] = "00"     }   }   const joinedString = timeString . join ( ":" ). slice ( 0 , 8 )   return joinedString } const HourFormat =   convertionTime ( "01:05:45PM" ) console . log ( HourFormat )