JAVASCRIPT difficulty


Hi
I am currently learning javascript and I came across this exercise question:
Code
  1. <html>

  2.         <head>

  3.         </head>

  4.         <body>

  5.                 <script language="javascript">

  6.                         var numbers = [-19, -7, -55, 78, 54, 60, -53, 35, 56, 42, 20, 77, 93, -41, -24, -60, 7, 86, 14, -99];

  7.                         alert (secondLargest(numbers));

  8.                         alert (reverse("CSY2028"));

  9.  

  10.                         function secondLargest(num){

  11.                                 return 0;

  12.                         }

  13.  

  14.                         function reverse(str){

  15.                                 return "";

  16.                         }

  17.                 </script>

  18.         </body>

  19. </html>

  20.  
Copyright GeekInterview.com

The scenario is that I have to
Define a function secondLargest() that takes an array and returned the 2nd largest number.
AND
Define a function reverse() that computes the reversal of a string. For example, reverse("Hello world") should return the string "I love Javascript".
I have had done many attempts to solve the task but I run into problems.
Any help? it would be appreciated

Questions by GlennYemoh

Showing Answers 1 - 1 of 1 Answers

Oleg

  • Dec 5th, 2014
 

Here is it.

Code
  1. var numbers = [-19, -7, -55, 78, 54, 60, -53, 35, 56, 42, 20, 77, 93, -41, -24, -60, 7, 86, 14, -99];

  2. function secondLargest(num){

  3.   var mn = num[0];

  4.   for(var i=1; i<num.length; i++)

  5.     if(mn < num[i])mn = num[i];

  6.   return mn;

  7. }

  8. function reverse(str){

  9.   var rstr = "";

  10.   for(var i=0; i<str.length; i++){

  11.     rstr = rstr + str.charAt(str.length - i -1);

  12.   }

  13.   return rstr;

  14. }

  15. alert (secondLargest(numbers));

  16. alert (reverse("CSY2028"));

  17.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions