DEV Community

Cover image for Does it work?

Does it work?

There is always a monkey.
Originally published at ・3 min read

Let's have some fun with theory. Just look at the five following codes (test them only after playing the game). Does it work or not?

To see the answer, click on the collapsible "answer".

HTML quotes or not?

Here is the most basic HTML markup for a link:

<a href=>go to</a>
Enter fullscreen mode Exit fullscreen mode

It seems there are no quotes on the href attribute, but does the link work?

Yes, it does work. It's not even an HTML error. Besides, HTML minifyers strip that to save bytes.

N.B.: I'm not saying you should not add quotes. I think it's less readable.

Php switch

Look at the following PHP code:

$number = 73;
case 73.0:
    echo "héhé";
case 73:
    echo " I'm not surprised";
    echo "what?!!!";
Enter fullscreen mode Exit fullscreen mode

I say you get "héhé I'm not surprised" with that. Am I right? Does the code even work without throwing any errors?

Yes, it works. The first case matches and because there's no break the second case triggers.

Ruby conditions

I want to execute some code under specific conditions. I've found something in the documentation. It's called unless:

Enter fullscreen mode Exit fullscreen mode

Will this work like I want?

No, it won't. Here the unless condition excutes the code only if the condition is false!

Python comprehension

Python has a pretty cool way to handle lists called "List Comprehension". Instead of using a for loop that can be annoying to write, you can use a special syntax for better readability.

For example, let's say we want to put the following text that someone had better say in a list:

Kesher = "This is the girl"
k = [i.lower() for i in Kesher]
Enter fullscreen mode Exit fullscreen mode

Kesher is a string, and we put each character, including spaces, in the list using just a line of code. We also apply the lower function to each element at the same time.

Looks neat, but is that possible? Only one line of code?

Yes. You get ['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 'g', 'i', 'r', 'l'].

JavaScript evaluation

They say JavaScript operators short-circuit. What does it mean?

When the first operand of your condition is true, the engine skips the remaining operands. Reciprocally, if it's false, the engine will continue evaluating.

Let's keep it simple. && and || are standard logical operators. They respectively mean "and" and "or".

Instead of using if-else statements, you can leverage the benefits of short-circuit evaluation. For example:

const responseObject = { success: false };
!responseObject.success && console.error('oooops!');
Enter fullscreen mode Exit fullscreen mode

Now, look at this stupid example:

let apple = "m1", 
    chip = null,
    battle = undefined,
    winner = chip || intel || battle || apple;

Enter fullscreen mode Exit fullscreen mode

Does it work? Do I get my m1?

Yes, it works, and I get what I want. It fallbacks to apple.

Wrap up

I hope you enjoyed those questions, but I lied a little bit. It's not only for fun.
It is the kind of questions you might get during job interviews.

Photo by Sigmund on Unsplash

Discussion (0)