# Alchemy University 学习教程，第一大块：Practice Problems 2

By [andrecronje](https://paragraph.com/@nicholastse) · 2023-01-01

---

估值102亿融资5.45亿的Alchemy 项目，大学每周任务逐步开始。持有大学生早期卡的地址可以进入以下官网，开始大学生活：

这一期我们需要完成第二次作业，一些练习题：

1: Shortest String 最短的字符

查看要求，比较两串字符，选择最短的字符：

![](https://storage.googleapis.com/papyrus_images/c95d024879305ae0c172b98707d3dd0f90f5b659355eca3c171f3b001f3e256e.png)

输入：

    function shortestString(str1, str2) {
        if (str1.length < str2.length) {
            return str1;
        } else {
            return str2;
        }
    }
    
    module.exports = shortestString;
    

点击运行。

1: Half Value 半价

查看要求，在一个集合中把每个元素除以二，小数四舍五入：

![](https://storage.googleapis.com/papyrus_images/aea6ef3f45ee06d0af5788dab5ad8fe2e5e45171da69dfce45a60c48bd8d0820.png)

输入：

    function halfValue(numbers) {
        let retarr = [];
        
        for (let i = 0; i < numbers.length; i++) {
            if (0 === numbers[i] % 2) {
                retarr.push(numbers[i] / 2);
            } else {
                let tmp = Math.floor(numbers[i] / 2) + 1;
                retarr.push(tmp);
            }
        }
    
        return retarr;
    }
    
    module.exports = halfValue;
    

点击运行。

1: Count C 计算C

在这里，我们需要统计 C 的数量，不区分大小写：

![](https://storage.googleapis.com/papyrus_images/8fedeb0632268d8933c9422e70c8d7c0a1e15cdc875b4d0303a5af425967f3fd.png)

输入：

    function countC(str) {
        let c = 0;
        let cstr = str.toLowerCase();    
        let i = cstr.indexOf("c");
    
        while (-1 !== i) {
            c = c + 1;
            i = cstr.indexOf("c", i + 1);
        }
    
        return c;
    }
    
    module.exports = countC;
    

点击运行。

1: Count Vowels 计算元音字母

查看要求，计算一个字符串中有多少个元音字母：

![](https://storage.googleapis.com/papyrus_images/a4f48790aaa74cb1968eda3ac428535e055a19066c81e6d0de521d638fc5b984.png)

输入：

    function countVowels(str) {
        let vowels = "aeiou";
        let c = 0;
        let vowelstr = str.toLowerCase();
    
        for (let i = 0; i < vowelstr.length; i++) {
            if (-1 !== vowels.indexOf(vowelstr[i])) {
                c = c + 1;
            }
        }
    
        return c;
    }
    
    module.exports = countVowels;
    

1: Reverse 反转

查看要求，需要将一个字符串的字母顺序反过来：

![](https://storage.googleapis.com/papyrus_images/adb22f52b865dcab6996058f4602ec4b6ab98e50b4ef287143fe11edaa34108d.png)

输入：

    function reverse(string) {
        let retstr = "";
        for (let i = 0; i < string.length; i++) {
            retstr = string[i] + retstr;
        }
    
        return retstr;
    }
    
    module.exports = reverse;
    

点击运行。

1: Palindrome 回文

查看要求，简单解释就是让一串字符有对称性，如 131 12321 141 14541 ：

![](https://storage.googleapis.com/papyrus_images/83d184e71c69c7a6f5725c9a24dde94d0ebbb66a7a1617b91734fb3751573d2f.png)

输入：

    function isPalindrome(string) {
        let i = 0;
        let endi = string.length - 1;
        while (endi >= i) {
            if (string[i] === string[endi]) {
                i = i + 1;
                endi = endi - 1;
            } else {
                return false;
            }
        }
    
        return true;
    }
    
    module.exports = isPalindrome;
    

1: Sum Together 求和

查看要求，给定两个集合，合并成第三个集合，要求一一对应相加：

![](https://storage.googleapis.com/papyrus_images/04588c4f05db7a116602add1e0dfa1faec8c76a8cfe18471345535da3f7f8c56.png)

输入：

    function sumTogether(arr1, arr2) {
        let a = [];
    
        for(let i = 0; i < arr1.length; i++) {
            a[i] = arr1[i] + arr2[i];
        }
    
        return a;
    }
    
    module.exports = sumTogether;
    

1: Count the Elements 计算元素

查看要求，需要计算出每个元素的数量：

![](https://storage.googleapis.com/papyrus_images/e4e71d5900754c88f810d0305811eda190c805805ec4b9daf3bc37059ecc1cbe.png)

    function countElements(elements) {
        let countEle = {};
    
        for (let i = 0; i < elements.length; i++) {
            if (countEle.hasOwnProperty(elements[i])) {
                countEle[elements[i]] = countEle[elements[i]] + 1;
            } else {
                countEle[elements[i]] = 1;
            }
        }
    
        return countEle;
    }
    
    module.exports = countElements;
    

点击运行。

1: Player Hand Score 计算牌分

查看要求，需要计算出玩家手中的牌的总分：

![](https://storage.googleapis.com/papyrus_images/52d887047889f99f4b5bea4cb3d65be4305131c3a5ebf8472c423d93412ef1a4.png)

输入：

    function playerHandScore(hand) {
        const jqkScore = {"J":2, "Q":3, "K":4};
        let s = 0;
    
        for (let i = 0; i < hand.length; i++) {
            s = s + jqkScore[hand[i]];
        }
    
        return s;
    }
    
    module.exports = playerHandScore;
    

以上练习 2 的作业已经完成。

未完待续......

了解更多，请关注作者：[https://twitter.com/bitc2024](https://twitter.com/bitc2024)

---

*Originally published on [andrecronje](https://paragraph.com/@nicholastse/alchemy-university-practice-problems-2)*
