MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/dt0j0k/deleted_by_user/f6ukxt4/?context=3
r/badcode • u/[deleted]
[deleted] — view deleted post
18 comments sorted by
View all comments
2
Every code challenges have one random entry. This one worked on one test case.
const convert_to_int = (value) => {return (value.match(/g/g) || []).length * 10000+ (value.match(/s/g) || []).length * 100 +(value.match(/c/g) || []).length ;}const int_to_wow = (value) => {const letters = ['g', 's', 'c'];let now = '';let tries = 0;while (true) {now += letters[Math.floor(Math.random()*letters.length)];const count = convert_to_int(now);if (count === value) {break;}if (count > value) {now = now.split('').sort(() => .5 - Math.random()).slice(0, -1).join('');}
if (count === value) {break;}tries++;if (tries === 500) {now = '';}}return `${(now.match(/g/g) || []).length}g${(now.match(/s/g) || []).length}s${(now.match(/c/g) || []).length}c` ;};
View all comments
2
u/jsforeveryone Nov 07 '19
Every code challenges have one random entry. This one worked on one test case.
const convert_to_int = (value) => {
return (value.match(/g/g) || []).length * 10000
+ (value.match(/s/g) || []).length * 100 +
(value.match(/c/g) || []).length ;
}
const int_to_wow = (value) => {
const letters = ['g', 's', 'c'];
let now = '';
let tries = 0;
while (true) {
now += letters[Math.floor(Math.random()*letters.length)];
const count = convert_to_int(now);
if (count === value) {
break;
}
if (count > value) {
now = now.split('').sort(() => .5 - Math.random()).slice(0, -1).join('');
}
if (count === value) {
break;
}
tries++;
if (tries === 500) {
now = '';
}
}
return `${(now.match(/g/g) || []).length}g
${(now.match(/s/g) || []).length}s
${(now.match(/c/g) || []).length}c` ;
};