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 - 6 of 6 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

Anshul

  • Jul 1st, 2019
 

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

  2.  

  3.  

  4. function secondLargest(num){

  5.  

  6.  

  7.   var first = num[0];

  8.   var second = num[0];

  9.  

  10.  

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

  12.  

  13.  

  14.     if(first < num[i]){

  15.                 second = first;

  16.         first = num[i];

  17.         }else{

  18.                 if(second < num[i]){

  19.                         second = num[i];

  20.         }

  21.     }

  22.  

  23.   return second;

  24. }

  25.  

  26. function reverse(str){

  27.  

  28.  

  29.   var rstr = "";

  30.  

  31.  

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

  33.  

  34.  

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

  36.  

  37.  

  38.   }

  39.  

  40.  

  41.   return rstr;

  42.  

  43. }

  44.  

  45. alert (secondLargest(numbers));

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

  47.  

  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