Skip to content

Instantly share code, notes, and snippets.

@Hupotronic
Forked from 140bytes/LICENSE.txt
Created October 2, 2012 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hupotronic/3818934 to your computer and use it in GitHub Desktop.
Save Hupotronic/3818934 to your computer and use it in GitHub Desktop.
Convert a floating point value to stars!

Usage: Assign the function to something, eg. var stars = function(a,b,c,d){ ... }
The function takes a floating point value and turns it into a "star" array. For example:

stars(2.5) => [2,2,1,0,0]
stars(3.8) => [2,2,2,2,0]

In these arrays, 2 = full star, 1 = half-star, 0 = no star. The function also takes an optional second argument which you can use to determine the amount of stars (default is 5). Like so:

stars(7.4,10) => [2,2,2,2,2,2,2,1,0,0,0]

This function can be useful if you want to display a star rating where each star is an individual element and you want half-stars as well.

function(a,b,c,d) // "a" is the floating point value, "b" is an optional "amount of stars".
{
for(
a += a + .5, // The +.5 allows us to round the original value by just discarding the decimals.
c = d = []; // c is our star array and d is our array index.
// ++ on an empty array turns it into a number, so we don't need to assign d = 0.
d < (b || 5); // if b is unset, use the default value 5.
c[d++] = // Set the value of c[d] (and increment d) to...
a >= d * 2 ? 2 : // 2, if a >= d*2, else to...
a & 1 ? a-- & 1 : 0 // 1, if int(a) is odd (then make it even), else to 0.
); // we can decrement from a, because we know anything after a half-star must be empty,
// and the decrementing itself is done to make sure there won't be more half-stars.
return c // Return the complete star array.
}
function(a,b,c,d){for(a+=a+.5,c=d=[];d<(b||5);c[d++]=a>=d*2?2:a&1?a--&1:0);return c}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 Hupo <https://github.com/Hupotronic>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "stars",
"description": "Convert a floating point value into a 'star' array.",
"keywords": [
"rating",
"average",
"stars",
"number",
"format"
]
}
@jed
Copy link

jed commented Oct 2, 2012

lotta space left, you should try rendering the stars!

@Hupotronic
Copy link
Author

I'd say trying to render the stars inside the function and returning that would be a lot less useful than the array it returns at the moment. Though if Unicode had a "half-star" glyph making the function return a star string instead would be easy...

@Hupotronic
Copy link
Author

So I wanted to see how much I could shave off from this, and managed to get it down to 84 characters (from the original 96). For anyone interested, here's my "progression":

96: function(a,b){b=b?b:5;var c=0,d=0,e=[],f=~~(a*2+.5);while(d<b)e[d++]=f<++c?0:f>c++?2:1;return e}
95: function(a,b){a=~~(a*2+.5);b=b?b:5;var c=[],d=0;while(d<b)c[d++]=a>=d*2?2:a&1?a--&1:0;return c}
93: function(a,b,c,d){a=~~(a*2+.5);b=b?b:5;c=d=[];while(d<b)c[d++]=a>=d*2?2:a&1?a--&1:0;return c}
91: function(a,b,c,d){a=~~(a*2+.5);c=d=[];while(d<(b?b:5))c[d++]=a>=d*2?2:a&1?a--&1:0;return c}
90: function(a,b,c,d){for(a=~~(a*2+.5),c=d=[];d<(b?b:5);c[d++]=a>=d*2?2:a&1?a--&1:0);return c}
86: function(a,b,c,d){for(a=a*2+.5,c=d=[];b?b>d:5>d;c[d++]=a>=d*2?2:a&1?a--&1:0);return c}
85: function(a,b,c,d){for(a=a*2+.5,c=d=[];d<(b||5);c[d++]=a>=d*2?2:a&1?a--&1:0);return c}
84: function(a,b,c,d){for(a+=a+.5,c=d=[];d<(b||5);c[d++]=a>=d*2?2:a&1?a--&1:0);return c}

@atk
Copy link

atk commented Oct 4, 2012

function(a,b,c,d){for(c=d=[];d<(b||5);c[d++]=a+.25>=d?2:a-d>-.75?1:0);return c} - 79 chars

@atk
Copy link

atk commented Oct 4, 2012

function(a,b,c,d){for(c=d=[];d<(b||5);c[d++]=+(a+.25>=d)+(a-d>-.75));return c} - 78 chars

@Hupotronic
Copy link
Author

Impressive!

@atk
Copy link

atk commented Oct 5, 2012

Thanks. I have to admit that I'm out of ideas how to make this any smaller.

@FarSeeing
Copy link

-1 byte, if you change a-d>-.75 into a+.75>d.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment