Dil*_*eet 3 javascript ecmascript-6
我有一个应该呈现随机颜色的功能,但不重复颜色.
意思是如果随机选择蓝色,则无法再次选择.当然,这意味着需要一个默认值.我在考虑使用一个switch声明.
这是我目前的代码:
const colors = {
grey: '#BDC8D1',
blue: '#0500FF',
pink: '#FF00C7',
orange: '#FF7A00'
}
const randomColor = () => {
let keys = Object.keys(colors)
return colors[keys[keys.length * Math.random() << 0]]
}
Run Code Online (Sandbox Code Playgroud)
您可以"使用"一系列有效值来返回.通过消费我的意思是,从数组中删除它们.
例如:
// List of all valid values (those that can be returned)
const colors = [ 'red', 'green', 'blue' ];
// The default color (when all others have been "consumed")
const defaultColor = 'black';
// The function that returns a random color
const getRandomColor = () => {
// At least we return a color
let color = defaultColor;
// If all colors were previously consumed, we won't need
// to pick one randomly
if (colors.length > 0) {
// We select randomly an index from the colors array
const index = Math.floor(colors.length * Math.random());
// We store the color to return
color = colors[index];
// We remove it from the array
colors.splice(index, 1);
}
return color;
};
console.log(getRandomColor());
console.log(getRandomColor());
console.log(getRandomColor());
console.log(getRandomColor());
console.log(getRandomColor());
console.log(getRandomColor());Run Code Online (Sandbox Code Playgroud)
此解决方案的明显问题是您无法多次重复使用您的函数.更好的解决方案是创建迭代器.每次应用程序的某些部分需要生成随机的一系列颜色时,您将创建一个新的迭代器,并使用其next方法获取新值.检查以下内容:
// The default color (when all others have been "consumed")
const defaultColor = 'black';
const RandomColorIterator = () => {
// List of all valid values (those that can be returned)
const colors = [ 'red', 'green', 'blue' ];
return {
next: () => {
// At least we return a color
let color = defaultColor;
// If all colors were previously consumed, we won't need
// to pick one randomly
if (colors.length > 0) {
// We select randomly an index from the colors array
const index = Math.floor(colors.length * Math.random());
// We store the color to return
color = colors[index];
// We remove it from the array
colors.splice(index, 1);
}
return color;
},
};
};
const iterator1 = RandomColorIterator();
console.log('1:', iterator1.next());
console.log('1:', iterator1.next());
console.log('1:', iterator1.next());
console.log('1:', iterator1.next());
console.log('1:', iterator1.next());
console.log('1:', iterator1.next());
console.log('1:', iterator1.next());
console.log('1:', iterator1.next());
console.log('1:', iterator1.next());
const iterator2 = RandomColorIterator();
console.log('2:', iterator2.next());
console.log('2:', iterator2.next());
console.log('2:', iterator2.next());
console.log('2:', iterator2.next());
console.log('2:', iterator2.next());
console.log('2:', iterator2.next());
console.log('2:', iterator2.next());
console.log('2:', iterator2.next());Run Code Online (Sandbox Code Playgroud)
我一直在使用箭头功能从父范围中获利.这允许colors基于每个呼叫进行访问.
| 归档时间: |
|
| 查看次数: |
228 次 |
| 最近记录: |