Compare text of equal strings in JavaScript [duplicate] - javascript

This question already has answers here:
How to compare an array of strings in Javascript?
(6 answers)
Closed 3 months ago.
I have a little problem to solve. I want to compare two texts if they are the same.
let currentValue = "24960307.W 25880305.W 24880208.W 25650156.W"
let newValue = "24880208.W 24960307.W 25650156.W 25880305.W"
// is the same text just diferent order
// when i did includes
let x = currentValue.includes(value);
console.log(x);
//response in console
false
I tried with includes and localeCompare but still show that text is different.

A quick solution to normalize order is to split, sort, rejoin, then compare. This avoids any fancy parsing.
let currentValue = "24960307.W 25880305.W 24880208.W 25650156.W"
let newValue = "25880305.W 24880208.W 25650156.W 24960307.W"
const a = currentValue.split(' ').sort().join(' ')
const b = newValue.split(' ').sort().join(' ')
console.log(a === b)
let x = a.localeCompare(b) === 0
console.log(x);

Related

I'm trying to create a function that follows the Luhn's Algorithm [duplicate]

This question already has answers here:
Implementation of Luhn algorithm
(14 answers)
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I tried to multiply each number whose index number is an even number by two, and that worked fine. Still, the problem lies here: If any of the results is greater than or equal to 10, then add up the two numbers, for example, if one of the results is 12, then add up 1 and 2, which should be equal to 3. So this is what I tried:
var num = 122345643345673;
var convNum = num.toString();
var aftertoString = convNum.split("");
for(let i = 1; i < aftertoString.length; i++){
if (i % 2 == 0) {
var multi = aftertoString[i] * 2;
if(multi > 10){
var multiStringed = multi.toString();
var aftermutliStringed = multiStringed.split("");
var first = parseInt(aftermutliStringed[2])
var multi = first + first;
}
console.log(multi);
}
}
Since any index of the "aftermultiSringed" array is not a number, I tried to convert it to a number using the "parseInt()" method, but the result is NaN, please why am I getting this.
The method parseInt usage is incorrect.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
// var first = aftermultiStringed[1].parseInt();
var first = parseInt(aftermultiStringed[1]);

TypeScript: How to replace a particular number in a string (a combination of numerical and non-numerical values), and then update its value?

