微信小程序第二章基础作业

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

1.打开微信开发者工具,新建一个项目

2.在index.wxml输入以下代码

<!--index.wxml-->
<view class='con'>
  <view wx:for="{{[1,2,3,4,5,6,7,8,9]}}" wx:for-item="i"><!--对应行-->
    <view class='inline' wx:for="{{[1,2,3,4,5,6,7,8,9]}}" wx:for-item="j"><!--对应列-->
      <view wx:if="{{j<=i}}">
        {{i}}×{{j}}={{i*j}}
      </view>
    </view>
  </view>
</view>

3.在index.wxss输入以下代码

/** index.wxss **/
.con {
  font-size: 10px; 
  margin: 10px; 
}
.inline {
  display: inline-block; 
  width: 38px; 
}

4.保存后然后编译,结果如下

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

1.js代码

// index.js
Page({
  data: {
    result: ''
  },
  inputChange(e) {
    this.setData({
      number: e.detail.value
    })
  },
  checkNumber() {
    const number = this.data.number;
    if (number.length !== 3) {
      this.setData({
        result: '请输入一个三位数'
      })
      return;
    }
    const digit1 = parseInt(number.charAt(0));
    const digit2 = parseInt(number.charAt(1));
    const digit3 = parseInt(number.charAt(2));
    const sum = Math.pow(digit1, 3) + Math.pow(digit2, 3) + Math.pow(digit3, 3);
    if (sum === parseInt(number)) {
      this.setData({
        result: '是水仙花数'
      })
    } else {
      this.setData({
        result: '不是水仙花数'
      })
    }
  }
})

json代码

{
  "navigationBarBackgroundColor": "#000000",
  "navigationBarTitleText": "水仙花",
  "navigationBarTextStyle": "white",
  "backgroundTextStyle": "dark"
}

wxml代码

<view class="container">
  <view class="title">水仙花数判断</view>
  <input class="input" placeholder="请输入一个三位数" bindinput="inputChange" />
  <button class="button" bindtap="checkNumber">验证</button>
  <view class="result">{{result}}</view>
</view>

2.运行结果

 

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

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

json代码

{
  "navigationBarBackgroundColor": "#000000",
  "navigationBarTitleText": "水仙花数总共有",
  "navigationBarTextStyle": "white",
  "backgroundTextStyle": "dark"
}

wxml代码

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

wxss代码

.container { 
  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;
}

2.结果如图

 

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

1.wxml代码

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

2.wxss代码

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

3.运行结果

 

 

 

 

 

 

 

 

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值