Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Day 1.
- // Good morning! Write a function that takes an array of strings and return the longest string in the array.
- // For example:
- // const strings1 = ['short', 'really, really long!', 'medium'];
- // console.log(longestString(strings1)); // <--- 'really, really long!'
- // Edge case: If you had an array which had two "longest" strings of equal length, your function should just return the first one.
- // For example:
- // const strings2 = ['short', 'first long string!!', 'medium', 'abcdefghijklmnopqr'];
- // console.log(longestString(strings2)); // <--- 'first long string!'```
- function longestString(arr) {
- return arr.reduce(function(acc, str) {
- return acc.length > str.length ? acc : str;
- });
- }
- // Day 2.
- // Write a function called *reverseString* that accepts a string and returns a reversed copy of the string.
- // Input Example:
- // ```'hello world'
- // 'asdf'
- // 'CS rocks!'```
- // Output Example:
- // ```'dlrow olleh'
- // 'fdsa'
- // '!skcor SC'```
- function reverseString(str) {
- return str.split("").reverse().join("");
- }
- // Day 3.
- // Write a function called reverseNumber that reverses a number.
- // ```Input Example:
- // 54321
- // 555
- // Output Example:
- // 12345
- // 555```
- function reverseNumber(num) {
- return parseInt(num.toString().split('').reverse().join('')) * Math.sign(num);
- }
- // The Math.sign() function returns the sign of a number, indicating whether the number is positive, negative or zero.
- console.log(reverseNumber(54321)); // 12345
- console.log(reverseNumber(-678)); // -876
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement