how to find no. of similar characters in two strings in javascript [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to find the no. of similar characters between two strings using JS/PHP
Example
str1: Mack
str2: Michelle
Similar Characters: "M" "C"
similar character count: 2

I'll do this:
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
var counter = 0;
var min = Math.min(str1.length, str2.length);
for(var i = 0; i < min; i++)
{
if(str1.charAt(i) === star2.charAt(i))
counter++;
}
alert("Result: "+counter);

After your precision, here is my solution:
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
var counter = 0, find = -1;
for(var i = 0; i < str1.length; i++)
{
find = str2.indexOf(str1.charAt(i));
if(find > -1)
{
counter++;
str2 = str2.substr(0, find) + str2.substr(find + 1);
}
}
alert("Result: "+counter);
It works with the 2 examples you gave us:
Michellle and Michelle = 8
Sneha and naveen = 3

Use similar_text() function
<?php
echo "character count: ". similar_text("Mack","Michelle");
?>
Output
character count: 2

I'd need more examples to make sure this works, but using regular expressions might work for you:
function similar_text_regex(str1, str2){
var regEx = new RegExp('['+str2+']', 'gi');
var matchCnt = str1.match(regEx2).length;
return matchCnt;
}
console.log(similar_text_regex('Michelle', 'Michellle')); // --> 8
console.log(similar_text_regex('Jason', 'Jane')); // --> 3
console.log(similar_text_regex("sneha","naveen")); // --> 3

Related

Why is my JavaScript code not accepted as the right answer? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I am trying to do this Javascript exercise: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/counting-cards
I am wondering why the solution below is not an accepted answer:
let count = 0;
function cc(card) {
// Only change code below this line
const low = [2, 3, 4, 5, 6];
const high = [10, 'J', 'Q', 'K', 'A'];
if (low.includes(card)) {
count += 1;
}
else if (high.includes(card)) {
count -= 1;
}
let decision;
if (count > 0) {decision = "Bet"}
else {decision = "Hold"}
return count + decision;
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
When I am comparing it to accepted answers I don't see what they are doing differently. One thing that is not clear to me in the assignment is that should return be called every time or only after the last function call (cc('A');).
Add a space between count and decision
return count + " " + decision;
You are giving an answer in the wrong format. Just missing the space between count and decision.
Incorrect:return count + decision;
Correct:return count +" "+ decision;

Invalid expression term '[' using c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Can someone assist me what's wrong in my code. I'm try to convert JavaScript code to C# code
public class SplitString
{
public static string[] Solutions(string str)
{
arr = [];
for(var i = 0; i < str.Length; i += 2){
second = str[i+1] || '_';
arr.push(str[i] + second);
}
return arr;
}
}
And i encounter this error
src/Solution.cs(5,11): error CS1525: Invalid expression term '['
src/Solution.cs(5,12): error CS0443: Syntax error; value expected
javascript and C# are completely different languages; .NET / C# arrays are fixed size - so, you might want a list here:
var arr = new List<string>();
for(var i = 0; i < str.Length; i += 2){
var second = str[i+1]; // || '_'; <== this on the right makes no sense in C#
arr.Add(str[i] + second);
}
return arr.ToArray();

jQuery count elements Start from 0? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to count the elements on page BUT i need the numbers to start from 0 as opposed to 1.
I know I can simply do var tadada = $('.clickableDiv').length;
but this will return the numbers from 1.
is this possible using jquery or javascript?
It's not clear what you're trying to do. If there's one element on the page, length being 1 makes sense. Otherwise, you'd have no way to express the fact that there is no matching element on the page (length = 0).
If you mean you're trying to find their indexes, then you'd just count from 0 through length - 1, usually be using < length as a loop condition. E.g.:
var foos = $(".foo");
for (var n = 0; n < foos.length; ++n) {
console.log("#" + n + " is '" + foos.eq(n).text() + "'");
}
<div class="foo">First</div>
<div>(not foo)</div>
<div class="foo">Second</div>
<div>(not foo)</div>
<div class="foo">Third</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
But you don't want length to be 0 when there's one element. It doesn't make sense.
Of course, if you're trying to output numbers in a list or something for human consumption, adding one to the index for display purposes is common practice:
var foos = $(".foo");
for (var n = 0; n < foos.length; ++n) {
console.log((n + 1) + ": '" + foos.eq(n).text() + "'");
// Adding 1 ^^^^^^^ for display purposes only
}
<div class="foo">First</div>
<div>(not foo)</div>
<div class="foo">Second</div>
<div>(not foo)</div>
<div class="foo">Third</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
This should work:
var tadada = $('.clickableDiv').length - 1;

Reverse The Odd Words in the sentence [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I am trying to reverse the odd words in the sentence. Its working fine. But its appending undefined in the front.
var str = "get busy living or get busy dying.";
var newstr = str.split(" "), result;
for(i=0;i<newstr.length;i++){
if(i%2 !== 0){
result += newstr[i].split("").reverse().join("");
result += ' ';
} else {
result += newstr[i];
result += ' ';
}
}
Output is
undefinedget ysub living ro get ysub dying.
Can some one point me where i am going wrong!!!!!
You're not initializing result, so the first += adds something to an undefined value.
Just declare and initialize result:
var result = "";
You could also do it more concisely:
var str = "get busy living or get busy dying.";
var result = str.split(" ").map(function(word, i) {
return i % 2 == 0 ? word : word.split("").reverse().join(""); }).join(" ");
alert(result);

numbers and words not numbers to words js [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a simple code gotten from the internet and it did not answer what I really wanted as output. I have two input fields; one for the input and another for the output and they are processed through this function:
<script type="text/javascript">
function AnEventHasOccurred() {
var x = document.getElementById("onkeyup").value
if (x >= "100") {
document.getElementById("eventlog").value = "" +
return x = ['Generalities'];
}
}
</script>
What I'm really needing is that when I enter numbers below 100, output must be Generalities. I haven't got it correctly. And I went here to ask some help. Thanks.
You're never outputting your value back into the output field. All you're doing is returning the value. You need to set the value of your output field to "Generalities".
Example
var input = document.getElementById("onkeyup").value;
// You should be giving your elements meaningful IDs.
if(+input < 100) {
document.getElementById("output").value = 'Generalities';
// Assumes an output field called "output".
}
Try this:
function AnEventHasOccurred() {
var x = document.getElementById("onkeyup").value;
if (x < 100){
document.getElementById("eventlog").value = "Generalities";
}
}
I see a few errors. Check this out for comparison:
function AnEventHasOccurred() {
// should probably save the elements to variables
// since you'll be checking and changing the values
var x = document.getElementById("onkeyup");
var y = document.getElementById("eventlog");
// should be 100, not "100"
if (x.value < 100) {
y.value = "Generalities";
} else {
y.value = "";
}
}
This should work fine. Check it out on jsfiddle.
More Recommendations
Your return statement doesn't correspond with your "output": it
does nothing valuable in this case.
You check or set the value of an input by getting the element and
using its value key.
You should put semi-colons at the end of most javascript lines, with the exceptions generally being curly brackets {}, comments // and /* */, and empty lines.

Categories