1.利用wx:if及wx:for数据绑定来实现输出乘法口诀表的编程
.wxml的代码:
<view class="pageview">
<block wx:for="{{nums}}"wx:for-item="line">
<view class="lineview">
<block wx:for="{{nums}}" wx:for-item="column">
<block wx:if="{{column>=line}}">
<view class="columnview">
{{line}}*{{column}}={{line*column}}
</view>
</block>
</block>
</view>
</block>
</view>
页面样式文件
/* pages/news/news.wxss */
.pgeview{
width: 100%;
height: 100vh;
}
.lineview{
width: 100%;
height: 5%;
display: flex;
}
.columnview{
width: 10%;
height: 100%;
font-size: 8px;
}
运行结果
2.编写程序,在Console控制台输出水仙花数(水仙花数是一个3位数的各位上的数字的3次幂之和等于它本身。例如,)
3.编写程序,在页面中输入水仙花数
// pages/news/news.js
Page({
/**
* 页面的初始数据
*/
data: {
sz: [],
},
onLoad: function() {
this.sun();
},
sun: function() {
let numbers = [];
for (let i = 100; i < 1000; i++) {
let digit1 = Math.floor(i / 100);
let digit2 = Math.floor((i % 100) / 10);
let digit3 = i % 10;
if (Math.pow(digit1, 3) + Math.pow(digit2, 3) + Math.pow(digit3, 3) == i) {
numbers.push(i);
}
}
this.setData({
sz: numbers.join(", ")
});
console.log(numbers);
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})
4.编写程序,在页面输出菱形图案
Page({
data: {
topHalf: [],
bottomHalf: [],
},
onLoad: function () {
const n = 5; // 菱形的行数,根据需要调整
const topHalf = [];
const bottomHalf = [];
for (let i = 1; i <= n; i++) {
const spaces = new Array(n - i).fill({});
const stars = new Array(2 * i - 1).fill({});
topHalf.push({ spaces, stars });
}
for (let i = n - 1; i >= 1; i--) {
const spaces = new Array(n - i).fill({});
const stars = new Array(2 * i - 1).fill({});
bottomHalf.push({ spaces, stars });
}
this.setData({ topHalf, bottomHalf });
},
});