Skip to content

Instantly share code, notes, and snippets.

@juliaamosova
Last active May 3, 2018 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliaamosova/c76d8436c69c1d518784adc41d5ea422 to your computer and use it in GitHub Desktop.
Save juliaamosova/c76d8436c69c1d518784adc41d5ea422 to your computer and use it in GitHub Desktop.
Checkio: Return the second index of a symbol in a given text.
/*
You are given two strings and you have to find an index of the second occurrence of the second string in the first one.
Let's go through the first example where you need to find the second occurrence of "s" in a word "sims". It’s easy to find its first occurrence with a function indexOf which will point out that "s" is the first symbol in a word "sims" and therefore the index of the first occurrence is 0. But we have to find the second "s" which is 4th in a row and that means that the index of the second occurrence (and the answer to a question) is 3.
Input: Two strings.
Output: Int or undefined
*/
function secondIndex(text, symbol) {
// returns the second index of a symbol in a given text
var count = 0;
// your code here
for (var i = 0; i < text.length; i++) {
if (text[i] === symbol) {
count++;
}
if (count === 2) {
return i;
}
}
}
var assert = require('assert');
if (!global.is_checking) {
console.log('Example')
console.log(secondIndex("sims", "s"))
// These "asserts" are used for self-checking and not for an auto-testing
assert.equal(secondIndex("sims", "s"), 3)
assert.equal(secondIndex("find the river", "e"), 12)
assert.equal(secondIndex("hi", " "), undefined)
assert.equal(secondIndex("hi mayor", " "), undefined)
assert.equal(secondIndex("hi mr Mayor", " "), 5)
console.log("You are awesome! All tests are done! Go Check it!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment