执行算术运算对于空

问题描述:

事先检查反应我folllowing形式的道具:执行算术运算对于空

type Props = { 
row = ?number 
}; 

当我尝试做

if (row === null) { 
     return null; 
    } 
const percent = Math.round((row * 100)/20); 

我得到错误

算术运算的操作数必须是数字

如果我做

type Props = { 
row = number 
}; 

错误消失。 关于做什么的任何建议:)

+0

我收到错误'常量百分比= Math.round((行* 100)/ 20);'这行 – crystal

+0

看看这里:https://开头计算器。 com/a/16673024/2065611 –

+0

'const percent = row? Math.round((row * 100)/ 20):null;'试试这个。 –

如果此代码不在函数中。 这里即使你的行是空的,你正在执行你的第二条语句。把它放在其他部分。而且你还需要返回百分比。

像这个 -

if (row === null) { 
    return null; 
} 

else { 
    const percent = Math.round((row * 100)/20); 
    return percent; 
}