Search code examples
javascriptreturn

How to return data from a function


I'm trying to solve the following Kata:

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

I'm unable to figure out how to return the data from my function so that I can later assign the value to an HTML element.

This is my code.

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);
   }
}

Solution

  • okay, so my issue has been solved! Thanks all of you, especially krillgar, so I had to alter the code you gave me krillgar, a little bit in order to populate the results array with only the numbers (one number in this case) that satisfy the parameters of the daily tease I was asking about. yours was populating with 89 undefined and on number, 27 because it is the only number that works.

    One of my problems was that I was expecting the return statement to not only save a value, but also show it on the screen, but what I was not realizing was that I needed a place to store the value. In your code you created a result array to populate with the correct numbers. And also, I needed a variable to store the data for each iteration of the for loop cycling through 10 - 100. Anyways, you gave me what I needed to figure this out and make it do what I wanted it to do, and all is well in the world again.

    Anyway, thank you all for your help and input, and I will always remember to make sure I have somewhere to store the answers, and also somewheres to store the value of each loop iteration in order to decide which numbers to push into the results array and save it so it can be displayed and/or manipulated for whatever purpose it may be. I guess I was just so busy thinking about the fact that when I returned num it didn't show the value, instead of thinking about the fact that I needed to store the value. Here is the final code for this problem and thanks again peoples!

    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;
    

    }