【问题标题】:Javascript ELSE statement running regardless of IF statement无论 IF 语句如何,Javascript ELSE 语句都会运行
【发布时间】:2016-01-17 03:11:12
【问题描述】:

非常简单的 sn-p 代码(我是编程新手),我被难住了。

为什么无论 IF 语句是否为真,控制台每次循环都返回 0?

例如,IF 语句运行,找到匹配项并将 1 写入控制台,但随后 ELSE 语句也运行,重置变量“count”。

//Snippet of the array of objects, that the program searches through
var students = [ 
  { 
    name: 'Dave',
    track: 'Front End Development',
    achievements: 158,
    points: 14730
  },
  {
    name: 'John',
    track: 'Full Stack',
    achievements: '24',
    points: '2450'
  }
];

//Declaring variables
var message = '';
var student;
var count = 0;
var name;
var search;


while (true) {
	search = prompt("Enter a name to see their report, or type 'quit' to exit.");
	if (search.toUpperCase() == 'QUIT' || search.toUpperCase() == null) {
		console.log(8);
		break;
	}
	for (i = 0; i < students.length; i++) {
		student = students[i];
		name = search.toUpperCase();
		if (name == student.name.toUpperCase()) {
			count ++;
			console.log(count);
		} else {
			count = 0;
			console.log(count);
		}
	}
}

【问题讨论】:

  • 你总是得到falsename == student.name.toUpperCase()。请编写完整的代码,以便其他人可以帮助您。并使用++count; 然后count++
  • namestudent.name.toUpperCase() 其中之一是返回 false 值。因此控制台返回 0。彻底调试您的代码。还将count 变量初始化为for loop 之外的0@
  • studentsname变量的值是多少?
  • 我现在更新了代码@AnkitKathiriya

标签: javascript if-statement for-loop


【解决方案1】:

我无法立即看到问题的原因,else 分支中count = 0 的重置看起来错误,如果名称重复会导致问题,但你至少应该得到1 一次第一个匹配,而您断言它总是返回0

无论哪种方式,更惯用的写法是:

var uSearch = search.toUpperCase();
var count = students.filter(function(student) {
    return student.name.toUpperCase() === uSearch;
}).length;

【讨论】:

    【解决方案2】:

    count = 0; 移出for loop。从一个空的柜台开始。

    【讨论】:

      【解决方案3】:

      对,正如马特所说...您可以先在循环外初始化 count=0 。我还会添加一行console.log(name) 在它被抓取后在条件之前进行调试。

      count = 0
      for (i = 0; i < students.length; i++) {
        student = students[i];
        name = search.toUpperCase();
      
        # show me the "name" before running condition
        console.log("name pulled from search: ",name)
      
        if (name == student.name.toUpperCase()) {
          count++;
          console.log(count);
        } else {
          count = 0;
          console.log(count);
        }
      }
      

      打印"name" 应该让您了解条件是如何匹配的。

      【讨论】:

        【解决方案4】:

        这是比较两个数组的值的示例代码。

           var count = 0;
        
        var students = [
            { name:"string 1", value:"this", other: "that" },
            { name:"string 2", value:"this", other: "that" }
        ];   
        
        var search = [
            { name:"string 1", value:"this", other: "that" },
            { name:"string 3", value:"this", other: "that" }
        ]; 
        
        for (var i = 0; i < students.length; i++) {
        
        for (var j = 0; j < search.length; j++) {
        
            var student = students[i].name.toUpperCase();
        
          var names = search[j].name.toUpperCase();
        
          if (names == student) {
            count++;
        
            console.log(count);
          } else {
            count = 0;
        
            console.log(count);
          }
          }
        }
        

        【讨论】:

        • OP 使用了student.name 大概是因为学生是一个具有name 属性(以及其他属性)的对象,而不仅仅是一个普通字符串。
        • @Alnitak 现在在这段代码中将两个具有三个属性的对象放在一个数组中,并希望比较清楚。
        • 现在您已经引入了双重嵌套搜索 - OP 只有 一个 搜索字符串。
        猜你喜欢
        • 2015-05-16
        • 1970-01-01
        • 1970-01-01
        • 2021-09-11
        • 2016-06-21
        • 2017-09-11
        • 1970-01-01
        • 1970-01-01
        • 2022-12-14
        相关资源
        最近更新 更多