s1ngs1ng / freecodecampproject Goto Github PK
View Code? Open in Web Editor NEWfreeCodeCamp Project: Calculator ⚙️ and Local Weather app 🌨
Home Page: https://singsing.io/freeCodeCampProject
freeCodeCamp Project: Calculator ⚙️ and Local Weather app 🌨
Home Page: https://singsing.io/freeCodeCampProject
关于数组的排序你写了3个函数: transformFunc、_isLargerThan、_comparator 来实现,代码互相缠绕,实在过于冗余。
其实只需一行即可:
let sortedValue = cardList.map(card => card.value).sort((a,b)=>b-a);
getRandomIndex(max){
return Math.floor(Math.random() * (max));
}
freeCodeCampProject/poker-game/lib/compare.js
Line 133 in 57ce81b
此处的两处判断逻辑完全一样,只有变量不同,不符合 DRY 原则。
card.suit = this.suit[this._getRandomIndex(this.suit.length)];
建议将 while 循环 改成 for 循环,毕竟 for 循环就是从 while 循环演变而来,解决了结构分散的问题。
用 while 来判断如果生成的 card 是重复的,则循环生成新的 card,直到新的 card 为唯一。
此处我的思路是:
首先生成52张牌。
然后在52张中随机取一张,在51张中随机取一张,以此类推,取5张为玩家手牌。
再次调用时,从47张中随机取一张,取满5张为电脑手牌。
利用数组的 splice 特性,无需手动去掉重复卡牌。
利用数组的 length 属性,无需手动计算具体的位置。
freeCodeCampProject/poker-game/lib/compare.js
Line 233 in 57ce81b
sort 是一个抽象程度很高的数组方法,在 v8 中根据ECMAScript 中定义的 sort 方法有了具体实现。
开发者只需关注参数的顺序即可,例如:
[1,2,5,11,9].sort((a,b)=>a-b) // [1,2,5,9,11]
[1,2,5,11,9].sort((a,b)=>b-a) // [11,9,5,2,1]
此处的修改可以参考我的代码哦,^_^
Object.assign 语句重复出现,不符合DRY原则。
直接 return {value:0} 即可, 减少读者心智负担。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.