Skip to content
All posts
  • #leetcode
  • #javascript
  • #computerscience
  • #beginners

LeetCode 2667. Create Hello World Function (Easy)

Could you solve the last LeetCode problem? ๐Ÿค“ Here's another one, the simplest of the simplest. But...

Julia ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป GDEJulia ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป GDE1 min read50
LeetCode 2667. Create Hello World Function (Easy)

Could you solve the last LeetCode problem? ๐Ÿค“ Hereโ€™s another one, the simplest of the simplest. But hey, we all have to start somewhere.

Starting point

Write a function createHelloWorld. It should return a new function that always returns "Hello World".

My Submission

Letโ€™s take a look at my code. Yours maybe looks different, and thatโ€™s okay. Everyone has their own approach.

var createHelloWorld = function() {
     return function() {
        return "Hello World"
     }
 }

What happens here?

var createHelloWorld = function() {

 }

What was given by LeetCode was the outer declaration, the initialization of var createHelloWorld, which was assigned a function.

๐Ÿ‘ป Note: I personally never use var when declaring a variable, I always opt for let or const, but since this was the default, Iโ€™ll keep it that way (thereโ€™s nothing really wrong with using var).

Return a function

In the description it is said that we should return a function, which I did by writing

return function() {

}

Always return โ€œHello Worldโ€

By adding

return "Hello World"

inside the function, the string "Hello World" will be returned, no matter which argument the function may get.


In general, I am bad at explaining technical stuff. So any advice is welcome to improve my explanation skills ๐Ÿ™๐Ÿฝ.

Enjoyed this post?

Join the discussion on dev.to (0 comments) or share it.

Keep reading

4 min read

Red Team vs Blue Team โ€” I Choose You!

Itโ€™s 1999. You slide a Pokรฉmon Red Version cartridge into your Game Boy. Four AA batteries, a...

  • #cybersecurity
  • #programming
  • #webdev
Read