Skip to content

Instantly share code, notes, and snippets.

@alexko30
Created May 16, 2018 18:23
Show Gist options
  • Save alexko30/0fadea06ac927f3892c7b1187eedaac9 to your computer and use it in GitHub Desktop.
Save alexko30/0fadea06ac927f3892c7b1187eedaac9 to your computer and use it in GitHub Desktop.
scoreWords.js
function scoreWords(sentence) {
sentence = sentence.split(' ');
let amountOfLetters = 0,
currentCounter = 0,
endCounter = 0,
index;
sentence.forEach((x, i) => {
x.split('').forEach(y => {
currentCounter++
});
if (currentCounter > endCounter) {
endCounter = currentCounter;
index = i;
}
});
console.log(index);
}
scoreWords('Hello, I am Alex, and I am from Ukraine'); // 8 index - longest item
scoreWords('1234, 123456, 12345678912345 1234567890'); // 3 index - longest item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment