Intermediate Algorithm Scripting: Missing letters - critique of my solution?

Intermediate Algorithm Scripting: Missing letters - critique of my solution?
0

#1

Hi all, as usual my answers aren’t as elegant as the intermediate (and even some of the basic) answers given in the hints section, but in this case I’d really appreciate some feedback as to why my answer isn’t just plain simpler than the ones given.

function fearNotLetter(str) {
  var alphabet = "abcdefghijklmnopqrstuvwxyz";
  if (alphabet.indexOf(str) >=0)  {
    return undefined;
  }
    for (let i = 1; i <= str.length; i++)  {
      if (alphabet.indexOf(str.substring(0,i)) == -1)  {
      return alphabet.charAt(alphabet.indexOf(str.charAt(0)) + i - 1);
    }
  }
}
fearNotLetter("abce");

Any ideas? Thanks.


#2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

Note: Backticks are not single quotes.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

First of all, for this challenge, the advanced solutions from “Get Hint” is only advanced in a sense that they use a bit more than the basic JS knowledge. IMO, the advanced solution listed in there is the worst solution out of all. So, don’t use them as a metric to judge what a good code looks like.
(Frankly, “advanced” is an ambiguous term to start with, advanced in what sense?)

Logic wise, your code isn’t complicated. You incrementally compare a portion of the given sequence to a expected sequence. If a term is missing in the given sequence, then it must be different from the expected sequence. So, it is fairly direct.

However, when compared to another candidate, yours simply can’t be simpler. I’ll compare yours with the Basic Solution (which is, in fact, the best one among the three)

Is your code shorter? Nope, it is longer. And, in general, longer code doesn’t make the code any simpler.
(But, don’t try to shorten your code by sacrificing readability)

Does your code read better? Not necessarily. The basic solution only uses the most common programming feature that any programmer can understand. Yours isn’t too bad but have you considered how bad this line reads?

return alphabet.charAt(alphabet.indexOf(str.charAt(0)) + i - 1)

Also, your code is guaranteed to be slower due to multiple calls to .indexOf() and .substring()

Your code is definitely simpler than the Advanced Solution, but I would still choose the Basic Solution anytime.


#4

Thanks for both of your replies. gunhoo93, I especially appreciated a couple of things you said. First that those “basic,” “intermediate,” and “advanced” spoilers aren’t necessarily as labeled. The idea that they were has been demoralizing. Second, I agree that the line of mine you referenced was awful to read. I tend to declare a bunch of variables and use them in my code. But because everyone seems to be so obsessed with line counts, I’ve been replacing variable declarations within the code with their underlying values. Trust me, the line you referenced made a lot more sense when it used variable names.

Finally, is there some reference to figure out which methods/etc are slower than others? I use Repl.it for testing code, if that helps. By way of further example, here’s what I did with the next challenge (“Convert HTML Entities”). My way makes more sense to me than the others, but if it looks like it’s a lot slower, I’d like to be able to test that. And now I’ll try to incorporate kevinSmith’s suggestion:

function convertHTML(str) {
  const arr = [["& ","&amp; "],['>','&gt;'],['<','&lt;'],["'",'&apos;'],['"','&quot;']];
  for (let i=0; i<arr.length;i++)  {
    let a = new RegExp(arr[i][0],"g");
    str = str.replace(a,arr[i][1]);
  }
return str;
}


#5

This thread is diverting from the original question posted. If you are going to continue with this discussion, I will move it to it’s own topic.


#6

Measuring performance accurately is too complex that you’d need a profiler. For roughly measuring performance, you can use online benchmarking tools. However, you should avoid unnecessary benchmarking.

To roughly guess a performance of a method without measuring, you can guess the algorithm for the function or read its source code. In general, things like ‘indexOf()’ is treated equally as one iteration over an array. substring() involves extending string multiple times. So, using them multiple times is clearly more involved than using a single loop.

Line count isn’t a good metric to measure the code’s simplicity, but, in general, legitimately shorter codes are less involved and reads faster than longer ones. However, reading one or two more lines doesn’t harm readability.


#7

Thanks. It’d be nice to see that built into FCC – I’m about to write a post about this issue as it relates to another challenge.


#8

Thanks. You’re right – I’ll start a new post.