Repository navigation
#058 Last Character
Create a function that takes a string (a random name).
If the last character of the name is an "n", return true, otherwise return false.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/058 Last Character.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 takes a string (a random name).
//If the last character of the name is an "n", return true, otherwise return false.
function isLastCharacterN(word) {
return word.charAt(word.length - 1).toLowerCase() === 'n';
}
import * as Test from '../testHelper.js';
Test.assertEquals(isLastCharacterN("Aiden"), true)
Test.assertEquals(isLastCharacterN("Roxy"), false)
Test.assertEquals(isLastCharacterN("Bert"), false)
Test.assertEquals(isLastCharacterN("Dean"), true)
Test.assertEquals(isLastCharacterN("Ian"), true)
Test.assertEquals(isLastCharacterN("Brian"), true)
Test.assertEquals(isLastCharacterN("Daniel"), false)Cross-language explanation preserved from the study materials
Reference file: #052 Last Character.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 Last Character an "N"?](https://edabit.com/challenge/GAr8KkJ7eMadxBDpF)
Published by [Nomore](https://edabit.com/user/p6ESyNjSyY6QbwMDW)
conditions regex strings validation
Create a function that takes a string (a random name). If the last character of the name is an "n", return `true`, otherwise return `false`.
`isLastCharacterN("Aiden") ➞ true`
`isLastCharacterN("Piet") ➞ false`
`isLastCharacterN("Bert") ➞ false`
`isLastCharacterN("Dean") ➞ true`
The function must return a boolean value ( i.e. `true` or `false`).
### Time Complexity:
Checking the last character of a string using EndsWith takes constant time _O(1)_, as it only looks at the final character without scanning the entire string. Although, manually iterating through the entire string `word` using a `foreach` loop is _O(n)_ but is also **400x** faster!
The surprising difference in benchmark results between EndsWith and ForeachLoop can be explained by the following factors:
1. **Built-In Method Overhead (**`EndsWith`**)**
* `EndsWith("n"`)` is a **high-level API** that provides additional functionality beyond just checking the last character. It supports:
* **Culture-specific comparisons** (e.g., ignoring case or diacritical marks in certain locales).
* Validation of the string input (e.g., ensuring it's not `null`).
* Handling multiple characters (e.g., `"str.EndsWith("suffix")"`).
* Even though it is optimized in .NET, this added flexibility introduces some overhead, as it internally delegates to more complex logic.
2. `Foreach Loop` **Simplicity**
* The manual method is an extremely **low-level implementation** that does one specific task:
* Iterates through the string.
* Stores the last character and checks if it is `'n'`.
* The simplicity of this method allows it to execute **much faster** because there’s no extra overhead from built-in optimizations, cultural checks, or extended functionality.
3. **.NET Runtime Optimizations**
* For ForeachLoop, the JIT (Just-In-Time) compiler likely **inlines** the method and eliminates unnecessary computations during optimization passes.
* The loop is simple enough for the compiler to **vectorize** or optimize heavily, reducing its runtime further.
Even better option is using index operator to simply chceck the last character. It is _O(1)_ and more than twice faster than loop approach!
### Space Complexity:
The time complexity of this function is _O(1)_. The function uses a constant amount of space for temporary storage and the return value.

Manual approach:

Index operrator:

//Create a function that takes a string (a random name).
//If the last character of the name is an "n", return true, otherwise return false.
using BenchmarkDotNet.Attributes;
using System;
namespace Challenges
{
public class Program52
{
public static bool IsLastCharacterN(string word) => word[^1] == 'n';
}
public class BenchmarkProgram52
{
[Benchmark]
[Arguments("Aiden")]
[Arguments("Roxy")]
[Arguments("Bert")]
[Arguments("Dean")]
[Arguments("Ian")]
[Arguments("Brian")]
[Arguments("Daniel")]
public bool IsLastCharacterN(string word) => Program52.IsLastCharacterN(word);
}
}
using Challenges.Edabit;
using Microsoft.VisualStudio.TestPlatform.TestHost;
using NUnit.Framework;
namespace Tests
{
[TestFixture]
public class Tests52
{
[Test]
[TestCase("Aiden", true)]
[TestCase("Roxy", false)]
[TestCase("Bert", false)]
[TestCase("Dean", true)]
[TestCase("Ian", true)]
[TestCase("Brian", true)]
[TestCase("Daniel", false)]
public void FixedTest(string a, bool expectedResult)
{
// Arrange
string word = a;
bool result = Program52.IsLastCharacterN(word);
Assert.That(result, Is.EqualTo(expectedResult));
}
}
}