Create Hello World Function - LeetCode: 30 Days of JavaScript
Table of contents
I've been eager to improve my skills in Javascript by solving code challenges. So, I came across "30 Days of JavaScript" on LeetCode, and decided to try it. I have also decided to use this platform to document my journey and also share my solutions.
So let's get to the first challenge:
Write a function createHelloWorld
. It should return a new function that always returns "Hello World"
.
Problem breakdown
We are expected to:
Write a function called
createHelloWorld
.The
createHelloWorld
should return a new function that always returns the string"Hello World"
.
My solution
//Write a function createHelloWorld. It should return a new function that always returns "Hello World".
var createHelloWorld = function() {
return function() {
return 'Hello World'
}
};
/**
* const result = createHelloWorld();
* console.log(result()); // "Hello World"
*/
In the code above, I declared a variable called createHelloWorld
and assigned it an anonymous function that does not take any parameter.
The function createHelloWorld
returns an inner anonymous function which, when called returns the string "Hello World"
.
The createHelloWorld
is stored in a variable called result
. When you,console.log(result())
it returns the string "Hello World"
.
Conclusion
This article shows how I solved the first Leetcode challenge in the "30 Days of JavaScript" series. I am excited to share my journey and look forward to documenting the next challenge.
Feel free to share how you would have solved it differently.
Subscribe to my newsletter
Read articles from Susan Odii directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Susan Odii
Susan Odii
Susan is a Frontend Web Developer. She is passionate about creating beautiful and engaging UI with good user experience. Susan is also a technical writer who writes on Frontend technologies to help other developers get a better understanding of a concept.