利用wx:if及wx:for数据绑定来实现输出乘法口诀表的编程
demo.wxml代码如下:
<view class='leo'>
<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="o">
<view wx:if="{{i<=o}}">
{{i}}*{{o}}={{i*o}}
</view>
</view>
</view>
</view>
demo.wxss代码如下
.leo{
font-size: 9px
}
运行结果如下:
编写程序,在Console控制台输出水仙花数(水仙花数是指一个三位数的各位上的数字的3次幂之和等于它本身。例如,1^3+5^3+3^3=153)。
demo.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;
}
});
demo.wxml代码如下:
<view class="leo">
<view wx:for="{{narcissisticNumbers}}" wx:key="{{demo}}">
<text>{{item}} </text>
</view>
</view>
demo.wxss代码如下:
.leo {
display: flex;
flex-direction: column;
align-items: center;
}
.leo text {
margin: 5px;
}
运行结果如下:
编写程序,在页面中输出水仙花数。
demo.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;
}
});
demo.wxml代码如下:
<view class="container">
<view class="row">水仙花数共有:
<view wx:for="{{narcissisticNumbers}}" wx:key="{{index}}">
<text>{{item}} </text>
</view>
</view>
</view>
demo.wxss代码如下:
.container {
display: flex;
flex-direction: column;
align-items: center;
}
.row {
display: flex;
flex-direction: row;
}
.container text {
margin: 5px;
}
运行结果 如下:
编写程序,在页面中输出菱形图案
demo.wxml代码如下:
<view class="leo">
<view>菱形</view>
<view> *</view>
<view> ***</view>
<view> *****</view>
<view> *******</view>
<view>*********</view>
<view>*********</view>
<view> *******</view>
<view> *****</view>
<view> ***</view>
<view> *</view>
</view>
demo.wxss代码如下:
.leo{
text-align: center;
}
运行结果如下: