Monday, 24 February 2020

Diagonal Difference

Diagonal Difference


Objective

Given a square matrix, calculate the absolute difference between the sums of its diagonals.

Description

For example, the square matrix  is shown below:
1   2   3
4   5   6
9   8   9
The left-to-right diagonal = 1 +  5 + 9 = 15. The right to left diagonal = 3 + 5 + 9 = 17. Their absolute difference is |15 - 17 | = 2

Code


Here’s my solution, with embedded comments:

function diagonalDifference(arr) {

    //here arr contains [[ 1,2,3 ] [ 4,5,6 ] [ 9,8,9 ]]

    //it means we have 3x3 metrics in the argument

    //depends on the input, we can also have nxn square matrics

    len = arr.length

    let diagonal1=0, diagonal2=0

    for( i=0 ; i<len ; i++){

        diagonal1 + = arr[i] [i]

        diagonal2 + = arr[len-1-i] [i]

    }

    total = Math.abs(diagonal1 - diagonal2)

    return total

}


I hope you found this helpful.
Cheers :)

Saturday, 22 February 2020

Second Max Number

Second Max Number


Objective

Given an array, we have to find the second max element of an array

Sample Input

[32,5,26,78,33,1]

Sample Output

33

Code

Here's my solution with the embedded comments.



arr=[32,5,26,78,33,1]

arr.sort(function(a,b){
  return a-b
})

len=arr.length

console.log(arr[len-2]) 


I hope you found this helpful.
Cheers :)

Time Conversion

Time Conversion


Objective


Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.
NOTE: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.

Input Format

A single string containing a time in 12-hour clock format (i.e.:hh:mm:ssAM  orhh:mm:ssPM ), where 01<=hh<=12 and 00>=mm,ss<=59.

Output Format

Convert and print the given time in 24-hour format, where 00<=hh<=23.

Sample Input 

07:05:45PM

Sample Output 

19:05:45

Code

Here’s my solution, with embedded comments:


I hope you found this helpful.
Cheers :)

Friday, 21 February 2020

Minimum-Max Sum

Minimum-Max Sum



Objective


Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly three of the five integers.


Description


For example, arr=[1,3,5,7,9] . Our minimum sum is 1+3+5+7 and our maximum sum is 3+5+7+9. We would print 
Min - 16
Max - 24


Code


Here’s my solution, with embedded comments:



I hope you found this helpful.
Cheers :)

Reverse the String

Reverse the String


Objective

Reverse the provided string using the built-in function.


Solution


For this solution, you will use three methods: the String.prototype.split() method, the Array.prototype.reverse() method and the Array.prototype.join() method.


  • The split() method splits a String object into an array of the string by separating the string into substrings.
  • The reverse() method reverses an array in place. The first array element becomes the last and the last becomes the first.
  • The join() method joins all elements of an array into a string.



Code

Here’s my solution, with embedded comments:


I hope you found this helpful.
Cheers :)

Tuesday, 11 February 2020

Sorting

Sorting of Array


Objective

Given an array having an integer value in its element. We have to sort the array.

Sample Input

[1, 5, 12, 3, 7, 15, 9]

Sample Output

[1, 3, 5, 7, 9, 12, 15]


Code


Here’s my solution, with embedded comments:



I hope you found this helpful.
Cheers :)

Monday, 10 February 2020

Vowels and Consonants

Vowels and Consonants

Objective 

Print each vowel in a given name (in the same order as they appeared in ). Second, print each consonant (i.e., non-vowel) in the new line (in the same order as they appeared in).



Sample Input 

‘Hello World’

Sample Output

"e"
"o"
"o"
"H"
"l"
"l"
"l"
"d"
" "
"W"
"r"


Code


Here’s my solution, with embedded comments:







I hope you found this helpful.
Cheers :)



Diagonal Difference

Diagonal Difference Objective Given a square matrix, calculate the absolute difference between the sums of its diagonals. Descripti...