I'm working on a bit of code that will search through a string and return any letters of the alphabet that are missing. This is what I have:
function findWhatsMissing(s){
var a = "abcdefghijklmnopqrstuvwxyz";
//remove special characters
s.replace(/[^a-zA-Z]/g, "");
s = s.toLowerCase();
//array to hold search results
var hits = [];
//loop through each letter in string
for (var i = 0; i < a.length; i++) {
var j = 0;
//if no matches are found, push to array
if (a[i] !== s[j]) {
hits.push(a[i]);
}
else {
j++;
}
}
//log array to console
console.log(hits);
}
But using the test case: findWhatsMissing("d a b c");
Results in all the letters before d being added to the missing array.
Any help would be greatly appreciated.
I'm working on a bit of code that will search through a string and return any letters of the alphabet that are missing. This is what I have:
function findWhatsMissing(s){
var a = "abcdefghijklmnopqrstuvwxyz";
//remove special characters
s.replace(/[^a-zA-Z]/g, "");
s = s.toLowerCase();
//array to hold search results
var hits = [];
//loop through each letter in string
for (var i = 0; i < a.length; i++) {
var j = 0;
//if no matches are found, push to array
if (a[i] !== s[j]) {
hits.push(a[i]);
}
else {
j++;
}
}
//log array to console
console.log(hits);
}
But using the test case: findWhatsMissing("d a b c");
Results in all the letters before d being added to the missing array.
Any help would be greatly appreciated.
Share Improve this question edited Feb 24, 2016 at 21:26 jcubic 66.6k58 gold badges249 silver badges450 bronze badges asked Feb 24, 2016 at 21:25 J. JohnsonJ. Johnson 911 silver badge3 bronze badges 2-
1
s.replace()
should bes = s.replace()
. – brso05 Commented Feb 24, 2016 at 21:28 -
You're only checking the first letter of
s
...s[j]
is alwayss[0]
. You should probably use 2 loops if you want to do it that way. Check every letter ofs
for every letter ofa
. – brso05 Commented Feb 24, 2016 at 21:31
5 Answers
Reset to default 7Inside your loop, you can use indexOf()
to see if the letter exists in your input. Something like this would work:
for (var i = 0; i < a.length; i++) {
if(s.indexOf(a[i]) == -1) { hits.push(a[i]); }
}
Hope that helps! You can see it working in this JS Fiddle: https://jsfiddle/573jatx1/1/
As Adam Konieska says. Something like this will work:
function findWhatsMissing(s) {
var a = "abcdefghijklmnopqrstuvwxyz";
s = s.toLowerCase();
var hits = [];
for (var i = 0; i < a.length; i++) {
if(s.indexOf(a[i]) == -1) { hits.push(a[i]); }
}
console.log(hits);
}
findWhatsMissing("d a b c");
Can use Array.prototype.filter()
and within each loop check string using indexOf()
function findWhatsMissing(s){
var a = "abcdefghijklmnopqrstuvwxyz";
//remove special characters
s = s.replace(/[^a-zA-Z]/g, "");
s = s.toLowerCase();
return a.split('').filter(function(letter){
return s.indexOf(letter) === -1;
});
}
alert( findWhatsMissing('d f v'))
You can use indexOf:
function findWhatsMissing(s){
var a = "abcdefghijklmnopqrstuvwxyz";
//remove special characters
s = s.replace(/[^a-zA-Z]/g, "");
s = s.toLowerCase();
//array to hold search results
var hits = [];
//loop through each letter in string
for (var i = 0; i < a.length; i++) {
//if no matches are found, push to array
if (s.indexOf(a[i]) == -1) {
hits.push(a[i]);
}
}
//log array to console
return hits;
}
alert(JSON.stringify(findWhatsMissing(' d a b c ')));
or two for loops:
function findWhatsMissing(s){
var a = "abcdefghijklmnopqrstuvwxyz";
//remove special characters
s = s.replace(/[^a-zA-Z]/g, "");
s = s.toLowerCase();
//array to hold search results
var hits = [];
//loop through each letter in string
for (var i = 0; i < a.length; i++) {
//if no matches are found, push to array
var found = false;
for (var j = 0; j < s.length; j++) {
if (s[j] == a[i]) {
found = true;
break;
}
}
if (!found) {
hits.push(a[i]);
}
}
//log array to console
return hits;
}
alert(JSON.stringify(findWhatsMissing(' d a b c ')));
function missingchar(str) {
let arr = new Array(26);
let mis_str = '';
Acharcode = 'A'.charCodeAt();
Zcharcode = 'Z'.charCodeAt();
for (i = Acharcode; i <= Zcharcode; i++) {
arr[i] = String.fromCharCode(i).toLowerCase();
}
arr.map((item, index) => {
if (str.indexOf(item) == -1) {
mis_str += item;
}
})
return mis_str;
}
console.log(missingchar('satya'));