Created
September 23, 2022 10:51
-
-
Save buymed-hoangpham/b3d2ae29e26ed2e082630608be2b124d to your computer and use it in GitHub Desktop.
Homework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //1. | |
| const subLength = (str, char) => { | |
| let charInStr = [], | |
| lowerStr = str.toLowerCase().split(''), | |
| length = 0; | |
| lowerStr.forEach((val, index) => { | |
| if (val === char) { | |
| charInStr.push(index); | |
| } | |
| }); | |
| if (charInStr.length !== 2) { | |
| return length; | |
| } | |
| return str.slice(charInStr[0], charInStr[1]).length + 1; | |
| }; | |
| // 2. | |
| const groceries = arr => { | |
| const len = arr.length; | |
| if (len == 1) | |
| return arr[0].item; | |
| else if (len == 2) | |
| return arr[0].item + ' and ' + arr[1].item; | |
| let str = ''; | |
| arr.forEach((obj, indx) => { | |
| if (indx + 1 == len) | |
| str += `and ${obj.item}`; | |
| else if (indx + 2 == len) | |
| str += `${obj.item} `; | |
| else | |
| str += `${obj.item}, `; | |
| }) | |
| return str; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment