0

So I have been racking my brain why this doesn't work, I am sure that I am over thinking it but after googling for a few hours I just can't find it.

So the main thing I am trying is to create a JavaScript function “legs” which takes animal Object and returns some custom message (String) if the animal has 0 legs, 2, 4, and one message for others.

My attempt:

var animal = prompt("Type in an animal")

function legs(animal){

    if(animal == 'fish'){
        return "no legs";
    }else if(animal == 'Tiger'){
        return "4 legs";
    }else if(animal == 'kangaroo'){
        return "2 legs";
    }else { 
        return "I don't know";
        }
}

What am I doing wrong?

9
  • 2
    You're never calling the legs function Commented Jan 8, 2020 at 6:17
  • 1
    You don't call the function. Commented Jan 8, 2020 at 6:17
  • 1
    ^... leads to even a more fundamental question: Where are you expecting to see the return value of the function? Commented Jan 8, 2020 at 6:18
  • Not related, but I can't help myself: kangaroos have 4 legs. Just like we, humans. Commented Jan 8, 2020 at 6:20
  • 1
    @GerardoFurtado Bah, the joke doesn't work in english, in my native language "primate" is "a creature with hands" ... Commented Jan 8, 2020 at 6:26

3 Answers 3

1

You need to call legs function like legs(animal)

var animal = prompt("Type in an animal")

function legs(animal){

    if(animal == 'fish'){
        return "no legs";
    }else if(animal == 'Tiger'){
        return "4 legs";
    }else if(animal == 'kangaroo'){
        return "2 legs";
    }else { 
        return "I don't know";
        }
}
console.log(legs(animal));

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, I see what I did wrong. Thank you for your help. I was overthinking it.
0

this way you can call the leg() function.

var animal = prompt("Type in an animal")

function legs(animal) {
   if (animal == 'fish') 
      return "no legs";
    else if (animal == 'Tiger') 
      return "4 legs";
    else if (animal == 'kangaroo') 
      return "2 legs";
    else 
      return "I don't know";
}

let result = legs(animal)
console.log(result)

Comments

0

Just improving the answer. You to get the answer you need to call the function you have defined. Anyway better (cleaner) if you define it before reaching the interactive part (prompt).

Since you are returning in each scenario you do not need to use an else.

function legs(animal) {
  if (animal == 'fish') 
    return "no legs";
  if (animal == 'Tiger') 
    return "4 legs";
  if (animal == 'kangaroo') 
    return "2 legs";

  return "I don't know";
}

const animal = prompt("Type in an animal");

console.log(legs(animal));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.