Skip to main content
Code Review

Return to Answer

included runnable test script
Source Link
mickmackusa
  • 8.8k
  • 1
  • 17
  • 31

Due to not being as well across .js likeas others here are, I'll post a humble forfor loop.

It does make 3 function calls per iteration, but they aren't heavy ones, there is at most only one pass through the string, and early-return programming is in effect.


After Rotora's challenge, I was doubting myself, so I whacked this little battery of tests together:

function MickMacKusa(string) {
 for (let char, pos, i = 0; i < string.length; ++i) {
 char = string.charAt(i);
 pos = string.indexOf(char);
 if (pos == i && string.indexOf(char, i + 1) == -1) {
 return pos;
 } 
 }
 return -1;
}
function RoToRa(string) {
 for (let char, pos, i = 0; i < string.length; ++i) {
 char = string.charAt(i);
 if (string.indexOf(char, i + 1) == -1) {
 return i;
 } 
 }
 return -1;
}
let table = document.getElementById("test"),
 row;
for (let i = 1; i < table.rows.length; ++i) {
 row = table.rows[i];
 row.cells[3].innerHTML = MickMacKusa(row.cells[0].innerHTML);
 row.cells[4].innerHTML = RoToRa(row.cells[0].innerHTML);
}
<table id="test" border="1" cellpadding="4">
<tr><th>Input</th><th>Letter</th><th>Index</th><th>MickMacKusa</th><th>RoTora</th></tr>
<tr><td>abccbcba</td> <td>-</td> <td>-1</td> <td></td> <td></td></tr>
<tr><td>abcbebc</td> <td>a</td> <td> 0</td> <td></td> <td></td></tr>
<tr><td>abc</td> <td>a</td> <td> 0</td> <td></td> <td></td></tr>
<tr><td>aabbc</td> <td>c</td> <td> 4</td> <td></td> <td></td></tr>
<tr><td>abcba</td> <td>c</td> <td> 2</td> <td></td> <td></td></tr>
<tr><td>abba</td> <td>-</td> <td>-1</td> <td></td> <td></td></tr>
<tr><td>abaa</td> <td>b</td> <td> 1</td> <td></td> <td></td></tr>
<tr><td>aabcbcbca</td> <td>-</td> <td>-1</td> <td></td> <td></td></tr>
</table>

Due to not being as well across .js like others here are, I'll post a humble for loop.

It does make 3 function calls per iteration, but they aren't heavy ones, there is only one pass through the string, and early-return programming is in effect.

Due to not being as well across .js as others here are, I'll post a humble for loop.

It does make 3 function calls per iteration, but they aren't heavy ones, there is at most only one pass through the string, and early-return programming is in effect.


After Rotora's challenge, I was doubting myself, so I whacked this little battery of tests together:

function MickMacKusa(string) {
 for (let char, pos, i = 0; i < string.length; ++i) {
 char = string.charAt(i);
 pos = string.indexOf(char);
 if (pos == i && string.indexOf(char, i + 1) == -1) {
 return pos;
 } 
 }
 return -1;
}
function RoToRa(string) {
 for (let char, pos, i = 0; i < string.length; ++i) {
 char = string.charAt(i);
 if (string.indexOf(char, i + 1) == -1) {
 return i;
 } 
 }
 return -1;
}
let table = document.getElementById("test"),
 row;
for (let i = 1; i < table.rows.length; ++i) {
 row = table.rows[i];
 row.cells[3].innerHTML = MickMacKusa(row.cells[0].innerHTML);
 row.cells[4].innerHTML = RoToRa(row.cells[0].innerHTML);
}
<table id="test" border="1" cellpadding="4">
<tr><th>Input</th><th>Letter</th><th>Index</th><th>MickMacKusa</th><th>RoTora</th></tr>
<tr><td>abccbcba</td> <td>-</td> <td>-1</td> <td></td> <td></td></tr>
<tr><td>abcbebc</td> <td>a</td> <td> 0</td> <td></td> <td></td></tr>
<tr><td>abc</td> <td>a</td> <td> 0</td> <td></td> <td></td></tr>
<tr><td>aabbc</td> <td>c</td> <td> 4</td> <td></td> <td></td></tr>
<tr><td>abcba</td> <td>c</td> <td> 2</td> <td></td> <td></td></tr>
<tr><td>abba</td> <td>-</td> <td>-1</td> <td></td> <td></td></tr>
<tr><td>abaa</td> <td>b</td> <td> 1</td> <td></td> <td></td></tr>
<tr><td>aabcbcbca</td> <td>-</td> <td>-1</td> <td></td> <td></td></tr>
</table>

