How to check if eval() doesen't return anything?

How to check if eval() doesen't return anything?
0

#1

Need help with the Javascript Calculator project: https://codepen.io/Teo03/pen/NaGRaB
I don’t know how to check if eval(num) doesen’t return anything and if that is a number.
Please help here is my code:

    num = output.value;
    var a = eval(num);
    if(typeof(a) !== 'undefined') && (a !== null)){
      console.log('Please be specific');
    } else {
      output.value = a;
    }

#2
    if(typeof a !== 'number'){
      console.log('Please be specific');
    } else {
      output.value = a;
    }

In your posted code, the if statement condition was going to display “Please be specific” in the console when a was a number. You want to display the message only if it is not a number (as shown in my code above).


#3

Thanks :slight_smile: , but how to console.log() if the eval() doesen’t return anything?


#4

The console.log above will still display if nothing is in a. If I am not understanding you, can you give me a specific example of when the console should display ‘Please be specific’, when it is currently not doing so?


#5

I want to concole.log() when a is a wrong condition like 7++ 7 or 20 // 5 or 20 - 5 - .


#6

You will need to build some logic to test for multiple operators in a row and flag those separately before trying the eval. For the 20 - 5 -, you will also need to make sure not to implement eval unless there is a number after the last operator.

Using eval comes with some unexpected situations just like this, where you will need some additional logic to deal with them.


#7

Thanks a lot for the help! :slight_smile: