简体   繁体   中英

Ternary Operator if else if issue

Been staring at this for days and from everything iv found it should be working but always ends up with ''. I'm checking the price change that can be positive or negative, hence the >0 <0.

 {parseInt(item.CheeseBarrelChange) > 0 ? <i className={styles.up}></i> :
 parseInt(item.CheeseBarrelChange) < 0 ? <i className={styles.down}></i>  :''} 

There doesn't seem to be anything wrong with the ternary statement itself, and so if the conditions keep failing with numbers you presume to be above or below zero; ensure the parseInt result of CheeseBarrelChange is not NaN .

 function change(input) { const c = parseInt(input.value); const i = c > 0 ? 'up' : c < 0 ? 'down' : ''; console.log({ change: c, icon: i}); } 
 <input value="0" onchange="change(this)"/> 

In order to check this, you may consider moving the statement into a function and using a regular if or switch statement.

检查是否是我得到NaN引起了我的思考,问题是ParseInt,切换到parseFloat,现在可以使用了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM