Javascript:如何获得以字母'a'开头的单词的长度 [英] Javascript: how to get the length of that words which starts with letter 'a'

查看:95
本文介绍了Javascript:如何获得以字母'a'开头的单词的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写函数,它可以获取文本并计算以字母a开头的单词数量



我尝试了什么:



我试过这样的,但这是错的。





I need to write function, which get text and count how many words start with letter 'a'

What I have tried:

I have tried like this, but it's wrong.


function startsWithACount(text){
	text = text.split(" ");
	for(var i = 0; i < text.length; i++){
		if(i == "a" || text[i].substring(0,1) == "a"){
 		return text[i].length;
 	}
	}
}
console.log(startsWithACount("I love Barcelona and Juventus")); //This must return 1, but it returns 3. Why?

推荐答案

引用:

我试过这样,但是错了。

I have tried like this, but it's wrong.



据我了解你的代码,3是正确的答案。你为什么期望1?



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么,你的任务是与它应该做的比较。

调试器中没有魔法,它不知道你应该做什么,它没有找到错误,它只是通过向你展示帮助你到底是怎么回事。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

JavaScript调试 [ ^ ]

Chrome DevTools &NBSP; |&NBSP;网络  |  Google Developers [ ^ ]

这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


As far as I understand your code, 3 is the correct answer. Why do you expect 1 ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
JavaScript Debugging[^]
Chrome DevTools  |  Web  |  Google Developers[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


Quote:

if(i == "a" || text[i].substring(0,1) == "a"){
    return text[i].length;
}



您将返回以字母a开头的第一个单词的长度。问题是你需要返回以字母a开头的单词数。



你需要另一个变量来保存数字你发现的一个字样。在if条件内,增加该变量( count = count + 1; )。然后,在函数结束时,返回该变量。



NB: i ==a永远不会 true ,因此您可以删除该测试。



NB 2:注意区分大小写 - A不等于a。


You're returning the length of the first word that starts with the letter "a". The question says you need to return the number of words that start with the letter "a" instead.

You're going to need another variable to hold the number of "a" words you've found. Inside the "if" condition, increment that variable (count = count + 1;). Then, at the end of the function, return that variable.

NB: i == "a" will never be true, so you can remove that test.

NB 2: Watch out for case-sensitivity - "A" is not equal to "a".


这篇关于Javascript:如何获得以字母'a'开头的单词的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