Created
September 18, 2022 07:46
-
-
Save NhanKhangg98/a90b7cca9375c8313ade98bdcbbd5a92 to your computer and use it in GitHub Desktop.
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
| function subLength(str, char){ | |
| let count = 0 | |
| for(let i = 0; i < str.length; i++){ | |
| if(str[i] === char) count++; | |
| } | |
| if(count !== 2) console.log(0) | |
| else console.log (str.slice(str.indexOf(char), str.lastIndexOf(char)+ 1).length) | |
| } | |
| subLength('Saturday', 'a'); // returns 6 | |
| subLength('summer', 'm'); // returns 2 | |
| subLength('digitize', 'i'); // returns 0 | |
| subLength('cheesecake', 'k'); // returns 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment