Click here to Skip to main content
15,886,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I might be messing up here the code, but I'm trying to work around include.
So this version checks only for the length of the sentence but not the elements... I can type 26 of letter a and will accept it... I want to test if the input has each elemenet of alphabet at least once.
PS: I'm new to programming.

What I have tried:

JavaScript
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!");
}
}
Posted
Updated 6-Oct-17 1:22am
v3

'The quick brown fox jumps over the lazy dog'
Algorithm:
You know that all letters are not in a sentence when you have found 1 letter that is not in the sentence.
You know that all letters are in a sentence when you have found each letter in the sentence.

Translation:
set an answer variable to true
Check each letter in a loop
    if the letter is not in sentence
        answer is false
then after loop, check if answer is true or false and do what you want.
if answer is true
    bingo
else
    Bollocks
 
Share this answer
 
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 i;
for ( i = 0; i < alphabet.length; i++) 
{
  if(input.includes(alphabet[i]) == false) 
    break;
} 
if ( i == alphabet.length)
  alert("Bingo");
else 
  alert("Bollocks!"); 
 
Share this answer
 
Comments
Member 13446970 6-Oct-17 6:51am    
I checked this, and it still looks for the length of the string rather than each particular element in the string.
If I type in 26 a's it will still give me bingo but what I want is alert bingo only if the input has all letters from a to z
CPallini 6-Oct-17 7:28am    
Try it:
https://www.w3schools.com/code/tryit.asp?filename=FK9R31U4M427
Try this,

var occurencePositive = 0;
for(var index = 0; index <alphabet.lenght; index++){
    if(input.IndexOf(alphabet[index]) > -1){
       //increase counter when match is found in string.
       occurencePositive++;
    }
}

if(occurencePositive == alphabet.length - 1){
  alert("Bingo");
} 
else { 
  alert("Bollocks!");
}


Hope this helps.
 
Share this answer
 
Comments
Member 13446970 6-Oct-17 7:00am    
So this was helpful. Tweaked a little bit, needed another variable that assigns each letter from the alphabet an index
so in the for statement before if needed a var called letters = alphabet[i];
if(input.indexOf(letters) > -1) {
occurencePositive++;

and the occurencePositive in the 2nd if statement has to be equal to 26.
GKP1992 6-Oct-17 7:17am    
do not see the necessity of the new variable, also to compare with 26 we have to write index <= alphabet.lenght in the for loop condition which we cannot do because alphabet[26] will break for the last comparison. Since it is index < alphabet.lenght, maximum possible value for occurencePositive is 25 (if all the letters are found). Anyways, glad it helped.
Member 13446970 6-Oct-17 7:33am    
I might be wrong but doesn't index start at 0, having 26 possible values for positive occurence? 0, 1, 2,..., 25 ?
GKP1992 6-Oct-17 8:06am    
Yes, and we only check the final value.
Member 13446970 6-Oct-17 7:29am    
Try the code I posted below.
if you use occurencePositive == alpha.length - 1, and then type in 'The quick brown fox jumps over the lazy dog' it will come back as bollocks, even though the sentence has all the letters from alpha.
With the help of GKP1992 I finally figured out a way to solve this.
I don't know if my comments are right, please correct me if I'm wrong.
Cheers,
OP.
JavaScript
var alpha = 'abcdefghijklmnopqrstuvwxyz';
var inputQ = prompt("Type in a message", "").toLowerCase(); //transform input to lower case
var occurencePositive = 0;
	for (var i = 0; i < alpha.length; i++) {
		if(inputQ.indexOf(alpha[i]) > -1) // increase counter when match is found in string
			occurencePositive++;
}
	if (occurencePositive == 26) //when counter reaches 26 
		alert("Bingo!");
	else
		alert("Bollocks!"); // when counter is less, meaning missing letters
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900