Tips to Make My Code More Efficient

Hi all,
I joined free code camp almost two months ago and I have reached the algorithm scripting challenges which I enjoy very much. Usually, after I finish one of those challenges, I compare my code with solutions from other campers and I noticed that my code is much more bulkier which I believe makes it less efficient. I have frequently tried to clean my code but I don’t seem to be going far with it. I wanted to get some tips on how I can improve my coding.
Below is one of the challenges that made me realize how inefficient my code is (I used two for loops for this!). It is the Missing Letters challenge in the intermediate algorithm scripting section.

function fearNotLetter(str) {
  var arr = [];
  var missing;
  for (var i = 0; i<str.length; i++){
    arr.push(str.charCodeAt(i));
  } 
  missing = arr[0];
  for (var j = 1; j<arr.length; j++){
    if (arr[j]==missing +1){
      missing++;
    }else if (missing+1 !== arr[j]){
      missing= missing+1;
      return String.fromCharCode(missing);
    }
  }
  return;
}

fearNotLetter("abce");

“Bulkier” does not always mean less efficient. Sometimes it could mean more readable. However, you could make your solution more efficient if you ask yourself a couple of questions:

  1. Do I need even need arrays to solve this problem?

  2. If your answer to #1 above is NO, can you figure out how just use one for loop?

1 Like

@Tobel158

I’d also add that you want to make your code as universal and concise as possible. Don’t jump through hoops if you don’t need to, other than that, congratulations on your hard work and speedy progress.

1 Like

@rmdawson71 @njanne19 I took both your tips into consideration and made the following modifications. I didn’t need the array and I also removed one of the for loops. Can you see anything else I can do?


function fearNotLetter(str) {
  
  var missing = str.charCodeAt(0);
  for (var j = 1; j < str.length; j ++){
    if (str.charCodeAt(j)== missing + 1){
      missing++;
    }else if (missing + 1 !== str.charCodeAt[j]){
      missing += 1;
      return String.fromCharCode(missing);
    }
  }
  return;
}

fearNotLetter("abce");

You should strive to make code as easy as possible to read for someone else. For example, I think if you did something like this it might be easier to read:

function fearNotLetter(str) {
  var missing, j, current, expected;
  missing = str.charCodeAt(0);
  for (j = 1; j < str.length; j++, missing++){
    current = str.charCodeAt(j);
    expected = missing + 1;
    if (current !== expected) {
      return String.fromCharCode(expected);
    }
  }
}

fearNotLetter("abce");

@codyseibert You see I did not know I could declare multiple variables like that. That is very helpful to know thank you. However, don’t you think creating the current and expected variables is a bit repetitive? Aren’t we better of by just sticking in their actual value?

The following is concise:

function fearNotLetter(str) {
  for (var i=0; i<str.length; i++) {
    if (str.charCodeAt(i+1) - str.charCodeAt(i) > 1) 
        return String.fromCharCode(str.charCodeAt(i)+1); 
  }
}

but the following is more readable:

function fearNotLetter(str) {
  for (var i=0; i<str.length; i++) {
    var currChar = str.charCodeAt(i);
    var nextChar = str.charCodeAt(i+1);
    if (nextChar - currChar > 1) 
        return String.fromCharCode(currChar+1); 
  }
}

Well, anytime you have a statement which is duplicated, you should always abstract it out into a variable. For example, you had the following declared in two places in your function:

str.charCodeAt(j)

It is a better practice to DRY (don’t repeat yourself) up your code if you notice any duplicates.

Note, your program will use more memory and take longer the more lines and variables you declare, but that performance is so small (we are talking nano seconds), that making the code easier to read is higher priority in all cases when possible.

That is really helpful, Thank you very much. I feel like i am getting a better understanding of how to write readable as well as concise code now.

Making it readable takes precedence over conciseness. Got it. Thanks.