Ad

Undefiend Is Returned In The Return Value Of Console Log

- 1 answer

I have a program that changes the string depending on the number that comes in like this, but it returns "undefined" for the value... What is the cause of this?

Again, I don't know what the solution is and I need your help.

function size(num){
   if (num >= 1000) {
       console.log('a')
   } else if(num >= 500) {
       console.log('b')
   } else if(num >= 300) {
       console.log('c')
   } else {
       console.log('d')
   }
}
console.log(size(100));
Ad

Answer

Your size method doesnt return anything and therefore, the result is undefined.

I guess you wanted to return a value from the function and outside, console.log it.

Something like this:

function size(num){
   if (num >= 1000) {
       return 'a';
   } else if(num >= 500) {
       return 'b';
   } else if(num >= 300) {
       return 'c';
   } else {
       return 'd';
   }
}
console.log(size(100));

A different approach would be to keep your original function and simply call it without console.log:

function size(num){
   if (num >= 1000) {
       console.log('a')
   } else if(num >= 500) {
       console.log('b')
   } else if(num >= 300) {
       console.log('c')
   } else {
       console.log('d')
   }
}

size(100);

Ad
source: stackoverflow.com
Ad