D. Dub
D. Dub

Reputation: 71

I want to print my answer to the console

I'm very new to programming. I'm following the tutorials on codeacademy.com and I'm stuck on a couple of tutorials. It's giving me trouble with console.log()

The first one is a simple function calculating the cost of 5 oranges at $5 each.

var orangeCost = function(price){
    var cost = price * 5;  //the 5 is the number of oranges

};

orangeCost(5); 

and the tutorial says this is correct. However, I want to return the answer to the console and when I try to do

console.log(orangeCost(5));

I get an error saying "TypeError: string is not a function".

Another one that is giving me the same error is

var my_number = 7; //this has global scope

var timesTwo = function(number) {
    var my_number = number * 2;
console.log("Inside the function my_number is: ");
console.log(my_number);
}; 

timesTwo(7);

console.log("Outside the function my_number is: ");
console.log(my_number);

Upvotes: 0

Views: 205

Answers (2)

Mritunjay
Mritunjay

Reputation: 25882

The error you are saying "TypeError: string is not a function" occurs when you do something like bellow

console.log("orangeCost"(5));// so here "orangeCost" is a string you cann't use that as function.

And you have to do a return from your function.

var orangeCost = function(price){
    var cost = price * 5;  //the 5 is the number of oranges
    return cost
};

Upvotes: 0

MD Sayem Ahmed
MD Sayem Ahmed

Reputation: 29166

simply do this -

return cost;

inside your orangecost -

var orangeCost = function(price){
    var cost = price * 5;  //the 5 is the number of oranges
    return cost;
};

console.log(orangeCost(5)); 

Upvotes: 2

Related Questions