This question already has answers here:
How to make 1 + 1 = 2 instead of 1 + 1 = 11 [duplicate]
(5 answers)
Closed 3 years ago.
I'm trying to do a function which add 5 each time I'm adding character to textarea, and subtract 5 when I'm removing a character.
This is my HTML
<textarea rows="4" cols="50" id="my-area"></textarea>
<p>Chars: <span id="counter">0</span></p>
This is my JS
let area = document.getElementById("my-area"),
counter = document.getElementById("counter"),
count = 0;
area.onkeyup = area.onkeydown = area.onpaste = area.onchange = function() {
if (area.value.length > count) {
count++;
parseInt(counter.innerHTML);
counter.innerHTML = parseInt(counter.innerHTML + 5);
} else if (area.value.length < count) {
count--;
counter.innerHTML -= 5;
}
};
When I add a character first time it works correct - 5 is adding to innerHTML of counter, but when I add second character number is concatenated not added. Subtraction works good. How Can I fix the adding?
And the second situation: How to make it work if I add to p which id is "counter" a string "PLN" or "DOLLARS" (example: "0 PLN") and I want it to add five to the number and substract 5 after?
Its because of the Type Coercion in javascript.
Instead parse it to int and then add 5
parseInt(counter.innerHTML) + 5
Related
This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
Why does JavaScript handle the plus and minus operators between strings and numbers differently?
(7 answers)
Closed 27 days ago.
All except + do the math. How do I do an actual addition mathematically? Don't ask why I need it. I'm curious.
let first = "2" + 2
let second = 4 + 4
let third = first + second
console.log(third)
logged 228
let first = "2" + 2
let second = 4 + 4
let third = first - second
console.log(third)
logged 14
let first = "2" + 2
let second = 4 + 4
let third = first * second
console.log(third)
logged 176
let first = "2" + 2
let second = 4 + 4
let third = first / second
console.log(third)
logged 2.75
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]);
This question already has answers here:
javascript random pick variable
(2 answers)
JavaScript random generate 0 or 1 integer [duplicate]
(2 answers)
Closed 2 years ago.
quick question: Is it possible to set a button that can increment and or decrement a number as it is clicked? I want to know if such a feature is possible and if so, how?
You can use Math.random() to decide whether you are going to increment or not:
var counter = 0,
$counter = document.getElementById('counter'),
$myBtn = document.getElementById('myBtn');
$myBtn.addEventListener('click', incrementOrDecrement);
function incrementOrDecrement() {
var shouldIncrement = Math.random() > 0.5; // 50% chances of incrementing
counter += shouldIncrement ? 1 : -1;
$counter.innerText = counter;
}
<button id="myBtn">Change counter</button>
<h2 id="counter">0</h2>
I just have a question about some while loop logic.
So, when you write a loop that displays a string of numbers to a document and say that while the loop is <= (less than or equal to) say, 5, and you tell the loop to add 1 each time this is true, wouldn't that mean that: while the loop is equal to 5 that it would add one to 5 too? It doesn't, but I messed up on some code when I was practicing and noticed that when it is equal to five it does not add one, but I thought it would...
console.log('2nd Loop:');
text = '';
// loop:
i = 1;
while (i <= 5) {
text += i + ' ';
i += 1
}
console.log(text); // Should print `1 2 3 4 5 `.
the reason your text doesn't display a 6 isn't because i isn't incremented. It's because the text gets added onto before it's incremented.
In other words when executing on that 5th loop, the text would add on 5, and then it would increment i, and then it would check the loop again, which would no longer be valid and therefore 6 is never printed.
In memory, it adds one. It doesn't add it to the text though.
Since you're incrementing the value after assigning it and then the loop condition fails, it doesn't get to the part where you concatenate the string.
It does. Just output i and you'll see it's 6. text never gets the 6 because of when you increment i.
console.log('2nd Loop:');
text = '';
// loop:
i = 1;
while (i <= 5) {
text += i + ' ';
i += 1
}
console.log(text,i); // Should print `1 2 3 4 5 `.
b/c you +1 after you add i to text, all you need to do is switch the two line order.
EDIT
if you want it start with one just change your i to 0 to start with.
i = 1
console.log('2nd Loop:');
text = '';
i = 0;
while (i <= 5) {
i += 1
text += i + ' ';
}
console.log(text);
This question already has answers here:
How do I check that a number is float or integer?
(52 answers)
Closed 7 years ago.
im trying to check if a number is a whole after a calculation. What I have so far prints out how many times one number gets divided by another, but when the number is not whole it dose not print anything out. Heres my code:
function round() {
var percent = document.getElementById('percent_sale').value;
var perShare = document.getElementById('singleShare').value;
var result = (percent / perShare);
if(result % 1 == 0) {
document.getElementById('results1').innerHTML = ('Number of shares:'+result);
} else {
document.getElementById(results1).innerHTML = ('number of shares must ');
}
}
The values get input buy a user, and the percent for sale is say 50 and the single share is say 2.5 this would return 20 shares.
What I need is if I put in something like 50 for sale and 3.15 single share it tells the user to make equal number of shares as it would return 15.87
Any ideas where ive gone wrong?
Convert your number into string and then check if the string contains only numbers
var num = 15;
var n = num.toString();
This will convert it into string then this
String.prototype.isNumber = function(){return /^\d+$/.test(this);}
console.log("123123".isNumber()); // outputs true
console.log("+12".isNumber()); // outputs false
For further reference.Link StackOverFlow