Repository navigation
#050 Empty String
Create a function that returns true if a string is empty and false otherwise.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/050 Empty String.js"Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.
This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.
//Create a function that returns true if a string is empty and false otherwise.
function isEmpty(s) {
return s.length === 0;
}
import * as Test from '../testHelper.js';
Test.assertEquals(isEmpty(""), true)
Test.assertEquals(isEmpty(" "), false)
Test.assertEquals(isEmpty(" "), false)
Test.assertEquals(isEmpty("38215"), false)
Test.assertEquals(isEmpty("afjabsdf"), false)
Test.assertEquals(isEmpty("!?@&"), false)Cross-language explanation preserved from the study materials
Reference file: #044 Empty String.txt. These older notes use a separate numbering scheme and may describe C# code, external diagrams or benchmark timings. They are reference material, not measured JavaScript performance. The current implementation and assertions above are authoritative.
# [Is the String Empty?](https://edabit.com/challenge/eTB4wZCdAykJ6n9pQ)
Published by [Helen Yu](https://edabit.com/user/mNMQvcxKSSvqqMYCH)
language_fundamentals strings validation
Create a function that returns `true` if a string is empty and `false` otherwise.
`isEmpty("") ➞ true`
`isEmpty(" ") ➞ false`
`isEmpty("a") ➞ false`
A string containing only whitespaces `" "` does **not** count as empty.
Don't forget to `return` the result.
### Time Complexity:
The time complexity of this function is _O(1)_. Checking the length of a string (`str.Length == 0`) is a constant-time operation, as the length property is stored and accessed in _O(1)_ time. Not using `Length`, mean manual iterating and this makes _O(n)_, unless we are using `IsNullOrEmpty`.
### Space Complexity:
The time complexity of this function is _O(1)_. The function does not require additional space that grows with input size; it only uses a constant amount of space for the parameters and return value.

//Create a function that returns true if a string is empty and false otherwise.
using BenchmarkDotNet.Attributes;
using System;
namespace Challenges
{
public class Program44
{
public static bool IsEmpty(string str) => string.IsNullOrEmpty(str);
}
public class BenchmarkProgram44
{
[Benchmark]
[Arguments("")]
[Arguments(" ")]
[Arguments(" ")]
[Arguments("38215")]
[Arguments("afjabsdf")]
[Arguments("!?@&")]
public bool IsEmpty(string str) => Program44.IsEmpty(str);
}
}
using System;
using Challenges.Edabit;
using NUnit.Framework;
namespace Tests
{
[TestFixture]
public class Tests44
{
[Test]
[TestCase("", true)]
[TestCase(" ", false)]
[TestCase(" ", false)]
[TestCase("38215", false)]
[TestCase("afjabsdf", false)]
[TestCase("!?@&", false)]
public void IsEmpty(string s, bool expectedResult)
{
bool result = Program44.IsEmpty(s);
Assert.That(result, Is.EqualTo(expectedResult));
}
}
}