Repository navigation
#062 Check String for Spaces
Create a function that returns true if a string contains any spaces.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/062 Check String for Spaces.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 contains any spaces.
function hasSpaces(str) {
return str.includes(" ");
}
import * as Test from '../testHelper.js';
Test.assertEquals(hasSpaces("Foo"), false)
Test.assertEquals(hasSpaces("Foo bar"), true)
Test.assertEquals(hasSpaces("Foo "), true)
Test.assertEquals(hasSpaces(" Foo"), true)
Test.assertEquals(hasSpaces(" "), true)
Test.assertEquals(hasSpaces(""), false)
Test.assertEquals(hasSpaces(",./;'[]-="), false)Cross-language explanation preserved from the study materials
Reference file: #056 Check String for Spaces.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.
# [Check String for Spaces](https://edabit.com/challenge/RhjB3ckgMXiW5xdTB)
Published by [Pickle](https://edabit.com/user/zzaD2EtTFR7PK52kY)
strings validation
Create a function that returns `true` if a string contains any spaces.
`HasSpaces("hello") ➞ false`
`HasSpaces("hello, world") ➞ true`
`HasSpaces(" ") ➞ true`
`HasSpaces("") ➞ false`
`HasSpaces(",./!@#") ➞ false`
An empty string does not contain any spaces.
Try doing this without RegEx.
### Time Complexity:
The time complexity is _O(n)_, where 𝑛 is the length of the string `str`. In the worst case, the function may need to check every character (e.g., if there are no spaces). In the best case (if the first character is a space), it returns immediately.
### Space Complexity:
The space complexity is _O(1)_ because the function only uses a fixed amount of space for a few variables, regardless of the length of the input string.

//Create a function that returns true if a string contains any spaces.
using BenchmarkDotNet.Attributes;
using System;
namespace Challenges
{
public class Program56
{
public static bool HasSpaces(string str)
{
foreach (char c in str)
{
if (c == ' ')
{
return true;
}
}
return false;
}
}
public class BenchmarkProgram56
{
[Benchmark]
[Arguments("Foo")]
[Arguments("Foo bar")]
[Arguments("Foo ")]
[Arguments(" Foo")]
[Arguments(" ")]
[Arguments("")]
[Arguments(",./;'[]-=")]
public bool HasSpaces(string str) => Program56.HasSpaces(str);
}
}
using System;
using Challenges.Edabit;
using NUnit.Framework;
namespace Tests
{
[TestFixture]
public class Tests56
{
[Test]
[TestCase("Foo", false)]
[TestCase("Foo bar", true)]
[TestCase("Foo ", true)]
[TestCase(" Foo", true)]
[TestCase(" ", true)]
[TestCase("", false)]
[TestCase(",./;'[]-=", false)]
public void FixedTest(string str, bool expectedResult)
{
bool result = Program56.HasSpaces(str);
Assert.That(result, Is.EqualTo(expectedResult));
}
}
}