【作业】ES6

1.定义一个构造函数Student,包含id、name、html、css、Javascript 5个属性,利用该构造函数创建5对象并存入数组arr中。然后对数组进行如下操作:

(1)找出html、css、Javascript三门课程总成绩最高的Student对象,输出

(2)对数组中对象按javascript成绩降序排列

<script>
    // 1.构造Student函数
    function Student(id,name) {
        this.id = id
        this.name = name
        // 假设最高成绩为0
        this.html = 0
        this.css = 0
        this.js = 0
        // 总成绩最高
        this.getScore = function(){
            return this.html + this.css + this.js
        }
    }
    // 2.创建对象
    let s1 = new Student('青青草原羊村1号','懒羊羊')
    s1.html = 88
    s1.css = 66
    s1.js = 77
    let s2 = new Student('青青草原羊村2号','喜羊羊')
    s2.html = 77
    s2.css = 78
    s2.js = 66
    let s3 = new Student('青青草原羊村3号','美羊羊')
    s3.html = 100
    s3.css = 100
    s3.js = 98
    let s4 = new Student('青青草原羊村4号','暖羊羊')
    s4.html = 56
    s4.css = 78
    s4.js = 88
    let s5 = new Student('青青草原羊村5号','沸羊羊')
    s5.html = 78
    s5.css = 98
    s5.js = 89
    // 冒泡排序
    // 总成绩
    // 方法一
    let arr = [s1,s2,s3,s4,s5]
    for (let i = 0; i < arr.length; i++) {
        for (let j = 0; j < arr.length - 1; j++) {
            if (arr[j].getScore() < arr[j+1].getScore()) {
                let temp = arr[j]
                arr[j] = arr[j+1]
                arr[j+1] = temp
            }
        }
    }
    console.log(arr[0])
    // 方法二
    let max = arr[0]//假设第一个总成绩最高
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].getScore()>max.getScore()) {
           max = arr[i]
        }
    }
    console.log('综合成绩最高的对象是',max)
    // 按js成绩降序排列
    for (let i = 0; i < arr.length; i++) {
        for (let j = 0; j < arr.length - 1; j++) {
            if (arr[j].js < arr[j+1].js) {
               let temp = arr[j]
               arr[j] = arr [j+1]
               arr[j+1] = temp
            }
        }
    }
    console.log('按javascript成绩降序排列:',arr)
</script>

2.设有字符串”yekmaakkccekymbvb”,求出该字符串中有多少种字符,以及每个字符的个数

<script>
    let str = 'yekmaakkccekymbvb'
    // 定义一个对象
    /* // 方法一
    let kinds = {}
    let count = 0
    for (let i = 0; i < str.length; i++) {
        let newstr = str.charAt(i)
        if (kinds[newstr]) {
            kinds[newstr]++
        }else{
            kinds[newstr] = 1
            count ++
        }
    }
    console.log('该字符串中有'+count+'种字符,以及每个字符的个数为',kinds) */
    let kinds = {}
    for (let i = 0; i < str.length; i++) {
        if (kinds[str[i]]) {//表示对象kinds有str[i]这个属性,对应的属性值加1
            kinds[str[i]]++
        }else{
            kinds[str[i]] = 1
        }
    }
    console.log(kinds)
</script>

3.现有若干图书信息(包含名称title、作者author、定价price)需要存储到set集合中,保证集合中无重复元素,并遍历查看。

<script>
    function Book(title,author,price) {
        this.title =title
        this.author = author
        this.price = price
    }
    let b1 = new Book('童年','高尔基',23)
    let b2 = new Book('在人间','高尔基',33)
    let b3 = new Book('我的大学','高尔基',23)
    let bookset = new Set()
    bookset.add(b1).add(b2).add(b3)
    console.log(bookset)
</script>

4.在某次考试中,学生的成绩信息如下: 姓名、年龄、 成绩:Tom 20 90; Jerry 22 95; John 20 100; Lily 22 100 ;Lucy 22 90; Kevin 22 90 请对以上同学的成绩做降序排序,如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序。

<script>
    function Student(name,age,score) {
        this.name = name
        this.age = age
        this.score =score
        this.Max = function(){
            return this.score
            // this.min = function(){
            //     return this.age
            // }
        }
    }
    let s1 = new Student('Tom',20,90)
    let s2 = new Student('Jerry',22,95)
    let s3 = new Student('John',20,100)
    let s4 = new Student('Lily',22,100)
    let s5 = new Student('Lucy',22,90)
    let s6 = new Student('Kevin',22,90)
    let arr=[s1,s2,s3,s4,s5,s6]
    for (let i = 0; i < arr.length; i++) {
        for (let j = 0; j < arr.length-1; j++) {
            if (arr[j].Max()<arr[j+1].Max()) {
                let temp = arr[j]
                arr[j]= arr[j+1]
                arr[j+1] =temp
            }
        }   
    }
    console.log(arr)
    /* let max = arr[0]
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].Max()>max.Max()) {
            max = arr[i]
        }
    }
    console.log(max) */
</script>

5.设计一个构造函数IntSet,包括属性 len(集合的长度)和集合web_set(存放元素),以及如下方法:insert(val) 向集合中添加一个元素,重复的元素不能添加;length() 返回集合的元素个数;getInt(index) 返回集合中位置index的元素;disp() 输出集合的所有元素;union(s2) 实现两个集合的并运算;intersection(s2) 实现两个集合的交运算;difference(s2) 实现两个集合的差运算.要求:定义两个整数集合{2,4,1,3,5}和{2,5,10},输出前者的元素个数以及它们进行集合的并、交、差运算

<script>
    function IntSet() {
        this.web_set = new Set();
        this.len = this.web_set.size
        // insert(val) 向集合中添加一个元素,重复的元素不能添加
        this.insert = function(){
            for (let i of arguments) {
                this.web_set.add(i)
            }
            this.len = this.web_set.size
        }
        // length() 返回集合的元素个数
        this.length = function(){
            return this.len
        }
        // getInt(index) 返回集合中位置index的元素
        this.getInt = function(index){
            let arr = [...this.web_set]
            if (index >= 0 && index < arr.length) {
                return arr[index]
            }else{
                return undefined
            }
        }
        // disp() 输出集合的所有元素
        this.disp = function(){
            console.log('集合元素是:')
            console.log(this.web_set)
        }
        // union(s2) 实现两个集合的并运算
        this.union = function(s2){
            let temp = new Set([...this.web_set,...s2.web_set])
            return temp
        }
        // intersection(s2) 实现两个集合的交运算
        this.intersection = function(s2){
            let tempArr = [...this.web_set]
            let newarr = tempArr.filter(function(item){
                if (s2.web_set.has(item)) {
                    return item
                }
                // return s2.web_set.has(item)
            })
            return newarr
        }
        // difference(s2) 实现两个集合的差运算
        this.difference = function(s2){
            let tempArr = [...this.web_set]
            let newarr= tempArr.filter(function(item){
                return !s2.web_set.has(item)
            })
            return newarr
        }
    }
    let set1 = new IntSet()
    set1.insert(2,4,1,3,5)
    set1.disp()
    console.log('set1的长度',set1.length())
    let set2 = new IntSet()
    set2.insert(2,5,10)
    set2.disp()
        
    let bj =set1.union(set2)
    console.log('并集',bj)

    console.log('交集',set1.intersection(set2))
    console.log('差集',set1.difference(set2))

</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值