1.利用wx:if及wx:for数据绑定来实现输出乘法口诀表
news.wxml代码:
<view class='one'>
<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>
news.wxss代码:
.one{
font-size: 8px
}
运行结果:
2.编写程序,在页面输出水仙花数(水仙花数是指一个三位数的各位上的数字的3次幂之和等于它本身。例 1^3+5^3+3^3=153)。
wxml代码:
<view class="row">
水仙花数:
<view wx:for="{{narcissisticNumbers}}" wx:key="{{index}}"> {{item}}, </view>
</view>
wxss代码:
.row {
display: flex;
}
json代码:
{
"navigationBarTitleText": "水仙花数目"
}
js代码:
Page({
data: {
narcissisticNumbers: []
},
onLoad: function () {
const narcissisticNumbers = this.a();
this.setData({
narcissisticNumbers: narcissisticNumbers
});
},
b: 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;
},
a: function () {
const narcissisticNumbers = [];
for (let i = 100; i < 1000; i++) {
if (this.b(i)) {
narcissisticNumbers.push(i);
}
}
return narcissisticNumbers;
}
});
3.编写程序,在Console控制台输出水仙花数
wxml代码:
<view class="row">
水仙花数:
<view wx:for="{{narcissisticNumbers}}" wx:key="{{index}}"> {{item}}, </view>
</view>
wxss代码:
.row {
display: flex;
}
json代码:
{
"navigationBarTitleText": "水仙花数目"
}
js代码:
Page({
data: {
narcissisticNumbers: []
},
onLoad: function () {
const narcissisticNumbers = this.a();
this.setData({
narcissisticNumbers: narcissisticNumbers
});
},
b: 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;
},
a: function () {
const narcissisticNumbers = [];
for (let i = 100; i < 1000; i++) {
if (this.b(i)) {
narcissisticNumbers.push(i);
}
}
return narcissisticNumbers;
}
});
4.编写程序,在页面中输出菱形图案。
wxml代码:
<view class="a">
<view wx:for="{{bb}}" wx:key="{{index}}">
<text>{{item}} </text>
</view>
</view>
wxss代码:
.a {
text-align: center;
}
json代码:
{
"navigationBarTitleText": "菱形"
}
js代码:
Page({
data: {
bb: []
},
onLoad: function () {
const bb = this.gd(10);
this.setData({
bb: bb
});
},
gd: function (height) {
const lines = [];
for (let i = 1; i <= height; i++) {
let line = '';
for (let j = 1; j <= height - i; j++) {
line += ' ';
}
for (let k = 1; k <= 2 * i - 1; k++) {
line += '*';
}
lines.push(line);
}
for (let i = height - 1; i >= 1; i--) {
let line = '';
for (let j = 1; j <= height - i; j++) {
line += ' ';
}
for (let k = 1; k <= 2 * i - 1; k++) {
line += '*';
}
lines.push(line);
}
return lines;
}
});