函数不返回值

时间:2015-08-09 13:22:16

标签: javascript jquery return return-value

我只是开始学习如何重构代码,而我却没有抽象出一个简单的函数。我将参数传递给checkAnwser并且它有效,但count“丢失”

我无法在此处获取/追加“计数”:

jsfiddle

<input data-correctanswer="javascript" id="answer1" name="" type="text">This works fine (no special chars)
<br/>
<button id="btn1">check 1</button>
<br/>
<input data-correctanswer="jávascripté" id="answer2" name="" type="text"> 
<br/>
<button id="btn2">check 2</button>
<div style="border: 1px solid;" id="result"></div>

使用Javascript:

 $(document).ready(function () {

         var count;
         $('#btn1').click(function () {
             checkAnswer($('#answer1').data('correctanswer'), $('#answer1').val());
             $('#result').append('result: ').append(count); <-- does not read count
     }); // end of click
     $('#btn2').click(function () {
         checkAnswer($('#answer2').data('correctanswer'), $('#answer2').val());
         $('#result').append('result: ').append(count); // <-- does not read count
     }); // end of click


         function checkAnswer(coorAns, UserAnswer) {
             var count = 0;
             //  var coorAns = $('input[type=text]').data('correctanswer');
             //  var UserAnswer = $('input[type=text]').val();
             var mistakesAllowed = 1;

             if (UserAnswer === coorAns) {
                 count = count + 2;
             }
             for (i = 0; i < coorAns.length; i++) {
                 if (coorAns.charAt(i) !== UserAnswer.charAt(i)) {
                     mistakesAllowed--; // reduce one mistake allowed
                     if (mistakesAllowed < 1) { // and if you have more mistakes than allowed
                         count = count + 1;
                     }
                     if (mistakesAllowed < 0) {
                         count = count - 2
                         break;
                     }
                 }
             }
             console.log('final count: ' + count);
             return count;
         }

     });

2 个答案:

答案 0 :(得分:3)

Adeneo说:

 var count; //here it's defined.
 $('#btn1').click(function () {
     count = checkAnswer($('#answer1').data('correctanswer'), $('#answer1').val());
     $('#result').append('result: ').append(count); 
 }); // end of click
 $('#btn2').click(function () {
     count = checkAnswer($('#answer2').data('correctanswer'), $('#answer2').val());
     $('#result').append('result: ').append(count); 
 }); // end of click

您的函数checkAnswer返回一个名为count的值。该值可以分配给变量count

您可能认为,在函数count中分配checkAnswer也会将其分配给函数外部的变量count。但是,除非您将函数的结果分配给函数外部的变量count,否则这两个变量在两个不同的scopes中并且彼此不连接。

更准确地说

checkAnswer与第一个count变量位于同一范围内。这意味着你可以这样做:

var count = 0;
console.log(count); //will log 0.
function checkAnswer()
{
   count = 1;
}
checkAnswer();
console.log(count); //will log 1.

但是当你在函数中使用运算符var时,它将创建一个绑定到函数范围的变量(它变成一个私有变量),只能在该函数中访问。除非你将它返回到该函数范围之外的变量。

var count = 0;
console.log(count); //will log 0.
function checkAnswer()
{
   var count = 1;
}
checkAnswer();
console.log(count); //will log 0. 

有关Stack Overflow范围的更多内容:

What is the scope of variables in JavaScript?

奖金

为您的代码提供一点效率建议

 var count;
 $('#btn1', '#btn2').click(function () {
     var buttonId = $(this).attr("id").substr(4); //will result in 1 or 2
     count = checkAnswer($('#answer' + buttonId ).data('correctanswer'), $('#answer' + buttonId ).val());
     $('#result').append('result: ').append(count); 
 }); // end of click

这将把它减少为一个通用函数。因此,当您需要更新代码时,您只需要更新一个函数,而不是更新相同代码的多个实例。

答案 1 :(得分:2)

而不是

 checkAnswer($('#answer2').data('correctanswer'), $('#answer2').val());

使用

var count=checkAnswer($('#answer2').data('correctanswer'), $('#answer2').val()); //Stored returned value to count variable.

现在您可以访问返回的值。

Here是您更新的小提琴。