Add +1 for data attributes not working [duplicate] - javascript

This question already has answers here:
Adding two numbers concatenates them instead of calculating the sum
(24 answers)
Closed 5 years ago.
This's my codes:
var following_tags = $('.follow-tags-start').attr("data-following-tags");
$('.follow-tags-start').attr('data-following-tags', following_tags+1);
the issue that if the count of data-following-tags="0" after do the action the count add 1 with the exists count so each time i do the action it's add 1 with 0.
data-following-tags="01111"

you can use parseInt() (assuming these are integer numbers) to convert data attribute to integer, then you can add 1. something like this:
var following_tags = parseInt($('.follow-tags-start').attr("data-following-tags"));
$('.follow-tags-start').attr('data-following-tags', following_tags+1);

You can just use a callback, and coerce the value to a number
$('.follow-tags-start').attr("data-following-tags", function(attr) {
return (+attr) + 1
});

Related

What can I change in my code to extract the first number? [duplicate]

This question already has answers here:
Get the first integers in a string with JavaScript
(5 answers)
How can I extract a number from a string in JavaScript?
(27 answers)
Closed 1 year ago.
I had a problem with extracting numbers from strings. With all the inputs the code works correctly, but there is a task -
If there are two separate numbers in the string - return the first of them.
So from this string 'only 5 or 6 apples', I should get 5. Not 56 or 5 6. I have no idea what to do.
My code looks like this:
function count(apples) {
const number = Math.floor(Number(apples.replace(/[^0-9.]+/g, '')));
console.log(number);
}

How do I convert my HEX string into a number? [duplicate]

This question already has answers here:
How to convert decimal to hexadecimal in JavaScript
(30 answers)
Closed 3 years ago.
I wish to convert the whiteHex variable to a decimal using the parseInt() function, and store it in a variable, whiteDecimal.
var whiteHex = 'ffffff';
var whiteDecimal = parseInt(whiteHex);
I am unsure if the above is correct or not. The reason being, that I then wish to subtract 1 from whiteDecimal and store it in a variable offWhiteDecimal. This is where I am getting stuck. How can I subtract one from the ffffff hex value? Am I missing something within the parseInt function?
You're looking for this:
var whiteDecimal = parseInt(whiteHex, 16)
console.log(whiteDecimal - 1);
ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#Syntax

JS how to add instead of concatenate numbers [duplicate]

This question already has answers here:
Javascript variables not adding two variables correctly, only concatenating
(3 answers)
Closed 4 years ago.
This question has an answer:
see - Javascript variables not adding two variables correctly, only concatenating
I am trying to add 5 units to a number but the number is being concatenated instead.
this.graphicState[i].shapes[j][k].x += 5
Each time this is run in a loop the outputs are
105.00
105.005
105.0055
105.00555
...
The output I am looking for is,
105.00
110.00
115.00
120.00
...
I tried,
this.graphicState[i].shapes[j][k].x += parseFloat(5)
I also tried this, but get the same results,
this.graphicState[i].shapes[j][k].x = this.graphicState[i].shapes[j][k].x + 5
Thanks,
You need to convert any/all string values to numbers in an expression that has strings as operands with + as the operator. The 5 isn't the issue, this is:
this.graphicState[i].shapes[j][k].x
So, that's what needs to be converted. You can do that easily by prepending a + to it:
+this.graphicState[i].shapes[j][k].x;
Do the conversion on the string first and then use the converted value in your mathematical expression. Here's a simplified example:
var result = "5"
result = +result + 10;
console.log(result);
Try this method
this.graphicState[i].shapes[j][k].x = (parseFloat(this.graphicState[i].shapes[j][k].x) + 5).toFixed(2);

Multiply a string in JS [duplicate]

This question already has answers here:
Repeat a string in JavaScript a number of times
(24 answers)
Closed 4 years ago.
i want to display a string as many times I have a generated variable. Therefore I'd like to do something like that, which doesn't work
var shower_total = 7; // this gets generated, but for simplification...
var uhrzeit = "<p class='clock_item'>Foo</p>";
document.getElementById("uhrzeit_block").innerHTML =5*uhrzeit;
That's why I tried looping it but that doesn't work neither
document.getElementById("uhrzeit_block").innerHTML =
for(b=0, b <= shower_total; b++){
uhrzeit
};
What do I do wrong or what would be a possible - beginner-compatible - solution. Thanks!
You could use String#repeat instead of a multiplication of a string. This does not work for value who can not converted to a number.
var uhrzeit = "<p class='clock_item'>Foo</p>";
console.log(uhrzeit.repeat(5));

Javascript/jquery get number value inside of element [duplicate]

This question already has answers here:
get element value using jquery
(2 answers)
Closed 9 years ago.
<span id="rate">2388</span>
var = $("#rate")...
I need get number value inside of element. any one know how to do it?
var number = +$("#rate").text();
// | \______get text of span as string
// |_________________unary plus converts to numeric
You can get the text inside your span using text() method and parseInt() to convert it to number:
var rateVal = parseInt($("#rate").text(), 10);
You can do also this
var result = Number($("#rate").html());
But becareful to use Number functon, Please have look this discussion for difference between parseInt and Number.
What is the difference between parseInt(string) and Number(string) in JavaScript?, as Roko indicated.

Categories

Resources