Does it work?

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>

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

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

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?!!!";

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

Answer 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:


Will this work like I want?

Answer 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]

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?

Answer 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!');

Now, look at this stupid example:

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

Does it work? Do I get my m1?

Answer 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