3

Here is the task at hand:

Write a function called charAt which accepts a string and an index (number) and returns the character at that index.

The function should return an empty string if the number is greater than the length of the string.

The kicker is that you CAN NOT use the built in charAt method.

Am I doing what it is asking correctly aside from not including the if statement? Also, what would a correct implementation of that look like? (New to JS so my apologies in advance).

function charAt(string, index) {
  var charAt = string[index];
  return charAt;
}
Community
  • 1
  • 1
  • Does it fulfill the requirement? – Dave Newton Dec 28 '19 at 04:57
  • Did you try running the code? Wouldn't that answer your first question? As for a correct approach, there a different variables and schools of thought as to what would be correct. Some code doesn't produce the proper output, some code is less optimized for space/time complexity, and some code is less readable. What are you looking to accomplish? – Matthew Moran Dec 28 '19 at 04:58
  • Simply using `[index]` instead of `.charAt(index)` seems too simple to be the correct answer, but who knows? Only the person who wrote that task would know if that's an acceptable solution to them. – JLRishe Dec 28 '19 at 04:58
  • You can refer may help to you [link]( https://stackoverflow.com/questions/5943726/string-charatx-or-stringx) – Shusang Dec 28 '19 at 05:02

1 Answers1

7

It looks mostly fine, except for one issue - there are a number of odd characters (those composed of surrogate pairs, also sometimes called multibyte characters) which take up more than a single index in a string. An example is . If the string contains a character like this, it will be counted as two indicies in the string:

function charAt(string, index) {
  var charAt = string[index];
  return charAt;
}
console.log(
  charAt('foobar', 3), // Broken character, wrong
  charAt('foobar', 4), // Broken character, wrong
  charAt('foobar', 5), // Wrong character (should be "a", not "b")
  charAt('foobar', 6), // Wrong character (should be "r", not "a")
);

If this is a possible problem for your situation, consider using Array.from to turn it into an array first:

function charAt(string, index) {
  var charAt = Array.from(string)[index];
  return charAt;
}
console.log(
  charAt('foobar', 3),
  charAt('foobar', 4),
  charAt('foobar', 5),
  charAt('foobar', 6),
);

Or, with the empty string being returned when the index doesn't exist:

function charAt(string, index) {
  return Array.from(string)[index] || '';
}
console.log(
  charAt('foobar', 3),
  charAt('foobar', 4),
  charAt('foobar', 5),
  charAt('foobar', 6),
);
console.log(charAt('foobar', 123));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • 1
    “a certain few odd characters” — you mean all the characters between **U+10000** and **U+10FFFF**? – chharvey Dec 28 '19 at 05:01
  • Upvoted, would be worth mentionning somewhere "multibyte" character sets, for the sake of getting more people landing here with that topic in mind. – axelduch Dec 28 '19 at 05:02
  • Yeah, there are actually a bunch of them, but (at least in my experience) they're rarely encountered – CertainPerformance Dec 28 '19 at 05:02
  • The entire emoji table is in there, so it's getting more and more used (iirc they are in the user defined blocks? I forgot, would need to read about it again). – ASDFGerte Dec 28 '19 at 05:05
  • Well it depends where you are from, a lot of languages mostly only use characters in that set (any cyrillic based language for instance) – axelduch Dec 28 '19 at 05:05
  • @CertainPerformance you're right. *most* of the characters we encounter are between U+0 and U+7F, and *a lot* of them are between U+0 and U+FFFF. By the UTF-16 encoding algorithm, the characters between U+10000 and U+10FFFF (exactly 16 times as many as the ones between U+0 and U+FFFF) take up 2 indices in the string. – chharvey Dec 28 '19 at 05:12