# Alchemy University 学习教程,第一大块:Practice Problems 2 **Published by:** [andrecronje](https://paragraph.com/@nicholastse/) **Published on:** 2023-01-01 **URL:** https://paragraph.com/@nicholastse/alchemy-university-practice-problems-2 ## Content 估值102亿融资5.45亿的Alchemy 项目,大学每周任务逐步开始。持有大学生早期卡的地址可以进入以下官网,开始大学生活: 这一期我们需要完成第二次作业,一些练习题: 1: Shortest String 最短的字符 查看要求,比较两串字符,选择最短的字符: 输入: function shortestString(str1, str2) { if (str1.length < str2.length) { return str1; } else { return str2; } } module.exports = shortestString; 点击运行。 1: Half Value 半价 查看要求,在一个集合中把每个元素除以二,小数四舍五入: 输入: 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 的数量,不区分大小写: 输入: 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 计算元音字母 查看要求,计算一个字符串中有多少个元音字母: 输入: 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 反转 查看要求,需要将一个字符串的字母顺序反过来: 输入: 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 : 输入: 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 求和 查看要求,给定两个集合,合并成第三个集合,要求一一对应相加: 输入: 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 计算元素 查看要求,需要计算出每个元素的数量: 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 计算牌分 查看要求,需要计算出玩家手中的牌的总分: 输入: 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 ## Publication Information - [andrecronje](https://paragraph.com/@nicholastse/): Publication homepage - [All Posts](https://paragraph.com/@nicholastse/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@nicholastse): Subscribe to updates - [Twitter](https://twitter.com/kobe80318935): Follow on Twitter