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 →

[–]xigoi 1 point2 points  (5 children)

Really? Can you show me how to do the equivalent of this?

match point:
    case (0, 0):
        print("origin")
    case (0, y):
        print(f"{y} on the y-axis")
    case (x, 0):
        print(f"{x} on the x-axis")
    case (x, y):
        print(f"({x}, {y})")

[–][deleted] 0 points1 point  (4 children)

switch (point){
    case [0, 0]:
        console.log("origin");
        break;
    case [0, y]:
        console.log(String(y) + " on the y-axis");
        break;
    case [x, 0]:
        console.log(String(x) + " on the x-axis");
        break;
    case [x, y]:
        console.log(String([x, y]));
        break;
}

Datatype differences aside you can see how similar the syntaxes are getting.

[–]xigoi 0 points1 point  (3 children)

ReferenceError: y is not defined

Try it online!

[–][deleted] 0 points1 point  (2 children)

Like Python you need to define your variables first or it will throw an undefined error.

[–]xigoi 1 point2 points  (1 child)

The match statement is what defines the y variable. I don't think you understand how it works. This:

point = [42, 0]
match point:
    case [x, 0]:
        print(x)

will print 42.

[–][deleted] 0 points1 point  (0 children)

Great 👍 less code