Skip to content

Instantly share code, notes, and snippets.

@NhanKhangg98
Created September 18, 2022 07:46
Show Gist options
  • Save NhanKhangg98/a90b7cca9375c8313ade98bdcbbd5a92 to your computer and use it in GitHub Desktop.
Save NhanKhangg98/a90b7cca9375c8313ade98bdcbbd5a92 to your computer and use it in GitHub Desktop.
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