这是Eloquent JavaScript的基本彩排。
但是,结果我只能得到“ 0”。由于某种原因,循环不会更新“ count”变量:
function countChar(str, n){
let count = 0;
for(let i = 0;i == str.length-1; i++){
if(str[i] == n){
count++;
}
}
return count;
}
console.log(countChar("dazzled", "z"));
答案 0 :(得分:2)
问题出在您的for循环语句中。
您有:
for(let i = 0;i == str.length-1; i++)
应该在哪里:
for(let i = 0;i < str.length; i++)
答案 1 :(得分:0)
您尚未指定何时希望obsValue
变量正确更新。您已经说过
//remove the fields obsValue and obs, and use local variables instead
//private List<string> listObs = new List<string>();
//private string obs = null;
public List<string> obsValue
{
get
{
//create a new list everytime the getter of this member gets called
var listObs = new List<string>();
foreach (DataGridViewRow row in dgv_uc.Rows)
{
//declare obs here since we only use it here
var obs = row.Cells["Observed(Sec)"].Value.ToString();
listObs.Add(obs);
}
return listObs;
}
}
不会更新。应该是这样的
count