function fearNotLetter(str) {
let arr = [];
let arr1 = [];
for (let i = 0; i < str.length ; i++){
arr.push(str.charCodeAt([i]));
}
let indx = arr.length-1;
console.log(arr);
for (let j =arr[0] ; j <= arr[indx] ;j++ ){
arr1.push(j);
}
console.log(arr1);
var findElement = arr1.filter(function(element) {
return arr.includes(element) == false;
});
console.log(findElement);
console.log(String.fromCharCode(findElement));
if (findElement == false) {
return undefined
} else{
return String.fromCharCode
как улучшить мой код
function fearNotLetter(str) {
const missing = Array
.from(str, n => n.charCodeAt(0))
.find((n, i, a) => n !== a[0] + i);
return missing && String.fromCharCode(missing - 1);
}
function fearNotLetter(str) {
const letters = str.split("");
const letter = letters.reduce((acc, letter, index) => {
if (acc !== undefined) return acc;
const prevLetterCode = (letters[index - 1] || letter).charCodeAt();
if (letter.charCodeAt() - prevLetterCode > 1) return String.fromCharCode(prevLetterCode + 1)
return undefined;
}, undefined);
return letter;
}
fearNotLetter("abce");
//d
function fearNotLetter(str) {
var missingLetter;
str.split('').map(v => v.charCodeAt(0)).forEach((v,i,arr) => {
if (!missingLetter && arr[++i]) {
if ((++v) !== arr[i]) {
missingLetter = String.fromCharCode(v);
};
};
});
return missingLetter;
}
console.log(fearNotLetter("bcdf"));
function fearNotLetter(str) {
var missingLetter;
Array(str.length).fill(str.charCodeAt(0)).map((v,i) => v + i ).forEach(v => {
if (!missingLetter && !str.includes(String.fromCharCode(v))) {
missingLetter = String.fromCharCode(v);
};
});
return missingLetter;
}
console.log(fearNotLetter("stvwx"));
var fearNotLetter = str => {
str = str.split('');
if (str.length < 2) return undefined;
let res = [];
for (var i = 0; i < str.length-1; i++){
if (str[i].charCodeAt() > str[i+1].charCodeAt()) {
return ['Error'];
} else
if (str[i].charCodeAt()+1 !== str[i+1].charCodeAt()) {
res.push(String.fromCharCode(str[i].charCodeAt()+1));
str.splice(i+1, 0, String.fromCharCode(str[i].charCodeAt()+1));
i--;
}
}
return res.length > 0 ? res.join(', ') : undefined;
}
const fearNotLetter = str => {
// guard expression на случай пустой строки
if (str === "") {
return undefined;
}
// на всякий случай приводим строку к нижнему регистру
const normalizedString = str.toLowerCase();
// берём код первого символа, ниже в цикле это значение будет обновляться.
let charCode = normalizedString[0].codePointAt(0);
// перебираем строку посимвольно,
// если код текущего символа соответствует коду из переменной charCode,
// то увеличиваем charCode на единицу
// если код символа не соответствует ожидаемому коду из переменной charCode,
// значит мы нашли пропавший символ и вернём его.
for (let char of normalizedString) {
if (charCode === char.codePointAt(0)) {
charCode = charCode + 1;
} else {
return String.fromCodePoint(charCode);
}
}
};