Ad
Undefiend Is Returned In The Return Value Of Console Log
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
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad