1.九九乘法表
js代码
Page({
data: {
sj: []
},
onLoad: function () {
this.cfkj();
},
cfkj: function() {
let table = [];
for (let i = 1; i <= 9; i++) {
let row = [];
for (let j = i; j <= 9; j++) {
row.push(`${i}x${j}=${i*j+"\t"}`);
}
table.push(row);
}
this.setData({
sj: table
});
}
});
wxml代码
<view class="ym">
<view wx:for="{{sj}}" wx:key="index" >
<text wx:for="{{item}}" wx:key="index">{{item}}</text>
</view>
</view>
结果
2.控制输出水仙花数
js代码
function sxhs() {
for (let num = 100; num < 1000; num++) {
let bai = Math.floor(num / 100);
let shi = Math.floor((num % 100) / 10);
let ge = num % 10;
if (Math.pow(bai, 3) + Math.pow(shi, 3) + Math.pow(ge, 3) === num) {
console.log(num);
}
}
}
sxhs();
结果
3.输出水仙花数
js代码
Page({
data: {
sj: ''
},
onLoad: function () {
this.sxhs();
},
sxhs: function() {
let sz = [];
for (let num = 100; num < 1000; num++) {
let bai = Math.floor(num / 100);
let shi = Math.floor((num % 100) / 10);
let ge = num % 10;
if ((bai ** 3 + shi ** 3 + ge ** 3) === num) {
sz.push(num);
}
}
this.setData({
sj:`水仙花数共有:${sz.join(',')}`
});
}
});
4.菱形
js代码
Page({
data:{
lx:[]
},
onLoad:function(){
this.gd(5);
},
gd:function(n){
let lines=[];
for(let i=0;i<n;i++){
let kg=' '.repeat(n-i-1);
let xh='*'.repeat(i*2+1);
lines.push(kg+xh);
}
for(let i=n-1;i>=0;i--){
let kg1=' '.repeat(n-i-1);
let xh1='*'.repeat(i*2+1);
lines.push(kg1+xh1);
}
this.setData({
lx:lines
});
}
})
wxml代码
<view class="js">
<block wx:for="{{lx}}" wx:key="index">
<view>{{item}}</view>
</block>
</view>
less代码
.js view{
text-align: center;
}
结果