1.利用wx.if及wx.for数据绑定来实现输出乘法口诀表(图-2-21)的编程。
new.js
Page({});
new.json
{
}
new.wxss
.ye{
font-size: 8px;
text-align: center;
}
new.wxml
<view class='ye'>
<view wx:for="{{[1,2,3,4,5,6,7,8,9]}}" wx:for-item="i">
<view style='display:inline-block;width:35px' wx:for="{{[9,8,7,6,5,4,3,2,1]}}" wx:for-item="j">
<view wx:if="{{i<=j}}">
{{i}}*{{j}}={{i*j}}
</view>
</view>
</view>
</view>
结果
2.编写程序,在Console控制台输出水仙花数(水仙花数是指一个3位数的各位上的数字的3次幂之和等于它本身。例如,1^3+5^3+3^3=153)。
do.js
Page({
data: {
narcissisticNumbers: []
},
onLoad: function () {
const narcissisticNumbers = this.findNarcissisticNumbers();
this.setData({
narcissisticNumbers: narcissisticNumbers
});
},
isNarcissisticNumber: function (num) {
const strNum = num.toString();
const digits = strNum.length;
let sum = 0;
for (let i = 0; i < digits; i++) {
sum += Math.pow(parseInt(strNum[i]), digits);
}
return sum === num;
},
findNarcissisticNumbers: function () {
const narcissisticNumbers = [];
for (let i = 100; i < 1000; i++) {
if (this.isNarcissisticNumber(i)) {
narcissisticNumbers.push(i);
}
}
return narcissisticNumbers;
}
});
do.json
{
"navigationBarTitleText": "水仙花"
}
do.wxss
<view class="container">
<view wx:for="{{narcissisticNumbers}}" wx:key="{{index}}">
<text>{{item}} </text>
</view>
</view>
do.wxml
.container {
display: flex;
flex-direction: column;
align-items: center;
}
.container text {
margin: 5px;
}
3.编写程序,在页面中输出水仙花数
new.js
Page({
data: {
narcissisticNumbers: []
},
onLoad: function () {
const narcissisticNumbers = this.findNarcissisticNumbers();
this.setData({
narcissisticNumbers: narcissisticNumbers
});
},
isNarcissisticNumber: function (num) {
const strNum = num.toString();
const digits = strNum.length;
let sum = 0;
for (let i = 0; i < digits; i++) {
sum += Math.pow(parseInt(strNum[i]), digits);
}
return sum === num;
},
findNarcissisticNumbers: function () {
const narcissisticNumbers = [];
for (let i = 100; i < 1000; i++) {
if (this.isNarcissisticNumber(i)) {
narcissisticNumbers.push(i);
}
}
return narcissisticNumbers;
}
});
new.json
{
"navigationBarTitleText": "水仙花数目"
}
new.wxss
<view class="container">
<view class="row">水仙花数共有:
<view wx:for="{{narcissisticNumbers}}" wx:key="{{index}}">
<text>{{item}} </text>
</view>
</view>
</view>
new.wxml
.container {
display: flex;
flex-direction: column;
align-items: center;
}
.row {
display: flex;
flex-direction: row;
}
.container text {
margin: 5px;
}
结果
4.第四题
news.wxml
<view class="content">
<view> *</view>
<view> *** </view>
<view> *****</view>
<view> *******</view>
<view>**********</view>
<view> *******</view>
<view> *****</view>
<view> ***</view>
<view> *</view>
</view>
news.wsxx
.content{
margin-top:20px;
font-size: 22px;
text-align: center;
}
结果