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))
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))
You need to return value out to the outermost level. You can just return simple string, so you can log or do something else to the returned value.
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))