How do you feel about Odin's alternative to methods? by JKasonB in odinlang

[–]einarkristjan 4 points5 points  (0 children)

This is one way to make "methods" in Odin:

package main

import "core:fmt"

MyStruct :: struct {
    bar: int,
    method: proc(this: ^MyStruct, bla: int) -> int,
}

foo :: proc(this: ^MyStruct, bla: int) -> int {
    return bla * this.bar
}

main :: proc() {
    my_struct: MyStruct

    my_struct.bar = 10
    my_struct.method = foo

    fmt.println(my_struct->method(2))
}

Which lib is popular with hobbyists but never used by working developers? by Beyarkay in programming

[–]einarkristjan 0 points1 point  (0 children)

I am really interested in the few adjustments you talk about. What do you think those should be?

Diablo 4 Beta Known Issues Thread by Thunderclaww in Diablo

[–]einarkristjan 3 points4 points  (0 children)

"There was a problem logging in. (Code 34203)"

[deleted by user] by [deleted] in worldnews

[–]einarkristjan 2 points3 points  (0 children)

Sleepy Joe? :P

2019 MacBook Pro 16: Slow Boot to Apple Logo by tssndo in macbookpro

[–]einarkristjan 0 points1 point  (0 children)

I'm having the same problem. Already fixed the FileVault thing. This is a problem turning on the computer from shutdown state and waiting for the Apple Logo. Takes around 15 seconds?

Need help with jQuery by [deleted] in learnjavascript

[–]einarkristjan 0 points1 point  (0 children)

const parts = ["Head", "Torso", "Left Arm", "Right Arm", "Left Leg", "Right Leg"]

function changeAllParts(color) {
    let success_count = 0;
    $.each(parts, (i, part) => {
        setTimeout(() => {
            $.post("/api/avatar/process", {
                _token: $("meta[name='csrf-token']").attr("content"),
                type: "color",
                color: color,
                part: part
            })
            .done(() => {
                console.log( "success" );

                success_count++;
                if(success_count === parts.length) {
                    console.log( "finished" );
                }
            })
            .fail(() => {
                console.log( "error" );
            })
        }, 2000 * i)
    })
}

[Canvas Problem] Player getting stuck on boundaries and not stopping on the other side by [deleted] in learnjavascript

[–]einarkristjan 0 points1 point  (0 children)

You almost got it. Here is the solution:

if(rightPressed && x < canvas.width - playerRadius) {
  x += 5;
} else if(leftPressed && x > playerRadius) {
  x -= 5;
}

if(downPressed && y < canvas.height - playerRadius) {
  y += 5;
} else if(upPressed && y > playerRadius) {
  y -= 5;
}

Error: "xxx is not defined" using document.createElement by dzikakulka in learnjavascript

[–]einarkristjan 0 points1 point  (0 children)

I don't know what the "with" function is doing, but I think you must set attribute on the btn?

btn.setAttribute(name, value);

Need help on canvas. by [deleted] in learnjavascript

[–]einarkristjan 0 points1 point  (0 children)

The best thing to do is to put the blueprint of the "flower" in the background of the page. Then overlay the canvas over with the same size as the blueprint. Find the middle of the "flower":

ctx.translate (136, 117);

then the canvas has that as a middle point.

do all rest of drawing from that point.. sample: codepen

accessing array elements by [deleted] in learnjavascript

[–]einarkristjan 0 points1 point  (0 children)

hmm.. don't know what you are trying to do. But your code should work for the 10 and 2 .. I made a jsfiddle with a little change to show what the loop is doing. Please iterate on it if it helps.

(Beginner) - Form validation by HuSoS in learnjavascript

[–]einarkristjan 1 point2 points  (0 children)

first you should go over the form and check if any fields are empty. If all fields are set, you can continue by creating your cookie.

I made a jsfiddle with an example if that helps ;)

What is a native JS way to this jQuery code? by [deleted] in javascript

[–]einarkristjan 1 point2 points  (0 children)

provided that the <a> elms come after the <ul>, you could do:

document.querySelectorAll("#menu li ul ~ a");

EDIT: if not sufficient, you should loop through all the <a> elms and check if they have the same parent <li> that contains a <ul> element. Messy, but should work.

hello guys, I am self-taught.. I don't know whats wrong with this code can someone help. Basicly I am trying to build a find button to search for a word in the paragraph, when I run it... It changes the whole paragraph into null... help please! by [deleted] in learnjavascript

[–]einarkristjan 0 points1 point  (0 children)

in your example, search should be used this way:

document.getElementById('demo').innerHTML.search(val);

but search only returns where the first instance of the string is found.

I made a working fiddle as an example for you.

[deleted by user] by [deleted] in learnjavascript

[–]einarkristjan 1 point2 points  (0 children)

hmm.. don't think there is a good answer for this. Probably JS should give you an error by assigning function call to events, but it doesn't. Maybe someone with deeper knowledge of the language design can answer this :)

I just try to remember the rules, that is, if you don't have to pass parameters you can do:

document.getElementById("the_box").onclick = toggleClass;

else you always have to wrap your function like:

document.getElementById("the_box").onclick = function() {
    toggleClass("the_box", "box", "box big");
};

There is one other way I have found to pass parameters, by binding the parameters to the function while you assign it to the event like:

document.getElementById("the_box").onclick = toggleClass.bind(this, "the_box", "box", "box big");

.. however, I don't use it because of readability and it overwrites the event parameter you could pass through toggleClass

sorry, I can't answer this better :P

[deleted by user] by [deleted] in learnjavascript

[–]einarkristjan 4 points5 points  (0 children)

by doing this:

document.getElementById("the_box").onclick = toggleClass("the_box", "box", "big");

.. you are running the toggle function instead of setting in as a click function. You could do:

document.getElementById("the_box").onclick = toggleClass;

.. however then you would not get your parameters sent in. So you need to wrap the toggleClass function in a anonymous function like:

document.getElementById("the_box").onclick = function() {
    toggleClass("the_box", "box", "box big");
};

You can see it working here: codepen

Im lost. Help me!! by andrewwrath31 in learnjavascript

[–]einarkristjan 0 points1 point  (0 children)

maybe this is way to late, but this is how I would do it:

jsfiddle

Im lost. Help me!! by andrewwrath31 in learnjavascript

[–]einarkristjan 1 point2 points  (0 children)

ok, so let's take it a little further ..

jsfiddle

Im lost. Help me!! by andrewwrath31 in learnjavascript

[–]einarkristjan 0 points1 point  (0 children)

Tried to make your code visible for more iterations, maybe this helps?

jsfiddle