第二章作业(微信小程序)

文章介绍了如何在微信小程序中使用wx:if和wx:for数据绑定技术实现乘法口诀表的动态输出,以及编写JavaScript函数寻找并显示水仙花数,并展示了如何生成菱形图案。
摘要由CSDN通过智能技术生成

1.利用wx:if及wx:for数据绑定来实现输出乘法口诀表

运行效果图:

wxml代码

<view class='con'>
  <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>

wxss代码:

.con{
  font-size: 8px
}

js代码:

Page({
  
})

json代码:{}

2.编写程序,在Console控制台输出水仙花数(水仙花数是指一个三位数的各位上的数字的3次幂之和等于它本身。例如1^3+5^3+3^3=153)

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;
  }
});

json代码

{
  "navigationBarTitleText": "水仙花"
 
}

wxss代码

.container {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.container text {
  margin: 5px;
}

wxml代码

<view class="container">
  <view wx:for="{{narcissisticNumbers}}" wx:key="{{index}}">
    <text>{{item}} </text>
  </view>
</view>

3.编写程序,在页面中输出水仙花数

运行效果图

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;
  }
});

json代码:

{
  "navigationBarTitleText": "水仙花"
}

wxss代码:

.container {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.row {
  display: flex;
  flex-direction: row;
}
.container text {
  margin: 5px;
}

wxml代码:

<view class="container">
  <view class="row">水仙花数共有:
    <view wx:for="{{narcissisticNumbers}}" wx:key="{{index}}">
      <text>{{item}} </text>
    </view>
  </view>
</view>

4.编写程序,在页面中输出菱形图案

运行效果图:

js代码:

Page({
  data: {
    diamondLines: []
  },
  onLoad: function () {
    const diamondLines = this.generateDiamond(10);
    this.setData({
      diamondLines: diamondLines
    });
  },
  generateDiamond: 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;
  }
});

json代码:

{
  "navigationBarTitleText": "菱形"
}

wxml代码:

<view class="container">
  <view class="diamond">
    <view wx:for="{{diamondLines}}" wx:key="{{index}}">
      <text>{{item}} </text>
    </view>
  </view>
</view>

wxss代码:

.container {
  display: flex;
  vertical-align: middle;
  text-align: center;
}
.diamond {
  margin-top: 40px; 
}
.container text {
  margin: 8px;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值