admin管理员组文章数量:1024631
this is my challenge: Create a function that will find the missing letter passed in the parameter and return it. If all letters are present in the string, the return will be undefined. For example missingLetter("abce") should return "d", missingLetter("bcd") should return undefined.
I am having trouble with this one, can you please tell me if I am on the right track with my code:
var missingLetter = function(char){
var missing = "";
var str = "abcdefghijklmnopqrstuvwxyz";
for (var i = char[0]; i < char.length; i++){
for(var y = char[0].indexOf(str); y < char.length; y++ ){
if(char[y].indexOf(str) == -1 ){
missing.push(char[y]);
}
}
}
console.log(missing);
return missing;
}
missingLetter("abce")
this is my challenge: Create a function that will find the missing letter passed in the parameter and return it. If all letters are present in the string, the return will be undefined. For example missingLetter("abce") should return "d", missingLetter("bcd") should return undefined.
I am having trouble with this one, can you please tell me if I am on the right track with my code:
var missingLetter = function(char){
var missing = "";
var str = "abcdefghijklmnopqrstuvwxyz";
for (var i = char[0]; i < char.length; i++){
for(var y = char[0].indexOf(str); y < char.length; y++ ){
if(char[y].indexOf(str) == -1 ){
missing.push(char[y]);
}
}
}
console.log(missing);
return missing;
}
missingLetter("abce")
Share
Improve this question
asked Feb 27, 2017 at 8:22
Colin SygielColin Sygiel
9456 gold badges19 silver badges31 bronze badges
9
-
1
you set your variable
i
as astring
but pare it with anumber
? – Amresh Venugopal Commented Feb 27, 2017 at 8:24 - @AmreshVenugopal Are you referring to the .indexOf()? There I am using -1 to determine if this letter is not present in the array. If it is, then I push that missing letter to new variable. – Colin Sygiel Commented Feb 27, 2017 at 8:26
-
aren't you meant to flip
char[y].indexOf(str)
i.e.str.indexOf(char[y])
why two loops? – Seabizkit Commented Feb 27, 2017 at 8:27 -
I was referrring to this part in the for loop:
var i = char[0]; i < char.length
– Amresh Venugopal Commented Feb 27, 2017 at 8:27 - @AmreshVenugopal Good point, that doesn't make sense. – Colin Sygiel Commented Feb 27, 2017 at 8:36
2 Answers
Reset to default 2Tonmoy already give the answer if you want you can check this. First if you want to use push function then you must create a array.
var missingLetter = function(char){
var missing = []
var y = 0
var str = "abcdefghijklmnopqrstuvwxyz";
for (var i = 0; i < str.length; i++){
while(y < char.length ){
if( char[y] != str[y+i] ){
missing.push(str[y+i])
++i
}
else
++y
}
}
console.log(missing)
return missing
}
missingLetter("cdz")
you have defined variable missing as string, but It should be a array(). The loop condition is not properly. Following is the code snippet, which works fine.
var missingLetter = function(char){
var missing = new Array();
var str = "abcdefghijklmnopqrstuvwxyz";
var i = 0;
while(i<char.length) {
for(var j=0;j<26;j++) {
if(str[j].indexOf(char[i])>-1){
i++;
} else {
missing.push(str[j]);
}
}
}
console.log(missing);
return missing;
}
missingLetter("abce");
this is my challenge: Create a function that will find the missing letter passed in the parameter and return it. If all letters are present in the string, the return will be undefined. For example missingLetter("abce") should return "d", missingLetter("bcd") should return undefined.
I am having trouble with this one, can you please tell me if I am on the right track with my code:
var missingLetter = function(char){
var missing = "";
var str = "abcdefghijklmnopqrstuvwxyz";
for (var i = char[0]; i < char.length; i++){
for(var y = char[0].indexOf(str); y < char.length; y++ ){
if(char[y].indexOf(str) == -1 ){
missing.push(char[y]);
}
}
}
console.log(missing);
return missing;
}
missingLetter("abce")
this is my challenge: Create a function that will find the missing letter passed in the parameter and return it. If all letters are present in the string, the return will be undefined. For example missingLetter("abce") should return "d", missingLetter("bcd") should return undefined.
I am having trouble with this one, can you please tell me if I am on the right track with my code:
var missingLetter = function(char){
var missing = "";
var str = "abcdefghijklmnopqrstuvwxyz";
for (var i = char[0]; i < char.length; i++){
for(var y = char[0].indexOf(str); y < char.length; y++ ){
if(char[y].indexOf(str) == -1 ){
missing.push(char[y]);
}
}
}
console.log(missing);
return missing;
}
missingLetter("abce")
Share
Improve this question
asked Feb 27, 2017 at 8:22
Colin SygielColin Sygiel
9456 gold badges19 silver badges31 bronze badges
9
-
1
you set your variable
i
as astring
but pare it with anumber
? – Amresh Venugopal Commented Feb 27, 2017 at 8:24 - @AmreshVenugopal Are you referring to the .indexOf()? There I am using -1 to determine if this letter is not present in the array. If it is, then I push that missing letter to new variable. – Colin Sygiel Commented Feb 27, 2017 at 8:26
-
aren't you meant to flip
char[y].indexOf(str)
i.e.str.indexOf(char[y])
why two loops? – Seabizkit Commented Feb 27, 2017 at 8:27 -
I was referrring to this part in the for loop:
var i = char[0]; i < char.length
– Amresh Venugopal Commented Feb 27, 2017 at 8:27 - @AmreshVenugopal Good point, that doesn't make sense. – Colin Sygiel Commented Feb 27, 2017 at 8:36
2 Answers
Reset to default 2Tonmoy already give the answer if you want you can check this. First if you want to use push function then you must create a array.
var missingLetter = function(char){
var missing = []
var y = 0
var str = "abcdefghijklmnopqrstuvwxyz";
for (var i = 0; i < str.length; i++){
while(y < char.length ){
if( char[y] != str[y+i] ){
missing.push(str[y+i])
++i
}
else
++y
}
}
console.log(missing)
return missing
}
missingLetter("cdz")
you have defined variable missing as string, but It should be a array(). The loop condition is not properly. Following is the code snippet, which works fine.
var missingLetter = function(char){
var missing = new Array();
var str = "abcdefghijklmnopqrstuvwxyz";
var i = 0;
while(i<char.length) {
for(var j=0;j<26;j++) {
if(str[j].indexOf(char[i])>-1){
i++;
} else {
missing.push(str[j]);
}
}
}
console.log(missing);
return missing;
}
missingLetter("abce");
本文标签:
版权声明:本文标题:Javascript: I am trying to loop through a string of characters and determine if a character is missing - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745561401a2156181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论