Reputation: 1264
I had this question on an interview and I failed. I was close with my answer but I didn't finish the script. The question was: get the text from the paragraph, add one and put back into paragraph every second the new value. My logic was that in the p the number is a string. I convert it in number with parseInt, then check if is notNaN, alert not a number, else increase the value with one and put it back into the p.
Anyone please tell me where I was wrong.
JS:
$(function() {
setInterval("addOne()", 1000);
});
function addOne() {
var divP = $("div p").text();
parseInt(divP);
if (isNaN(divP)) {
alert("Not a number!");
} else {
divP++;
$("div p").text();
}
}
HTML:
<div>
<p>1</p>
</div>
Upvotes: 1
Views: 86
Reputation: 1
You can find the answer here.
HTML
<div>
<button id="btnClickStart" >
Start Increment
</button>
<button id="btnClickStop">
Stop Increment
</button>
<p id="pId">
I am testing this script if it will work the 1st time.
</p>
</div>
JAVASCRIPT
$('#btnClickStart').click(function() {
startIncrement();
});
$('#btnClickStop').click(function() {
stopIncrement();
});
var myInterval ;
function startIncrement(){
myInterval = setInterval(function(){ addOne(); }, 1000);
}
function stopIncrement() {
clearInterval(myInterval);
}
var _array = [];
function addOne() {
var divP = document.getElementById('pId').innerHTML;
var _array = [];
for (var i = 0, len = divP.length; i < len; i++) {
parseInt(divP[i]);
if (isNaN(divP[i])) {
//alert("Not a number!");
} else{
_array.push(divP[i]);
}
}
for (var j = 0, len = _array.length; j < len; j++) {
_str =parseInt( _array[j]);
if(!isNaN(_str) ){
divPLast= divP.replace(_str, parseInt(_str)+1);
}
}
document.getElementById('pId').innerHTML = divPLast;
}
Upvotes: 0
Reputation: 740
You can use this
$("div p").html();
instead of
$("div p").text();
Sorry, it must be html instead of val because val is for input elements. Thank You
Upvotes: 1
Reputation: 337580
You have quite a few issues:
parseInt()
to anythingradix
parameter to parseInt()
addOne()
function to setInterval()
to avoid the intrinsic use of eval()
text()
, not setting the value of divP
Here's a working example:
$(function() {
setInterval(addOne, 1000);
});
function addOne() {
var divP = parseInt($("div p").text(), 10);
if (isNaN(divP)) {
alert("Not a number!");
} else {
$("div p").text(++divP);
}
}
It could also be argued that calling an alert()
to show UI state information (especially in a function called every second) is not good practice.
Upvotes: 5
Reputation: 13679
There are 3 errors I can find.
You didn't assign the parseInt(divP)
to anything.
Should be:
divP = parseInt(divP);
You are not using the setInterval
properly.
Should be: setInterval(function(){ addOne(); }, 1000);
You didn't put the new value to the element $("div p").text();
.
Should be: $("div p").text(divP);
Do something like this:
$(function() {
setInterval(function(){ addOne(); }, 1000);
});
function addOne() {
var divP = $("div p").text();
divP = parseInt(divP);
if (isNaN(divP)) {
alert("Not a number!");
} else {
divP++;
$("div p").text(divP);
}
}
Upvotes: 1