admin管理员组文章数量:1313175
I am attempting the bean counting example in the functions chapter of the book Eloquent Javascript. My function is returning a blank.
Without giving me the full answer (I'm working through this example to learn), can someone tell me why my code is not printing any text?"
var string = "donkey puke on me boot thar be thar be!";
for (var i = 0; i <= string.length; i++);
function getB(){
if (string.charAt(i) == "b")
return i;
else return "";
}
console.log(getB());
I am attempting the bean counting example in the functions chapter of the book Eloquent Javascript. My function is returning a blank.
Without giving me the full answer (I'm working through this example to learn), can someone tell me why my code is not printing any text?"
var string = "donkey puke on me boot thar be thar be!";
for (var i = 0; i <= string.length; i++);
function getB(){
if (string.charAt(i) == "b")
return i;
else return "";
}
console.log(getB());
Share
Improve this question
edited Feb 23, 2015 at 13:11
Bergi
666k161 gold badges1k silver badges1.5k bronze badges
asked Feb 23, 2015 at 12:43
AndrewAndrew
7472 gold badges8 silver badges24 bronze badges
5
-
1
Why does your loop have an empty body? Where would
getB
get itsi
variable from? – Bergi Commented Feb 23, 2015 at 12:48 -
2
♫ I am a lonely programmer,
i
ain't got no body. – Andy Commented Feb 23, 2015 at 12:49 -
Lose the
;
after thefor()
, make the next lineconsole.log(getB(i))
change tofunction getB(i){...
– Alex K. Commented Feb 23, 2015 at 12:49 -
1
if you want to search the location of your string, you can use
string.indexOf("b");
or write your own function which traverses trough the whole string. (use the function as the body for your loop and use just the first condition) – Raphael Müller Commented Feb 23, 2015 at 12:51 - 1 @RaphaelMüller: Actually that's not the task – Bergi Commented Feb 23, 2015 at 13:11
6 Answers
Reset to default 2There is something wrong about how you're trying to implement this feature.
First of all I think it's better if you have a function that accepts the string
and the char
as parameters in order to call it whenever you want.
Example of calling :
getChar('this is my custom string', 'c') -> it should search character `c` in `this is my custom string`
getChar('this is another custom string', 'b') -> it should search character `b` in `this is another custom string`
Example of implementation :
var getChar = function(string, char){
for(var i=0;i<string.length;i++)
{
if(string.charAt(i)==char) console.log(i);
}
}
Now, try to make it not case-sensitive and instead of console.log
the output try to return a sorted array with character positions
Use This,
var string = "donkey puke on me boot thar be thar be!";
for (var i = 0; i <= string.length; i++) {
if (string.charAt(i) == "b") {
console.log(i);
}
}
if you want to print every position your value is at, you can program something like this:
var string = "donkey puke on me boot thar be thar be!";
for (var i = 0; i <= string.length; i++)
{
getChar(i, "b");
}
function getChar(i, input)
{
if (string.charAt(i) == input)
console.log(i);
}
another example: collect all b positions:
var string = "donkey puke on me boot thar be thar be!";
function getB(string){
var placesOfB = [];
for (var i = 0; i < string.length; i++) {
if (string.charAt(i) == "b") {
placesOfB.push(i);
}
}
return placesOfB;
}
console.log(getB(string));
tip: your for
has no body (putting ;
after it just loops without doing anything)...
and it does not make sense to define a function inside that for
.
Without giving you the full answer, I'll just give you pointers: 1. Your for loop is not plete - it doesn't do anything. 2. Your getB() function needs to accept string parameter in order to perform some action on it. 3. The if..else statement doesn't have opening and closing brackets {}
本文标签: javascriptUsing for loop to find specific characters in stringStack Overflow
版权声明:本文标题:javascript - Using for loop to find specific characters in string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741926977a2405346.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论