﻿ CoffeeScript function: Second lowest and second greatest numbers from an array - w3resource

# CoffeeScript function: Second lowest and second greatest numbers from an array

## CoffeeScript Function : Exercise-16 with Solution

Write a CoffeeScript function which will take an array of numbers stored and find the second lowest and second greatest numbers, respectively.

Sample array : [1,2,3,4,5]
Expected Output : 2,4

HTML Code :

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<script src="//jashkenas.github.io/coffee-script/extras/coffee-script.js"></script>
<title>Second lowest and second greatest numbers from an array</title>
<body>

</body>
</html>

CoffeeScript Code :

Second_Greatest_Lowest = (arr_num) ->
arr_num.sort (x, y) ->
x - y
uniqa = [ arr_num[0] ]
result = []
j = 1
while j < arr_num.length
if arr_num[j - 1] != arr_num[j]
uniqa.push arr_num[j]
j++
result.push uniqa[1], uniqa[uniqa.length - 2]
result.join ','

console.log Second_Greatest_Lowest([
1
2
3
4
5
])

Sample Output:

"2,4"

Live Demo :

See the Pen coffeescript-exercise-16 by w3resource (@w3resource) on CodePen.

Improve this sample solution and post your code through Disqus.

﻿