Reputation: 203
I have been searching high and low for a solution, but I cannot seem to find one. I am trying to make a function that checks two different strings to identify any words in the same position of each word, and output that as a number. An example:
"sterile" "meeting"
These two words have an E and an I in the same place. Never mind letters that exist in both, but scrambled (I have a solution for this, but it does not help me I'm afraid). I am trying to build something that outputs 2 for the two letters that do match.
So yeah, how would I get this to work? Code below is the non-working one, that checks for existing letters in both, and not if they match exactly within the words. Maybe I'm seeing something wrong with it?
var str1 = "sterile";
var str2 = "meeting";
function second() {
return Array.prototype.filter.call(str1, function(c) {
return str2.indexOf(c) === -1;
}, this).join('');
}
function second() {
return Array.prototype.filter.call(str1, function(c) {
return str2.indexOf(c) === -1;
}, this).join('');
}
console.log(second(str1, str2));
console.log(str1.length - second(str1, str2).length);
```
Upvotes: 0
Views: 2304
Reputation: 2036
This should do the job
const str1 = "sterile";
const str2 = "meetinghey";
const getSameLetters = (a, b) => {
const minLength = Math.min(a.length, b.length);
const sameLetters = []
for (let i = 0; i < minLength; i++) {
if (a[i] === b[i]) {
sameLetters.push({ i, letter: a[i] })
}
}
return sameLetters
}
console.log(getSameLetters(str1, str2))
or the following, if you only care about the number of the same letters:
const str1 = "sterile";
const str2 = "meetinghey";
const getSameLetters = (a, b) => {
const minLength = Math.min(a.length, b.length);
let sameLetters = 0
for (let i = 0; i < minLength; i++) {
if (a[i] === b[i]) {
sameLetters++
}
}
return sameLetters
}
console.log(getSameLetters(str1, str2))
The idea behind those algorithms is this:
Math.min(a.length, b.length)
finds.minLength
. On each loop cycle, we compare letters on the same index from a
and b
strings. If letters match, then we increment sameLetters
by one, and continue the loop.sameLetters
- that's the number we're looking for, the number of the same letter under the same indexUpvotes: 0
Reputation: 4545
A short and simple way to accomplish this is by splitting one of the words into an array using split
, then iterating over that array using forEach
to see if the character at each position is equal to the character at that same position in the other word, and increasing the counter if they are the same.
const str1 = "sterile";
const str2 = "meeting";
checkWords = (a, b) => {
let count = 0;
a.split('').forEach((letter, index) => letter === b[index] ? count++ : '');
return count;
}
console.log(checkWords(str1,str2));
Upvotes: 0
Reputation: 2120
Could be optimized and could count for a few edge cases but this might do.
const str1 = 'sterile';
const str2 = 'meetinghey';
function stringEqualityCheck(str1, str2) {
const longestString = str1.length > str2.length ? str1 : str2;
const longestStringArray = longestString.split('');
const shortestString = str1.length > str2.length ? str2 : str1;
let equalityCounter = 0;
shortestString.split('').forEach((letter, index) => {
if (letter === longestStringArray[index]) {
equalityCounter++;
}
});
return equalityCounter;
}
console.log(stringEqualityCheck(str1, str2));
Upvotes: 1