added 86 characters in body
Source Link
mickmackusa
  • 8.8k
  • 1
  • 17
  • 31

Your solution looks rather map-happy.

It is important to remember for this task that counting higher than 2 of any encountered letter is needless processing as is any processing on any letter after the earliest positioned unique letter.

Also, doing a complete sweep of the input string may be ill-advised if the input string is of considerable length.

Due to not being as well across .js like others here are, I'll post a humble for loop.

function firstNonRepeatedCharacterPosition(string) {
  for (let char, pos, i = 0; i < string.length; ++i) {
    char = string.charAt(i);
    pos = string.indexOf(char);
    if (pos == i && string.indexOf(char, i + 1) == -1) {
      return pos;
    } 
  }
  return -1;
}
console.log(firstNonRepeatedCharacterPosition('abcbebc'));

It does make 3 function calls per iteration, but they aren't heavy ones, there is only one pass through the string, and early-return programming is in effect.

  • Grab the letter at the incremented position.
  • Find the earliest occurrence of that letter.
  • Check if there is a later occurrence of the same letter.

The later the unique letter exists (or if there are no unique letters), the more laborious my function is. On the other hand, if the first letter is unique, then you are finished in just 3 function calls.

p.s. I lack the knowledge to interpret your js code, so I cannot review it beyond saying that it isn't very novice friendly.

Your solution looks rather map-happy.

It is important to remember for this task that counting higher than 2 of any encountered is needless processing.

Also, doing a complete sweep of the input string may be ill-advised if the input string is of considerable length.

Due to not being as well across .js like others here are, I'll post a humble for loop.

function firstNonRepeatedCharacterPosition(string) {
  for (let char, pos, i = 0; i < string.length; ++i) {
    char = string.charAt(i);
    pos = string.indexOf(char);
    if (pos == i && string.indexOf(char, i + 1) == -1) {
      return pos;
    } 
  }
  return -1;
}
console.log(firstNonRepeatedCharacterPosition('abcbebc'));

It does make 3 function calls per iteration, but they aren't heavy ones, there is only one pass through the string, and early-return programming is in effect.

  • Grab the letter at the incremented position.
  • Find the earliest occurrence of that letter.
  • Check if there is a later occurrence of the same letter.

The later the unique letter exists (or if there are no unique letters), the more laborious my function is. On the other hand, if the first letter is unique, then you are finished in just 3 function calls.

p.s. I lack the knowledge to interpret your js code, so I cannot review it beyond saying that it isn't very novice friendly.

Your solution looks rather map-happy.

It is important to remember for this task that counting higher than 2 of any encountered letter is needless processing as is any processing on any letter after the earliest positioned unique letter.

Also, doing a complete sweep of the input string may be ill-advised if the input string is of considerable length.

Due to not being as well across .js like others here are, I'll post a humble for loop.

function firstNonRepeatedCharacterPosition(string) {
  for (let char, pos, i = 0; i < string.length; ++i) {
    char = string.charAt(i);
    pos = string.indexOf(char);
    if (pos == i && string.indexOf(char, i + 1) == -1) {
      return pos;
    } 
  }
  return -1;
}
console.log(firstNonRepeatedCharacterPosition('abcbebc'));

It does make 3 function calls per iteration, but they aren't heavy ones, there is only one pass through the string, and early-return programming is in effect.

  • Grab the letter at the incremented position.
  • Find the earliest occurrence of that letter.
  • Check if there is a later occurrence of the same letter.

