javascript - 如何从函数返回数据

标签 javascript return

我正在尝试解决以下 Kata:

a 2 digit number, if you add the digits together, multiply by 3, add 45 and reverse.

我无法弄清楚如何从函数返回数据,以便稍后将值分配给 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);
   }
}

最佳答案

从你的问题来看,我猜你需要每日循环函数的反转值。

建议您从 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
   }
}

关于javascript - 如何从函数返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53117599/

相关文章:

Python BadYieldError : yielded unknown object HTTPError ('HTTP 599: Connection closed' , )

ruby - 主环境`return`

javascript - knockout JS : What Does Calling applyBindings Without Arguments do?

javascript - Laravel Ajax 调用整个页面

javascript - 当我在Javascript中通过键从对象中获取值时,时间复杂度是O(1)吗?

C++14 'auto' 可以获取函数返回类型,还需要std::result_of<>吗?

c - 如何在 C 函数中返回超过 1 个值?

java - 如何返回字符串中的数字循环? [Java]

javascript - Google 脚本网络应用程序提交后变为空白页

javascript - 使用 Javascript 创建列表