Use a compound assignment operator:
v += 4;
Answer from helpermethod on Stack OverflowHow does this increment after 5 runs?
variables - JavaScript incrementing by 0.5 - how? - Stack Overflow
Basic JavaScript: Increment a Number with JavaScript
javascript - Increment HTML5 Number Field by .5 with step of .01 - Stack Overflow
Videos
Learning simple Javascript concepts using Pluralsight and I've stumbled upon a challenge that I don't fully understand. I get how this block of code works for the most part. What I can't figure out is how it is clearing the interval after it repeats the console.log() block 5 times. Can someone explain to me how this knows to increment the delay every 5 runs?
let lastIntervalId, counter = 5;
const greeting = delay => {
if (counter === 5) {
clearInterval(lastIntervalId);
lastIntervalId = setInterval(() => {
console.log('Hello World. ' + delay);
greeting(delay + 100);
}, delay);
counter = 0;
}
counter += 1;
};
greeting(100);
How I read this is counter is set to 5. The IF block is checking if the counter is set to 5 and running the code within it. It sets an interval, executes console.log, executes the greeting function again with a 100ms delay, which (in my mind) should loop things back to line 3 and start over indefinitely. How does it know to add another 100ms every 5th time? Thanks for the help!
newvalue is a string. Use += on your num directly:
num += 0.5;
You are casting it to a number, but still call the string variable in the following code:
var num = new Number(newvalue);
var num = newvalue += 0.5;
var newtemp = newvalue + '°';
I think that what you meant to do was
var num = new Number(newvalue);
num = num += 0.5;
var newtemp = num + '°';
But whichever it is, you should keep a numeric variable out of the function, and increment that, instead of loading the temperature that you posted on the screen from the last run, and then do it again over and over.
Basically, you need to change its set value property between:
<input type="number" step=".01">
and
<input type="number" step=".5"> When the user clicks the up/down arrow or presses the up/down keys.
So, I've made an example in JavaScript to illustrate this particular scenario.
Something like this:
(function() {
var numberFields = document.querySelectorAll("input[type=number]"),
len = numberFields.length,
numberField = null;
for (var i = 0; i < len; i++) {
numberField = numberFields[i];
numberField.onclick = function() {
this.setAttribute("step", ".5");
};
numberField.onkeyup = function(e) {
if (e.keyCode === 38 || e.keyCode === 40) {
this.setAttribute("step", ".01");
}
};
}
}());
<input type="number" step=".01">
You can't do this with an number input, but with a text input, the magic happens...
I've styled it a bit so you can see when it's valid and invalid.
It is possible to use negative numbers in here, so if you want that I can edit it, but it gets a little wacky around -1 to 0.
let input = document.getElementById('addNum');
increment = 0.5;
input.onkeydown = function(e){
if(e.key == "ArrowUp"){
e.preventDefault();
let value = +this.value;
this.value = value + increment;
} else if(e.key == "ArrowDown"){
e.preventDefault();
let value = +this.value;
this.value = value - increment;
}
}
input:valid {
border-color: #0f0;
}
input:invalid {
border-color: #f00;
}
input {
outline: none;
}
Enter a (non-negative) number:
<input type="text" pattern="\d*[.]*\d{0,3}" id="addNum">
Use the += assignment operator:
for (var i = 0; i < myVar.length; i += 3) {
Technically, you can place any expression you'd like in the final expression of the for loop, but it is typically used to update the counter variable.
For more information about each step of the for loop, check out the MDN article.
A for loop:
for(INIT; TEST; ADVANCE) {
BODY
}
Means the following:
INIT;
while (true) {
if (!TEST)
break;
BODY;
ADVANCE;
}
You can write almost any expression for INIT, TEST, ADVANCE, and BODY.
Do note that the ++ operators and variants are operators with side-effects (one should try to avoid them if you are not using them like i+=1 and the like):
++imeansi+=1; return ii++meansoldI=i; i+=1; return oldI
Example:
> i=0
> [i++, i, ++i, i, i--, i, --i, i]
[0, 1, 2, 2, 2, 1, 0, 0]