最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

string - Where should I find how the indexOf function is written for JavaScript core looks like? - Stack Overflow

programmeradmin1浏览0评论

I want to see it because there is an algorithm interview question asked similar thing but no one was able to resolve it without some sort of internal function like indexOf() which would have been perfectly fine except that the requirement was NOT to use an internal function to resolve this. Since I think the best implementation is already there, why don't I just learn from the best? Which I suppose is how they wrote .indexOf() for String already in JavaScript.

My solution was humiliating to even show it... so I decide not to, in other words, there are way too much conditional toggling than I would want to put in there.

Thanks

Question is following

Avoid using built-in functions to solve this challenge. Implement them yourself, since this is what you would be asked to do during a real interview.

Implement a function that takes two strings, s and x, as arguments and finds the first occurrence of the string x in s. The function should return an integer indicating the index in s of the first occurrence of x. If there are no occurrences of x in s, return -1.

Example

For s = "CodefightsIsAwesome" and x = "IA", the output should be strstr(s, x) = -1; For s = "CodefightsIsAwesome" and x = "IsA", the output should be strstr(s, x) = 10. Input/Output

[time limit] 4000ms (js) [input] string s

A string containing only uppercase or lowercase English letters.

Guaranteed constraints: 1 ≤ s.length ≤ 106.

[input] string x

String, containing only uppercase or lowercase English letters.

Guaranteed constraints: 1 ≤ x.length ≤ 106.

[output] integer

An integer indicating the index of the first occurrence of the string x in s, or -1 if s does not contain x

Bitw its from codefight

For anyone interested to test their code in full with all test cases, its here (see where my cursor is pointing to in the image vvv?)

I want to see it because there is an algorithm interview question asked similar thing but no one was able to resolve it without some sort of internal function like indexOf() which would have been perfectly fine except that the requirement was NOT to use an internal function to resolve this. Since I think the best implementation is already there, why don't I just learn from the best? Which I suppose is how they wrote .indexOf() for String already in JavaScript.

My solution was humiliating to even show it... so I decide not to, in other words, there are way too much conditional toggling than I would want to put in there.

Thanks

Question is following

Avoid using built-in functions to solve this challenge. Implement them yourself, since this is what you would be asked to do during a real interview.

Implement a function that takes two strings, s and x, as arguments and finds the first occurrence of the string x in s. The function should return an integer indicating the index in s of the first occurrence of x. If there are no occurrences of x in s, return -1.

Example

For s = "CodefightsIsAwesome" and x = "IA", the output should be strstr(s, x) = -1; For s = "CodefightsIsAwesome" and x = "IsA", the output should be strstr(s, x) = 10. Input/Output

[time limit] 4000ms (js) [input] string s

A string containing only uppercase or lowercase English letters.

Guaranteed constraints: 1 ≤ s.length ≤ 106.

[input] string x

String, containing only uppercase or lowercase English letters.

Guaranteed constraints: 1 ≤ x.length ≤ 106.

[output] integer

An integer indicating the index of the first occurrence of the string x in s, or -1 if s does not contain x

Bitw its from codefight https://codefights./interview-practice/task/C8Jdyk3ybixqQdAvM

For anyone interested to test their code in full with all test cases, its here (see where my cursor is pointing to in the image vvv?)

https://codefights./interview-practice/topics/strings

Share Improve this question edited Oct 13, 2017 at 15:53 ey dee ey em asked Oct 13, 2017 at 13:42 ey dee ey emey dee ey em 8,67315 gold badges72 silver badges128 bronze badges 6
  • 1 but no one was able to resolve it how do you know? I mean, this is simple to write – Jaromanda X Commented Oct 13, 2017 at 13:44
  • by looked at the top solutions got voted up in JS in codefights as well as whats already on stackoverflow? @Jaromanda X – ey dee ey em Commented Oct 13, 2017 at 13:45
  • V8 (the JavaScript engine in Chrome) and SpiderMonkey (the JavaScript engine in Firefox) are both open source... – T.J. Crowder Commented Oct 13, 2017 at 13:47
  • 2 The problem is indexOf is most likely written in C++ as it's a core function of the String object. And if your challenge is to write it in Javascript, it's not really going to help you is it? – Keith Commented Oct 13, 2017 at 13:49
  • en.wikipedia/wiki/String_searching_algorithm – Bergi Commented Oct 13, 2017 at 13:58
 |  Show 1 more ment

2 Answers 2

Reset to default 6

no one was able to resolve it

I'm a little shocked that was the case, as it's just 2 simple nested for loops. Eg, it took me less than a few minutes to knock this up.

function strstr(a,b) {
  for (var o = 0; a[o]; o ++)
  {
    var found = true;
    for (var i = 0; b[i]; i ++) {
      if (a[o + i] !== b[i]) { found=false; break; }
    } 
    if (found) return o;
  }
  return -1;
}

var s = "CodefightsIsAwesome";
var x = "IA";

console.log(strstr(s, x));

s = "CodefightsIsAwesome";
x = "IsA";

console.log(strstr(s, x));

JavaScript runs on ECMAScript Engines. ECMAScript is nothing but the specification of JavaScript. Different browser provides different implementation for that, like Chrome has V8 engine implementation for ECMAScript, similarly Carakan for Opera. Source

So with that in mind... Here you might get some more information on how things work:

ECMA Specification

General MDN entry: Click

发布评论

评论列表(0)

  1. 暂无评论