I am trying to solve this simple js function question but its giving the undefined as output along with the correct answer. I know its probably because of the return value issue but how solve in this situation?
function findMax(a,b,c){
if(a>b && a>c){
console.log("a" + " is max")
}else if(a<b && b>c){
console.log("b" + " is max")
}else{
console.log("c" + " is max")
}
}
console.log(findMax(2,4,5))
>Solution :
your console logging the return value which is undefined. just get rid of the last con sole log.
function findMax(a,b,c){
if(a>b && a>c){
console.log("a" + " is max")
}else if(a<b && b>c){
console.log("b" + " is max")
}else{
console.log("c" + " is max")
}
}
findMax(2,4,5);
Or you could return the string
function findMax(a,b,c){
if(a>b && a>c){
return "a" + " is max";
}else if(a<b && b>c){
return "b" + " is max";
}else{
return "c" + " is max";
}
}
console.log(findMax(2,4,5))