第二次作业

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

结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值