第二章作业

本文介绍了如何使用微信小程序开发环境,通过JavaScript编写nxn.js和shuixianhuashu.js,实现乘法口诀表展示和水仙花数查找功能,以及创建菱形图案的示例。
摘要由CSDN通过智能技术生成

创建一个新项目来装我们的第二次作业:No2

乘法口诀表

新建文件夹nxn,再去app.json里添加一下

然后创建旗下的文件:

nxn.js

Page({
  data: {
    table: []
  },
  onLoad: function () {
    let table = [];    for (let i = 1; i <= 9; i++) {
      let row = [];
      for (let j = 1; j <= 9; j++) {
        row.push(i * j);
      }
      table.push(row);
    }
    this.setData({
      table: table
    });
  }
})

nxn.json

{
  "pages":[
  ]
}

nxn.wxml

<view class='row' wx:for="{{[1,2,3,4,5,6,7,8,9]}}" wx:for-item="row">
  <view class='col' wx:for="{{[1,2,3,4,5,6,7,8,9]}}" wx:for-item="col" wx:if="{{col <= row}}">
    {{col}}*{{row}} = {{col*row}}
  </view>
</view>

nxn.wxss

.row{
  font-size: 8px;
}
.col{
  display: inline-block;
}

结果如图:

水仙花数

创建文件夹sxh,记入app.json

文件:

sxh.js

Page({
  data: {
    messagez:[]
  },
  onLoad: function () {
    this.findmessage();
  },
  findmessage: function (){ 
    const num =[];
  for (let i=100;i<1000;i++){
     const a = Math.floor(i / 100);
  const b = Math.floor((i % 100) / 10); const c=i% 10;
  if (a ** 3 + b** 3 +c**3===i){ 
    num.push(i);}}
  this.setData({
  message: num});
  console.log("水仙花数:"+num);
},
})
Page({ 
  data:{
  narcissisticNumbers: []
},
  onLoad: function () {
  this.findNarcissisticNumbers();
},
  findNarcissisticNumbers: function (){ 
    const numbers =[];
  for (let i=100;i<1000;i++){
     const a = Math.floor(i / 100);
  const b = Math.floor((i % 100) / 10); const c=i% 10;
  if (a ** 3 + b** 3 +c**3===i){ 
    numbers.push(i);}}
  this.setData({
  narcissisticNumbers: numbers});}
  });
  
  Page({ 
    data:{
    narcissisticNumbers: []
  },
    onLoad: function () {
    this.findNarcissisticNumbers();
  },
    findNarcissisticNumbers: function (){ 
      const numbers =[];
    for (let i=100;i<1000;i++){
       const a = Math.floor(i / 100);
    const b = Math.floor((i % 100) / 10); const c=i% 10;
    if (a ** 3 + b** 3 +c**3===i){ 
      numbers.push(i);}}
    this.setData({
    narcissisticNumbers: numbers});}
    });

sxh.json

{
  "navigationBarBackgroundColor": "#808080",
  "navigationBarTitleText": "水仙花数",
  "navigationBarTextStyle":"black",
  "usingComponents": {}
}

sxh.wxml

<text>水仙花数:</text>
<view class="Shui">
<view class="shui">
<text wx:for="{{message}}" wx:key="*this">{{item}}
</text>
</view>
</view>

截图:

输出水仙花数

创建shuixianhuashu,记入app.json

文件:

shuixianhuashu.js

Page({ 
  data:{
  narcissisticNumbers: []
},
  onLoad: function () {
  this.findNarcissisticNumbers();
},
  findNarcissisticNumbers: function (){ 
    const numbers =[];
  for (let i=100;i<1000;i++){
     const a = Math.floor(i / 100);
  const b = Math.floor((i % 100) / 10); const c=i% 10;
  if (a ** 3 + b** 3 +c**3===i){ 
    numbers.push(i);}}
  this.setData({
  narcissisticNumbers: numbers});}
  });

shuixianhuashu.json

{
  "navigationBarBackgroundColor": "#808080",
  "navigationBarTitleText": "水仙花数",
  "navigationBarTextStyle":"black",
  "usingComponents": {}
}

shuixianhuashu.wxml

<view class="sxhs">
<view class="narcissistic-numbers">
<text wx:for="{{narcissisticNumbers}}" wx:key="*this">{{item}}</text></view>
</view>

shuixianhuashu.wxss

.sxhs { 
  display: flex;
  flex-direction: column;
   align-items: center;
  justify-content: center;
   height: 100vh;
}
  .narcissistic-numbers { 
    margin-top: 20px;
  }
  .narcissistic-numbers text {
  display: block;
  margin-bottom: 10px;
}

截图:

菱形

创建文件夹ling,记入app.json

文件:

ling.js

// pages/ling/ling.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})

ling.json

{
  "navigationBarBackgroundColor": "#808080",
  "navigationBarTitleText": "菱形图案",
  "navigationBarTextStyle":"black",
  "usingComponents": {}
}

ling.wxml

<view class="diamond">
  <view>    *</view>
  <view>   ***</view>
  <view>  *****</view>
  <view> *******</view>
  <view>*********</view>
  <view> *******</view>
  <view>  *****</view>
  <view>   ***</view>
  <view>    *</view>
</view>

 ling.wxss

.diamond {
  text-align: center;
  margin-top: 20px;
  font-size: 20px;
}

截图:

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值