我正在尝试创建一个函数来检查字母是否是元音。
为什么我总是说它不是元音?
function isItAVowel(letter) {
var vowel = ["a", "e", "i", "o", "u"];
for (var i=0; i < vowel.length; i++) {
if (letter == vowel[i]) {
document.getElementById("paragraph").innerHTML = "vowel";
} else {
document.getElementById("paragraph").innerHTML = "not vowel";
}
}
}
isItAVowel("i");
答案 0 :(得分:2)
另一种方法是:
function isItAVowel(letter) {
var vowel = ["a", "e", "i", "o", "u"];
document.getElementById("paragraph").innerHTML = (vowel.indexOf(letter) >= 0) ? "vowel" : "not vowel";
}
isItAVowel('i');
所以我认为这种方式更具说明性,可以得到相同的结果。
答案 1 :(得分:1)
因为一旦找到了匹配项,您就需要从循环中断开并退出该函数。
处理错误的正确方法:
function isItAVowel(letter) {
var vowel = ["a", "e", "i", "o", "u"];
for (var i=0; i < vowel.length; i++) {
if (letter == vowel[i]) {
document.getElementById("paragraph").innerHTML = "vowel";
return;
}
}
document.getElementById("paragraph").innerHTML = "not vowel";
}
isItAVowel("i");
由于每个人都以不同的方式发布有趣的方法,所以这里有一种方法可以在没有功能的情况下完成,执行速度最快:
var isItAVowel = {
'a': true,
'e': true,
'i': true,
'o': true,
'u': true };
!!isItAVowel["a"] == true;
!!isItAVowel["o"] == true;
!!isItAVowel["c"] == false;
答案 2 :(得分:1)
一旦发现字母是元音,就打破你的'for'循环,否则你的最终结果将被覆盖
vagrant plugin install vagrant-triggers
完整代码
document.getElementById("paragraph").innerHTML = "vowel";
break;
没有'for'循环
的函数的优化版本function isItAVowel(letter) {
var vowel = ["a", "e", "i", "o", "u"];
for (var i=0; i < vowel.length; i++) {
if (letter == vowel[i]) {
document.getElementById("paragraph").innerHTML = "vowel";
break;
}
else { document.getElementById("paragraph").innerHTML = "not vowel";
}
}
}
isItAVowel("i");
答案 3 :(得分:1)
TbWill4321答案是正确的,但只是想分享这个,我避免了for循环并使用了js数组的indexOf属性
x=load('C:....dat'); %Load the file with data of precipitation, temperature and snowpack
td=x(:,4); %hour of the day
GlobalRad=x(:,5); %Global Radiation
T=x(:,6); %Air Temperature
P=x(:,7); %Precipitation
Snow_obs=x(:,8); %SWE Observed
SnowDepth=x(:,9); %Snow Depth Observed
ts=x(:,10); %start time of daylight on day d (t0)
te=x(:,11); %end time of daylight on day d (t1)
dTd=x(:,12); %difference between the max and min daily temperatures on day d
snow_sim(1)=0;
runoff=zeros(length(P));
%Parameters
Scf=1.3; %Snowfall correction factor
TT=1; %Threshold temperature
C=3.5; %Degree-day factor (mm day-1 C-1)(Ac)
Cfr=0.05; %Refreezing coefficient (use default value of 0.05)
Cwh=0.1; %Water holding capacity (use default value of 0.1)
B=0.05; % Factor to convert the temp amplitude into a degree day factor
snow_sim(1)=0; %Simulated snowpack is 0 mm for day 1
snow_sim_water(1)=0; %Liquid water in snowpack for day 1 the water is 0 mm
for t=1 : length(td); %**FIRST time series loop**
ln(t)=24-te(t)+ts(t); %length of the night
Z(t)=2*((te(t)-ts(t))/(3.14*ln(t))); %factor ensuring that daily mean vaules of As equals Ac
if ts(t)<=td(t)<te(t);
As(t)=C+(B*dTd(t)*(sin(3.14*((td(t)-ts(t))/(te(t)-ts(t)))))); %equation for time variant degree day factor secnario1
else As(t)=C-(B*dTd(t)*Z(t)); %equation for time variant degree day factor senario 2
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for t=2 : length(P); %**SECOND time series loop**
if T(t)< TT; %If the temperature for day t is lower than the threshold value for melting (=below TT degrees) then refreezing of melt water will occur
refreez(t)=Cfr*As*(TT-T(t)); %Equation for refreezing of meltwater
if refreez(t) > snow_sim_water(t-1); %If the refreezing for day t is larger than the water in the snowpack the day before, then the refreezing is limited
refreez(t)= snow_sim_water(t-1); %Can't freez more water than is accumulated in the snowpack
end
snow_sim(t)=P(t)*Scf+snow_sim(t-1)+refreez(t); %The total simulated snowpack for any given day is the precipitation that day together with snow pack from day before and refreeze of that day.
snow_sim_water(t)=snow_sim_water(t-1)-refreez(t); %The total simulated amount of water in the snowpack is the water in the snowpack the day before minus the water refrozen the same day
else %T(t) > TT %temperature above threshold temperature, snowmelt will occur
Melt(t)=As*(T(t)-TT); %Equations for melting rate of existing snowpack
if Melt(t) > snow_sim(t-1); %If the melting rate for day t is larger than the snowpack the day before, then the melting is limited
Melt(t)= snow_sim(t-1); %Because it can't melt more snow than is available
end
snow_sim(t)=snow_sim(t-1)-Melt(t); %Total simulated snow is the simulated snowpack for the day before minus the melted snow
snow_sim_water(t)=snow_sim_water(t-1)+P(t)+Melt(t); %Total water amount in snow is the water amount in snow for the day before plus the precipitation and the melted snow
if Cwh*snow_sim(t) < snow_sim_water(t); %The snowpack can retain as much as 10% of its water equivalent, but not more
runoff(t)=snow_sim_water(t)-0.1*snow_sim(t); %if there is more liquid water, this goes to runoff (note:if there is no snowpack all water will go to runoff
snow_sim_water(t)=0.1*snow_sim(t);
end
end
end
snow_sim_total=snow_sim+snow_sim_water; %The total simulated snowpack is the water in snow and the simulated snowpack
daynr=1:length(P);
答案 4 :(得分:1)
使用正则表达式的另一种有趣的方式,但简而言之,是的,上面提到的那些人,你从来没有弯腰或打破循环。祝你好运
<强> JS 强>
function isItAVowel(letter) {
var smallLetter = letter.toLowerCase(),
DOM = document.getElementById('paragraph');
smallLetter.match(/[aeiou]/gi) ? DOM.innerHTML = "vowel" : DOM.innerHTML = "not vowel";
}
isItAVowel("a");
小提琴 - https://jsfiddle.net/ToreanJoel/vq8gas4L/
请注意,这比使用for循环更快的其他选项慢
答案 5 :(得分:1)
它实际上是循环并连续写入每个元素的html。找到匹配后,您可以添加如下的中断:
function isItAVowel(letter) {
var vowel = ["a", "e", "i", "o", "u"];
for (var i=0; i < vowel.length; i++) {
if (letter == vowel[i]) {
document.getElementById("paragraph").innerHTML = "vowel"; break;
} else {
document.getElementById("paragraph").innerHTML = "not vowel";
}
}
}
isItAVowel("i");