You need to sort the word alphabetically with the exception of duplicate letters.
let's say there is a трафарет word -> there will be an аертф .
that the word can be accessed as an array I am aware of, but .sort() does not apply.
You need to sort the word alphabetically with the exception of duplicate letters.
let's say there is a трафарет word -> there will be an аертф .
that the word can be accessed as an array I am aware of, but .sort() does not apply.
You can, for example, like this:
var original = 'трафарет', sorted = '', res = ''; sorted = original.split('').sort(function (a, b) { return a.localeCompare(b); }).join(''); for (var i = 0, l = sorted.length; i < l; i++) { if (res.indexOf(sorted[i]) > -1) { continue; } res += sorted[i]; } console.log(res); indexOf(sorted[i]) really shorter :-) Fixed. - Dmitriy Simushevres.length > 0 && res[res.length - 1] === sorted[i] in the condition; it is faster, but worse to read. - Dmitriy SimushevindexOf :-) - Dmitriy SimushevSort, then exclude duplicates:
function letters( word) { var alphabet = word.split('').sort().join('') ,i ,prev ,out='' ; for(i=0; i<alphabet.length; i++) { if( prev==alphabet.charAt(i)) continue; prev = alphabet.charAt(i); out = out + prev; } return out; } document.body.innerHTML = letters('трафарет'); // аертф Source: https://ru.stackoverflow.com/questions/448503/
All Articles
str.split("")-> array of letters - Vismanstring.split('').sort().join('')- Sergiks