How to compare strings in different languages (javaScript? [closed] - javascript

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
I need to make a function that compares two strings, no matter the language. Example:
let string1 = 'hola' //Spanish
let string2 = 'hi' //english
console.log (string1 === string2) //true as expected result
Is there a way to compare strings in different languages.
Regards!!

No, not really. Doesn't make much sense either considering that each word might have different meaning in each language depending on context.
Best you can do is try to translate the words and try to find matches in one of the many translations, or use an embedding model for those specific languages and see if the vectors are similar.

The only idea that I have right now is to declare 2 Maps for both languages and then get the key:
var spEn = new Map();
spEn.set('hola', 'hi');
var enSp = new Map();
spEn.set('hi', 'hola');
function spanishEnglish(spanish, en){
translation = spEn.get(spanish);
return translation === en;
}
function englishSpanish(en, spanish){
translation = enSp.get(en);
return translation === spanish;
}
console.log(spanishEnglish('hola', 'hi'))
console.log(englishSpanish('hi', 'hola'))
However this is a just a rudimentary example. There are tons of things you need to pay attention to :)

Related

React -- Why does this line append rather than add [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 15 days ago.
Improve this question
I have other ways of doing the same thing .. So I am not looking for a different solution to this problem ... I am looking for an explanation as to why if I have defined a integer, it still concatenates with .map as if it were a string.
I have a basic set of data retrieved from an API:
"data":["8","8","12","1","7","4","2"]
If I map it using
let count = response.data.metrics.data.map((item) => + parseInt(item));
I am having a hard time understanding why it's treating this as a string returning
88121743
When I feel like because I am parsing it as an integer it should add and come out with 42.
Is this just an issue just using .map? Can shortcut math functions be used here?
Here is my Reproducible Example
Your current approach using Array#map creates a new array with each element converted to a number. React renders this array as multiple text nodes, so it looks like a single string.
To sum the values, use Array#reduce with parseFloat/parseInt, the Number function, or the unary plus operator to convert each string to a number.
const visitCount = data.reduce((a,b) => a + parseFloat(b), 0);

How to extract number from backwards in javascript in optimised way [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 3 years ago.
Improve this question
I have to extract a string from backwards upto 10 digits.
Use case is when we select a mobile number it is prefixed with country code sometimes and sometimes we get only the mobile number.
Let's say the number is : +91-84040355236545
I have to extract the number from the end say from 5 to last 10 degits so the end result would be 0355236545
I have a solution of using string.substring method
Here's a simple solution using substrings:
var number = "+91-84040355236545";
var lastTenDigitsNumber = number.substr(number.length - 10);
console.log(lastTenDigitsNumber);
A simpler solution is using slice:
var number = "+91-84040355236545";
console.log(number.slice(-10));
Another solution using a function and RegEX:
function extractTenDigits(number) {
var rx = /(\d{10}$)/g;
var arr = rx.exec(number);
return arr[1];
}
var number = "+91-84040355236545";
console.log(extractTenDigits(number));
I did a simple benchmark and the best solution for small quantity of numbers is the one using slice.
If you provide more details I can provide a more tailored suggestion.
You can try the following using substring :
let str = "+91-84040355236545";
str.substr(str.length - 10 ,10);

Assignment within a ternary operator, an anti-pattern? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have a colleague who uses ternary operators this way (In javascript):
var genderLabel = '';
isMale? genderLabel = 'Man' : genderLabel = 'Woman';
In C#, I'd just do that .
var genderLabel = isMale? "Man" : "Woman";
My colleague says it's a javascript coding convention... is that true? I'm no javascript expert and I dislike that language... When I review code, I focus on my left hand-side to follow a variable initialization or assignment, that kind of style forces me to read the whole line.
I'm also maintaining a Java code of an ex-employee, he uses ternary operators the same way. Is that an anti-pattern? I think it should be disallowed by the compiler the same way it's disallowed in a if statement :
if(x = 2)
{
...
}
This won't compile in C#.
My colleague says it's a javascript coding convention... is that true?
No.
Is that an anti-pattern?
Usually a line by itself is a statement, however this
isMale ? genderLabel = 'Man' : genderLabel = 'Woman';
is an expression, with a side effect of setting the value of genderLabel. Is that a good practice? I don't know, however if you think that is a good practice, then you will also have to allow this:
var a = 1, b = 2;
b = [a][a = b, 0]; # swap a and b
Your colleague might as well do:
if(isMale) genderLabel = 'Man';
else genderLabel = 'Woman';
which is much clearer.

Can someone help explain why I get this 'Value is not what was expected' error with Javascript? [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 6 years ago.
Improve this question
I'm working on beefing up my Javascript skills with some katas on codewars. Here is one such kata:
At a job interview, you are challenged to write an algorithm to check if a given string, s, can be formed from two other strings, part1 and part2.
The restriction is that the characters in part1 and part2 are in the same order as in s.
The interviewer gives you the following example and tells you to figure out the rest from the given test cases.
For example:
'codewars' is a merge from 'cdw' and 'oears':
s: c o d e w a r s = codewars
part1: c d w = cdw
part2: o e a r s = oears
Here is my solution that I am working on:
function presentInString(element, index, array) {
return string.includes(element);
}
function isMerge(s, part1, part2) {
string = s;
var mergedParts = (part1 + part2).split('');
mergedParts.every(presentInString);
}
My approach is simple, I get passed in a string 'codewars' and with parts 'cdw' and 'oears' The above methods should return true because all of the characters are in the string. but I keep getting a Value is not what was expected error. I must be using the .every method wrong but I'm not sure how. I pretty much based it off of the Javascript MDN docs. Could someone pinpoint what I'm doing wrong?
It's a kata in progress by the way and I haven't tested all edge cases. Some cases will fail.
Additionally, do I have to create another function to pass into .every? I would rather just right the logic in the scope of .every instead of writing another function to pass off to.
I can't vouch for your overall algorithm, but the problem with isMerge is that it doesn't use the return value of every and doesn't return anything. You probably wanted:
return mergedParts.every(presentInString);
as the last line, which makes the return value of isMerge whatever every returns (true if presentInString returns a truthy value for each element, false if it doesn't).

Iterating over an array with `every` [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 6 years ago.
Improve this question
The book that I'm studying says about iterating over arrays with every that:
The function these methods use must follow one ruleā€”it must accept
three arguments like the following code:
function functionName(value, index, array) {
// do something here
}
Does that mean that I must always use 3 arguments? If so then why does this code work?
var numbers = [ 1, 2, 2 ];
function isLessThan3(value) {
var returnValue = false;
if (value < 3) {
returnValue = true;
}
return returnValue; }
document.write(numbers.every(isLessThan3));
There is no limitation on how many arrguments you can put in a function with Javascript.
you have a very good explenation about this topic in the next answer by #Niet the Dark Absol
https://stackoverflow.com/a/22747272/1283672
i believe that the book was reffering to something more specific within it's scope.
And just to be clear you can put no arrgs in a function either.
It's a bit ugly, the code, you have, but there is help. You might use the following without a temporary variable. Just return the result of the comparison.
function allLessThan3(value) {
return value < 3;
}
var numbers = [1, 2, 2];
console.log(numbers.every(allLessThan3));
No, you can use from 0 to 3 arguments

Categories