I asked a similar question here TypeScript: How to replace a particular number in a string, and then update its value?
Really appreciate the answers there. It answered my questions.
I am run into a new issue. Thought it s more helpful to raise a new question while referring to the old question.
I have a column that is full of 10-digits string.
Sometimes that entire 10-digits only contain numerical values (e.g. 3345678901), but sometimes there is dash included (e.g. 3345678---)
I would like to be able to:
Input an index number X
Locate the corresponding number in the string
Add or subtract a particular number A to/from that number to update the string
Update the string
Example 1 (all numerical): 3345678901
Input index number "4"
The corresponding number is 6
Increase that number by +2 to 8
Update the string to 3345878901
Example 2 (numerical & non-numerical): 3345678---
Input index number "4"
The corresponding number is 6
Increase that number by +2 to 8
Update the string to 3345878---
Example 3 (numerical & non-numerical): 3345678---
Input index number "7"
The corresponding value is -
Increase (or rather, update) that number by +2 to 2
Update the string to 33458782--
For example 1, I know I could do following (as a contributor from the OG post has pointed out):
const givenStr = "3345678901";
let givenStrArray = givenStr.split('').map(Number);
const inputIndex = 4;
const increaseAmount = 2;
givenStrNumber += increaseAmount
console.log(givenStrNumber);
But, how do I go about Example 2 and 3 though? Since there are string '-' involved? In this case map(Number) would lead to Null values that would break the code.
Any help would be greatly appreciated!
Check for NaN. This is a tiny snippet that would give you a hint:
// ...
// ...
const increment = 2;
let givenStrArray =
givenStr
.split('')
.map((digiit) => isNaN(digit) ? increment : Number(digit + increment);
// ...
// ...
Here's a simple little snippet. Break the sting apart, map through each item, when we find the index we need to update we update it and return it, then join them all back together.
Multiple conditional ternary operator have been used here, you can find out more about how they work here: Conditional (ternary) operator
const givenStr = "334567----",
inputIndex = 6,
increaseAmount = 2
let givenNumber = givenStr
.split('')
.map((v,i) => i === inputIndex ? isNaN(v) ? increaseAmount : +v + increaseAmount : v)
.join('')
console.log(givenNumber);
This should do the trick
let str = '3345678---'
let NumericStrArray = str.split('').map(x => Number.isNaN(Number(x)) ? x: Number(x))
let ipIndex = 7
let incAmount = 2
let val = NumericStrArray[ipIndex]
NumericStrArray[ipIndex] = typeof val === 'string' ? incAmount : val + incAmount
let result = NumericStrArray.join('')

Is parseInt doesn't needed for '*' in JavaScript? [duplicate]

This question already has answers here:
What exactly is Type Coercion in Javascript?
(9 answers)
Closed 2 years ago.
I have array, by which at some point am mapping the array and calculating sum and percentages. So while implementing the logic i saw that, when i use '*' directly its working but when i use '+' it just adds the two string
For example:
const a = '100';
const b = '10';
const c = a * b;
const d = a + b;
console.log(d)
When i checked the d , it gives '10010' and when c it gives '1000' ! How is this ?
But when i use parseInt(a) + parseInt(b) it works perfectly with 110 as output
In JavaScript there are no primitive datatypes like int, float etc. There are only variables which can be initialized with everything you need. For your example
const a = 100;
const b = 10;
const c = a * b;
const d = a + b;
console.log(d);
should work perfectly, because I removed ''. With '' the constant thinks it is a string provided. Without '' there are just the numbers saved in the constants. Also + doesn't work in your example, because as I said the constants think the numbers are a string due to ''. So it just puts this two "strings" together and not summing them up.

Setting multiple javascript variables at once [duplicate]

This question already has answers here:
What does a comma do in assignment statements in JavaScript?
(4 answers)
Closed 4 years ago.
I found some code which looks like this
var v = color.val(), sel_c = (v == '') ? "#234872" : v;
I'm not sure why it was written like this, but I was wondering how to read it. If we'd just have
var v = color.val();
or
var sel_c = (v == '') ? "#234872" : v;
I'd understand. But what does it mean when you separate it with commas? Its as if we were trying to set multiple variables at once or something...
The comma operator is used to separate multiple statements.
In the case of variable assignment it is just a way to leave out subsequent var keywords. Each variable is set in turn
Yap, totally valid to omit having to write var, let or const multiple times
const a = 1,
b = a + 1,
c = b + 1;
console.log(a, b, c);
Is the same as
const a = 1;
const b = a + 1;
const c = b + 1;
just a few var, let or const fewer to write.
The following is the equivalent of the code you posted:
var v= color.val();
var sel_c;
if(v==''){
sel_c="#234872"
}else{
sel_c=v;
}
As for why it was written as you posted it - I believe it is to conserve "space". And by space I dont mean bytes of data, but rather lines in the code editor. At least that is the reason I always get. I personally think that written like that the code is less readable, even if more compact.
This:
var v = color.val(), sel_c = (v == '') ? "#234872" : v;
equals to:
var v = color.val();
var sel_c = (v == '') ? "#234872" : v;
The only benefit of writing it on one line is that your code will be a bit "shorter".
I personally find it ugly and hard to read.

Javascript: compare string numbes above 10.0 [duplicate]

This question already has an answer here:
Comparing numbers of type string
(1 answer)
Closed 4 years ago.
After ajax query I have the next numbers:
var a = "5.20";
var b = "7.20";
var c = "11.20";
I have noticed that if compare numbers above 10.00 the condition is false. I do not understand why this happens
if (b > a) console.log("higher"); // true
if (c > b) console.log("higher"); // not true
I have solved the problem with next code (it should also work parseFloat)
if ((+c) > (+b)) console.log("higher"); // true
"5" > "10" for the same reason that "m" > "ba". Strings are compared alphabetically (even if they contain digits) - or rather lexicographically.
The variables are string type. Not integers.
typeof("5.20"); // OUTPUT: "string"
Remove the quotation marks:
var a = 5.20;
var b = 7.20;
var c = 11.20;
if (b > a) console.log("higher"); // true
if (c > b) console.log("higher"); // also true
Example: https://www.w3schools.com/js/js_comparisons.asp
You are comparing two strings alphabetically. If you remove the quotes where you create the variables then you will have numbers and your code will work.
var a = 5.20;
var b = 7.20;
var c = 11.20;

Categories