-1

And I am sorry to bother with this noob-stuff, but currently new to all this. But learning slowly. In the first lines of code im getting a return (in this code i get 20*2=40. in the next phase I want to multiplie the return (40) with 20. = 800. so in the outcome it will show 40 And 800. But i only get it to be in the outbox [function], it says. and a msg; "it looks like you didnt print out a value for newNumber". What do I do wrong? Thanks for all help!

var timesTwo = function (number) {
    return number * 2;
};
timesTwo(20);

var newNumber = function (tal) {
    (timesTwo * tal);
    console.log(newNumber);
};
newNumber(20);
Colin Brock
  • 21,267
  • 9
  • 46
  • 61

2 Answers2

1

What you need to do is assign the result to a variable, and in the second function return the result:

var timesTwo = function(number) {
   return number * 2;
};

var twoTimesResult = timesTwo(20); 

var newNumber = function (tal) {
  return twoTimesResult * tal;
};

var result2 = newNumber(20);

console.log(result2);

If you wanted to be fancy you could also do the following:

function multiplier(num, multiplier) {
  var by = num * multiplier;
  return function (number) {
    return number * by;
  };
}

var multiplyResult = multiplier(20, 2);

console.log(multiplyResult(20));
Andy
  • 61,948
  • 13
  • 68
  • 95
  • Oh stupid course. it didnt let me cause i didn't do EXACTELY what it said, fixed now. Thanks anyways Andy! – user3006873 Nov 19 '13 at 01:01
  • @user3006873, in case you wanted to try something else, I added an edit to my answer. – Andy Nov 19 '13 at 01:08
  • hm! thats a bit advanced for me yet, running the codeacademy online, and it only lets me pass the "challenges" if i write the code as THEY want me to;p but will defently check it out! – user3006873 Nov 19 '13 at 01:22
0

I'm answering this question too because I think these types of things are fun.

Another approach you could take is the OOP approach.

From your example you have a common multiplier of 20 and a beginning constant of 2.

I would personally attack this problem like so to avoid multiple variables:

// Create a calc object
var Calc = (function () {

    // Constructor
    Calc = function () {

        // Initial constant
        this.current_val = 2;
    }

    // Prototype methods
    Calc.prototype = {

        // inc() method
        inc: function () {
            return this.current_val * 20;
        }
    }

    // Return object
    return Calc;
})();

// Object Instance
var obj = new Calc();

// Initial call to inc() -> 40
obj.current_val = obj.inc();
// Log
console.log(obj.current_val);
// Second call to inc() -> 800
obj.current_val = obj.inc();
// Log
console.log(obj.current_val);
// Third call to inc() -> 16000
obj.current_val = obj.inc();
// Log
console.log(obj.current_val);

I made a jsfiddle so you can see the output.

iamjpg
  • 7,078
  • 1
  • 16
  • 18
  • The IIFE syntax is useful when you have shared private members but since you don't have any you're creating closures for no good reason and making the code look more complicated than it needs to be. Replacing prototype with another object causes Calc.prototype.constructor to point to Object instead of Calc. http://stackoverflow.com/a/16063711/1641941 – HMR Nov 19 '13 at 03:55
  • 1
    @HMR While I understand what you're saying I do believe that everyone has a personal preference to Javascript design patterns. The reason I chose to do it this way was because it's a solid Javascript design pattern (i.e., see the Coffeescript Class definition) and something which can continue to grow, evolve, and more importantly, reuse. – iamjpg Nov 19 '13 at 04:58
  • That's good enough, I never use closures to simulate privates (shared or instance) so never bother to IIFE the constructor functions. Coffeescript and TypeScript both use that syntax. – HMR Nov 19 '13 at 06:22