user8731431
user8731431

Reputation: 1

How do I check if one string has all elements of the other string at least once

I’m quite new to programming, just learning JavaScript. Here is what I tried but it actually checks the length of the string and not if the elements in input match elements in alphabet. I’m trying to work around the includes command:

var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 
        'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];  
var input = prompt("Type in a message","");  
for ( i = 0; i < alphabet.length; i++) {  
    if(input.includes(alphabet[i])) {  
         alert("Bingo");  
    } else {   
         alert("Bollocks!");  
    }  
} 

Upvotes: 0

Views: 57

Answers (3)

CodeSmith
CodeSmith

Reputation: 3197

I am not sure what you are doing are you trying to check if something you input matches the small letter of English alphabet or what?

But usually regex is a good approach:

  • Step 1 - build a regex of what you need to match

  • Step 2 - check if something else matches

If You need to check every character in input is a small letter of English alphabet

var input = "aahahahaaa"; // try with "nope,nomatch!"
var regex = new RegExp("[a-z]" + "{" +  input.length + "}");
console.log(regex);
if (input.match(regex) !== null) console.log("YUP CHAMP")
else console.log("NOPE BLOKE")

If you need to match other characters, simply modify the regex. I can't cover all the cases but here are some for you to test out:

For small and capital letters:

 var regex = new RegExp("[a-zA-Z]" + "{" +  input.length + "}");

For above and spaces

var regex = new RegExp("[a-zA-Z\s]" + "{" +  input.length + "}");

For above and numbers

var regex = new RegExp("[0-9a-zA-Z\s]" + "{" +  input.length + "}");

Fast, clean, can handle very complex matching rules. This is how the pros do it ;)

Here is the: fiddle

And here is a good tutorial about Regular expressions: regexone

And a playground for you to test regexes as you learn: playground

Welcome to the wonderful world of regexes :)

Upvotes: 0

Ole Haugset
Ole Haugset

Reputation: 3797

The main issue I see with your code, is that you will fire a alert for every letter in the alphabet array. In this code, you have a hit variable, set to false, which will be set to true if it results in a success.

My method uses the indexOf, which checks if the input has any letter of the current pointed letter in alphabet array. If so, it returns the actual index of it. If none, it returns -1:

var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];  

var input = prompt("Type in a message","");  
var hit = false;

for( var letter in alphabet ){
    if( input.indexOf(alphabet[letter]) !== -1 ){
        hit = true;
    }
}

if( hit ){ alert( "Bingo" ); }
else { alert( "Bollocks!" ); }

Be aware that the code is case sensitive. So M will not match anything. You can work around this by using to toLowerCase method

Upvotes: 1

user7222571
user7222571

Reputation:

I dont know exactly what you mean. Look at this fiddle. I think this is what you mean. I have added a variable bingo and show the Bollock alert at the end.

Fiddle

var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 
'z'];
var input = prompt("Type in a message", "");
var bingo = false
for (i = 0; i < alphabet.length; i++) {
    if (input == (alphabet[i])) {
        bingo = true
        alert("Bingo");
}
}
if (bingo == false) {
     alert("Bollok")
}

Upvotes: 0

Related Questions