The later the unique letter exists (or if there are no unique letters), the more laborious my function is. On the other hand, if the first letter is unique, then you are finished in just 3 function calls.

p.s. I lack the knowledge to interpret your js code, so I cannot review it beyond saying that it isn't very novice friendly.

A couple more thoughts
Source Link
mickmackusa
  • 8.8k
  • 1
  • 17
  • 31

Your solution looks rather map-happy.

It is important to remember for this task that counting higher than 2 of any encountered is needless processing.

Also, doing a complete sweep of the input string may be ill-advised if the input string is of considerable length.

Due to not being as well across .js like others here are, I'll post a humble for loop.

function firstNonRepeatedCharacterPosition(string) {
  for (let char, pos, i = 0; i < string.length; ++i) {
    char = string.charAt(i);
    pos = string.indexOf(char);
    if (pos == i && string.indexOf(char, i + 1) == -1) {
      return pos;
    } 
  }
  return -1;
}
console.log(firstNonRepeatedCharacterPosition('abcbebc'));

It does make 3 function calls per iteration, but they aren't heavy ones, there is only one pass through the string, and early-return programming is in effect.

  • Grab the letter at the incremented position.
  • Find the earliest occurrence of that letter.
  • Check if there is a later occurrence of the same letter.

The later the unique letter exists (or if there are no unique letters), the more laborious my function is. On the other hand, if the first letter is unique, then you are finished in just 3 function calls.

p.s. I lack the knowledge to interpret your js code, so I cannot review it beyond saying that it isn't very novice friendly.

Your solution looks rather map-happy.

Due to not being as well across .js like others here are, I'll post a humble for loop.

function firstNonRepeatedCharacterPosition(string) {
  for (let char, pos, i = 0; i < string.length; ++i) {
    char = string.charAt(i);
    pos = string.indexOf(char);
    if (pos == i && string.indexOf(char, i + 1) == -1) {
      return pos;
    } 
  }
  return -1;
}
console.log(firstNonRepeatedCharacterPosition('abcbebc'));

It does make 3 function calls per iteration, but they aren't heavy ones, there is only one pass through the string, and early-return programming is in effect.

  • Grab the letter at the incremented position.
  • Find the earliest occurrence of that letter.
  • Check if there is a later occurrence of the same letter.

The later the unique letter exists (or if there are no unique letters), the more laborious my function is. On the other hand, if the first letter is unique, then you are finished in just 3 function calls.

p.s. I lack the knowledge to interpret your js code, so I cannot review it beyond saying that it isn't very novice friendly.

Your solution looks rather map-happy.

It is important to remember for this task that counting higher than 2 of any encountered is needless processing.

Also, doing a complete sweep of the input string may be ill-advised if the input string is of considerable length.

Due to not being as well across .js like others here are, I'll post a humble for loop.

function firstNonRepeatedCharacterPosition(string) {
  for (let char, pos, i = 0; i < string.length; ++i) {
    char = string.charAt(i);
    pos = string.indexOf(char);
    if (pos == i && string.indexOf(char, i + 1) == -1) {
      return pos;
    } 
  }
  return -1;
}
console.log(firstNonRepeatedCharacterPosition('abcbebc'));

It does make 3 function calls per iteration, but they aren't heavy ones, there is only one pass through the string, and early-return programming is in effect.

  • Grab the letter at the incremented position.
  • Find the earliest occurrence of that letter.
  • Check if there is a later occurrence of the same letter.

The later the unique letter exists (or if there are no unique letters), the more laborious my function is. On the other hand, if the first letter is unique, then you are finished in just 3 function calls.

p.s. I lack the knowledge to interpret your js code, so I cannot review it beyond saying that it isn't very novice friendly.

deleted 15 characters in body
Source Link
mickmackusa
  • 8.8k
  • 1
  • 17
  • 31
Loading
Source Link
mickmackusa
  • 8.8k
  • 1
  • 17
  • 31
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /