如何从函数返回数据

时间:2018-11-02 11:18:17

标签: javascript return

我正在尝试解决以下问题:

  

2位数字,如果将这些数字加在一起,则乘以3,再加45并取反。

我无法弄清楚如何从函数中返回数据,以便以后可以将值分配给HTML元素。

这是我的代码。

function daily() {
   for(var j = 10; j < 100; j++) {
       function teaser(num) {
           var x = num;
           var y = x.toString().split("");
           if(y.length == 2) {
               var sum = parseInt(y[0]) + parseInt(y[1]);
               if(sum * 3 == x) {
                   console.log(x + " is equal to 3 times " + sum);
                   var addFortyFive = x + 45;
                   console.log("Adding 45 to " + x + " gives " + addFortyFive);
                   var reversal = parseInt(addFortyFive.toString().split('').reverse().join(''));
                   console.log("'The 2 digit number " + x + ", is 3 times the sum (" + sum + ") of its digits. If 45 is added to " + x + ", the result is " + addFortyFive + ". If the digits are reversed, the number is... " + reversal + ".");
               }
           } else {
               console.log("Not a 2 digit Number!!");
           }
       }
       teaser(j);
   }
}

4 个答案:

答案 0 :(得分:1)

根据您的问题,我猜测您每天需要在函数上使用reversal值进行循环。

建议您从for循环内部取出函数筋急转弯,这将使代码更简洁易懂,您可以这样做:

function daily() {
   for(var j = 10; j < 100; j++) {
       var teaser = teaser(j);
       // Can now use anything returned from teaser function here
   }
}

function teaser(num) {
     var x = num;
     var y = x.toString().split("");
     if(y.length == 2) {
         var sum = parseInt(y[0]) + parseInt(y[1]);
         if(sum * 3 == x) {
             console.log(x + " is equal to 3 times " + sum);
             var addFortyFive = x + 45;
             console.log("Adding 45 to " + x + " gives " + addFortyFive);
             var reversal = parseInt(addFortyFive.toString().split('').reverse().join(''));
             console.log("'The 2 digit number " + x + ", is 3 times the sum (" + sum + ") of its digits. If 45 is added to " + x + ", the result is " + addFortyFive + ". If the digits are reversed, the number is... " + reversal + ".");

             return reversal;
         }
     } else {
         console.log("Not a 2 digit Number!!");
         return false;
     }
 }

如果不想删除功能,则可以执行以下操作:

function daily() {
   for(var j = 10; j < 100; j++) {
       function teaser(num) {
           var x = num;
           var y = x.toString().split("");
           if(y.length == 2) {
               var sum = parseInt(y[0]) + parseInt(y[1]);
               if(sum * 3 == x) {
                   console.log(x + " is equal to 3 times " + sum);
                   var addFortyFive = x + 45;
                   console.log("Adding 45 to " + x + " gives " + addFortyFive);
                   var reversal = parseInt(addFortyFive.toString().split('').reverse().join(''));
                   console.log("'The 2 digit number " + x + ", is 3 times the sum (" + sum + ") of its digits. If 45 is added to " + x + ", the result is " + addFortyFive + ". If the digits are reversed, the number is... " + reversal + ".");

                   return reversal;
               }
           } else {
               console.log("Not a 2 digit Number!!");
               return false;
           }
       }

       var teaser = teaser(j);
       // Can now use anything returned from teaser function here
   }
}

答案 1 :(得分:0)

从函数中返回某些内容非常简单!

只需将return语句添加到函数中即可。

function sayHello(name) {
	return 'Hello ' + name + '!';
}

console.log(sayHello('David'));

答案 2 :(得分:0)

正如问题评论中所述,由于您(很可能)将有多个符合您条件的答案,因此需要将其存储在数组中。您的teaser函数返回单个结果,其中daily将检查您范围内的所有数字。

function daily() {
    var results = [];
   for(var j = 10; j < 100; j++) {
       function teaser(num) {
           var x = num;
           var y = x.toString().split("");
           if(y.length == 2) {
               var sum = parseInt(y[0]) + parseInt(y[1]);
               if(sum * 3 == x) {
                   console.log(x + " is equal to 3 times " + sum);
                   var addFortyFive = x + 45;
                   console.log("Adding 45 to " + x + " gives " + addFortyFive);
                   var reversal = parseInt(addFortyFive.toString().split('').reverse().join(''));
                   console.log("'The 2 digit number " + x + ", is 3 times the sum (" + sum + ") of its digits. If 45 is added to " + x + ", the result is " + addFortyFive + ". If the digits are reversed, the number is... " + reversal + ".");

                    // Here you have one that is correct, so return it:
                    return num;
               } else {
                    // Make sure we don't return undefined for when the sum 
                    // times three doesn't equal the number.
                    return null;
                }
           } else {
               console.log("Not a 2 digit Number!!");
                return null;
           }
       }
       var answer = teaser(j);

        if (answer !== null) {
            results.push(answer);
        }
   }

    return results;
}

答案 3 :(得分:0)

好的,所以我的问题已经解决了!谢谢大家,尤其是小磷虾,所以我不得不稍微修改一下您给我的小磷虾的代码,以便只用满足日常挑逗参数的数字(在这种情况下为一个数字)填充结果数组在问。您的计算机上填充了89个未定义数字,其中27个是因为它是唯一有效的数字。

我的问题之一是我期望return语句不仅可以保存值,还可以在屏幕上显示它,但是我没有意识到的是我需要一个存储值的地方。在您的代码中,您创建了一个结果数组,以填充正确的数字。而且,我还需要一个变量来存储for循环(每次循环10-100)的每次迭代的数据。无论如何,您给了我我需要弄清楚并使其执行我想要做的事情,而所有工作都是再次好起来。

无论如何,谢谢大家的帮助和投入,我将永远记住确保有地方存储答案,并有地方存储每次循环迭代的值,以便确定要推入的数字结果数组并将其保存,以便可以出于任何目的显示和/或操作它。我想我只是在忙着思考以下事实:当我返回num时,它并没有显示值,而不是在考虑我需要存储值的事实。这是此问题的最终代码,再次感谢各界人士!

function daily() {
var results = [];
for(var j = 10; j < 100; j++) {
    function teaser(num) {
        var x = num;
        var y = x.toString().split("");
        if(y.length == 2) {
            var sum = parseInt(y[0]) + parseInt(y[1]);
            if(sum * 3 == x) {
                console.log(x + " is equal to 3 times " + sum);
                var addFortyFive = x + 45;
                console.log("Adding 45 to " + x + " gives " + addFortyFive);
                var reversal = parseInt(addFortyFive.toString().split('').reverse().join(''));
                console.log("'The 2 digit number " + x + ", is 3 times the sum (" + sum + ") of its digits. If 45 is added to " + x + ", the result is " + addFortyFive + ". If the digits are reversed, the number is... " + reversal + ".");
                return num;
                // Here you have one that is correct, so return it:
           } else {
                console.log(num + " does not fulfill function parameters");
                // This is just so you can visualize the numbers
                return null;
            }
        }
    }
    var answer = teaser(j);
    if(answer != null) {
        results.push(answer);
    }
}
return results;

}