1、案例说明:

2、案例实现
2.1、案例代码
wxml:
<view class="box">
<view class="title">条件语句和数学函数</view>
<input type="digit" bindblur="circ"/>
<view class="end">计算y的值为:{{y}}</view>
</view>
wxss:
input{
border-bottom: solid 2px blue;
margin: 20px auto;
}
.end{
margin-bottom: 10px;
}
js:
// pages/dem_2/dem_2.js
Page({
/**
* 页面的初始数据
*/
data: {
},
circ: function(e){
var x;
var y;
// console.log(e)
x = e.detail.value;
if(x<0){
y = Math.abs(x);
}
else if(x<10){
y = Math.exp(x) * Math.sin(x);
}
else if(x<20){
y = Math.pow(x,3);
}
else{
y = (3 + 2 * x) * Math.log(x);
}
this.setData({
y : y
})
}
})
2.2、结果展示:

3、总结说明:
3.1、知识汇总:





3.2、踩坑说明:
- js代码中条件语句的模糊和调用数学公式的方法不清楚踩的坑:
//错误的js代码:
// pages/dem_2/dem_2.js
Page({
/**
* 页面的初始数据
*/
data: {
},
circ: function(e){
var x;
var y;
// console.log(e)
x = e.detail.value;
if(x<0){
this.setData({
y : -1 * x
})
}
if(0<=x<10){
this.setData({
y : e ^ x * sin(x)
})
}
if(10<=x<=20){
this.setData({
y : e ^ x * sin(x)
})
}
if(20<=x){
this.setData({
y : (3 + 2 * x)*ln(x)
})
}
}
})
- else格式报错
//错误代码
else(20<=x){
y = (3 + 2 * x) * Math.log(x);
}
报错:unknown: Missing semicolon
排错:else后面是不能加条件的
//正确代码
else{
y = (3 + 2 * x) * Math.log(x);
}
网友评论