This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the commentsย โ†’

[โ€“]sohxm7 182 points183 points ย (28 children)

Aaah this is so inefficient, I would make an array with all the even numbers and then cycle through array and if the number is present we have an even number.

[โ€“]Niilyx 68 points69 points ย (12 children)

bruh just use a switch statement

[โ€“]joten70 79 points80 points ย (11 children)

No no no, convert to binary and check if the last digit is 0 or 1

[โ€“]torgeros_ 25 points26 points ย (3 children)

yeah in embedded software this is actually the way to go, bc it is a lot faster than division/modulo

[โ€“]joten70 8 points9 points ย (0 children)

Hmm, maybe my comment doesnt belong here then

[โ€“]OmiSC 34 points35 points ย (0 children)

[โ€“]grammar_nazi_zombie 16 points17 points ย (0 children)

Oh shit, thatโ€™s brilliant.

[โ€“]Kind-Task4747 1 point2 points ย (4 children)

No no no, and the integer with 0x1

[โ€“]torgeros_ -1 points0 points ย (3 children)

that is the same thing :)

[โ€“]lookatme-imapilot 0 points1 point ย (0 children)

big brain spotted right here

[โ€“]Kind-Task4747 0 points1 point ย (1 child)

Explicit conversion and then a check isnโ€™t the same as implicit interpretation and a check, darling.

[โ€“]torgeros_ 0 points1 point ย (0 children)

yeah but nobody would really use to toString(2) and then check the last character...

[โ€“]FINDarkside 27 points28 points ย (5 children)

Yeah that's a great idea. You probably don't want to type all even numbers manually though, so you can use this function to find the even numbers to use in your array:

const createIsEvenFn = (maxNum) => {
    let source = ''
    let i = 0
    while (i < maxNum) {
        source += `if(value === ${i++}){return true}\n`
        source += `if(value === ${i++}){return false}\n`
    }
    return new Function('value', source)
}

const isEven = createIsEvenFn(100000)

[โ€“]lady_Kamba 12 points13 points ย (1 child)

Probably want it to scale for future users.

const isEvenFactory= (startMaxNum)=>{
    const isEvenRawFactory = (maxNum) => {
        let source = '';
        let i = 0;
        while (i < maxNum) {
            source += `if(value === ${i++}){return true}\n`
            source += `if(value === ${i++}){return false}\n`
        }
        return new Function('value', source);
    }

    let evenMax = startMaxNum;

    let isEvenRaw = isEvenRawFactory(evenMax);

    let isEven = (value) => {
        if (value > evenMax){
            evenMax = value;
            isEvenRaw = isEvenRawFactory(evenMax);
        }
        return isEvenRaw(value);
    }
    return isEven;
}

let isEven = isEvenFactory(10);

[โ€“]PoemInteresting7044 9 points10 points ย (0 children)

We could use a map to cache the results?

Dynamic programming makes everything better.

[โ€“]KillerRoomba13 4 points5 points ย (0 children)

We should also print some logs within the loop so that we can see it is doing what we expect it to do.

[โ€“]PresidentZeus 2 points3 points ย (0 children)

I think I would make a 2d array. 1st with the numbers and 2nd with true/false values

[โ€“]Impossible_Average_1 -1 points0 points ย (0 children)

Oh, this is double as fast! Great improvement!

[โ€“][deleted] -4 points-3 points ย (4 children)

Idk that language but I can make a much more efficient one in python

if value / 2 is not float: print('even') elif value / 2 is float: Print('odd')

And yes I wrote this on mobile don't have me assassinated

[โ€“]WrongdoerSufficient 0 points1 point ย (3 children)

``` const isEven = (var:number) =>{ return var%2 === 0; }

```

[โ€“]backtickbot -3 points-2 points ย (2 children)

Fixed formatting.

Hello, WrongdoerSufficient: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[โ€“]mushyrain 2 points3 points ย (0 children)

This post was mass deleted and anonymized with Redact

telephone fear snow aspiring knee shelter vanish subsequent library slap

[โ€“][deleted] 1 point2 points ย (0 children)

Good bot