Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. // Day 1.
  2. // Good morning! Write a function that takes an array of strings and return the longest string in the array.
  3. // For example:
  4. // const strings1 = ['short', 'really, really long!', 'medium'];
  5. // console.log(longestString(strings1)); // <--- 'really, really long!'
  6. // Edge case: If you had an array which had two "longest" strings of equal length, your function should just return the first one.
  7. // For example:
  8.  
  9. // const strings2 = ['short', 'first long string!!', 'medium', 'abcdefghijklmnopqr'];
  10.  
  11. // console.log(longestString(strings2)); // <--- 'first long string!'```
  12.  
  13. function longestString(arr) {
  14. return arr.reduce(function(acc, str) {
  15. return acc.length > str.length ? acc : str;
  16. });
  17. }
  18.  
  19. // Day 2.
  20. // Write a function called *reverseString* that accepts a string and returns a reversed copy of the string.
  21.  
  22. // Input Example:
  23.  
  24. // ```'hello world'
  25. // 'asdf'
  26. // 'CS rocks!'```
  27.  
  28. // Output Example:
  29.  
  30. // ```'dlrow olleh'
  31. // 'fdsa'
  32. // '!skcor SC'```
  33.  
  34. function reverseString(str) {
  35. return str.split("").reverse().join("");
  36. }
  37.  
  38. // Day 3.
  39. // Write a function called reverseNumber that reverses a number.
  40.  
  41. // ```Input Example:
  42. // 54321
  43. // 555
  44.  
  45. // Output Example:
  46. // 12345
  47. // 555```
  48.  
  49. function reverseNumber(num) {
  50. return parseInt(num.toString().split('').reverse().join('')) * Math.sign(num);
  51. }
  52.  
  53. // The Math.sign() function returns the sign of a number, indicating whether the number is positive, negative or zero.
  54.  
  55. console.log(reverseNumber(54321)); // 12345
  56. console.log(reverseNumber(-678)); // -876
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement