Really sorry if you've gone over this already with me. The good news: I've made progress. The bad news: it's broken progress.
I have a function that counts up from a certain number. It's a big number, and I need to insert commas in the correct places. Here's the code I've put together. It works at first, but then displays NaN...
http://jsfiddle.net/blackessej/TT8BH/7/
function createCounter(elementId,start,end,totalTime,callback)
{
var jTarget=jQuery("#"+elementId);
var interval=totalTime/(end-start);
var intervalId;
var current=addCommas(start)+'';
var f=function(){
jTarget.text(current);
if(current==end)
{
clearInterval(intervalId);
if(callback)
{
callback();
}
}
++current;
}
intervalId=setInterval(f,interval);
f();
}
jQuery(document).ready(function(){
createCounter("counter",12714086,9999999999,10000000000000,function(){
alert("finished")
})
})
function addCommas(str) {
var amount = new String(str);
amount = amount.split("").reverse();
var output = "";
for ( var i = 0; i <= amount.length-1; i++ ){
output = amount[i] + output;
if ((i+1) % 3 == 0 && (amount.length-1) !== i)output = ',' + output;
}
return output;
}
The problem comes from adding the commas to the number. Just convert the number to the comma string before putting it in the .text() function:
http://jsfiddle.net/TT8BH/11/
When you call addCommas, you are returning a String, nicely formatted for humans, but terrible for things like ++. You need to track and modify the start variable, and only convert to string when necessary:
var f=function(){
jTarget.text(addCommas(start));
if(start==end)
{
clearInterval(intervalId);
if(callback)
{
callback();
}
}
++start;
}
Related
I am new to JS, so I have couple of questions.
In JS, how can we calculate memory consume by code/code-block and which block is taking what time. Any articles/blogs will be big help.
Below code execute successfully but exceeds execution time. How can i reduce the execution time of the code. Any tips will be helpful.
Problem Statement : given a range L to T. Find such numbers whose all digits are a multiple of K.
Input for code:
The first line contains the number of test cases.
Next lines contain integers. (L T K)
5
5 10 5
20 40 2
300 600 3
process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";
process.stdin.on("data", function (input) {
stdin_input += input; // Reading input from STDIN
});
process.stdin.on("end", function () {
main(stdin_input);
});
function main(input) {
// process.stdout.write("Hi, " + input + ".\n"); // Writing output to STDOUT
input = input.split('\n');
// console.log(input);
let T = parseInt(input[0])+1;
for(var i=1;i<T;i++){
let d=input[i].split(' ');
calc(parseInt( d[0]),parseInt( d[1]),parseInt( d[2]));
}
}
function calc(T,L,k){
ar=[];
for(let i = T;i<=L;i++){
if((i>0 && i<10)&& (i%k==0) ){
ar.push(i);
} else {
splitNumber(i,k) ? ar.push(i):'';
}
}
console.log(ar.length);
}
function splitNumber(num,k)
{
let bool=false;
num= splitToDigit(num);
for(let i =0;i<num.length;i++){
if(num[i]%k == 0 ){
bool=true;
} else{ return false; }
}
return bool;
}
function splitToDigit(n){
return [...n + ''].map(Number);
}
I am trying to using a for loop for trying to validate the input of the user and this is the code i got.
function Valid() {
objfieldid = ["userMail", "userCont"]
objboxid = ["cancelMail", "cancelCont"]
return objfieldid.every(callnonvalid)
}
function callnonvalid(id) {
var valid = false
var objlength = objfieldid.length
objlength--;
for (var i = objlength; i >= 0; i--){
var cobj = document.getElementById(objboxid[i]).checked;
if (document.getElementById(id).value != "" ){
var obj = document.getElementById(id).value;
} else if (cobj == true) {
alert(i); //return 1, 1
return true
} else {
return false
}
}
}
As you can see, in the code, the for loop is running twice. but the i variable is left unchanged. Why would this happen?
btw, I did read different material about closure and i am sure there didnt got a closure problem
EDIT:guys please note that i did noticed the array is zero based, and i did minus the objlength by one.
Mistakes were found after checking the code carefully. The Mistake that I made was that I should not use the return for the out since that would stop the function from working, however that array.every Called the function twice which make the i not decreasing
I'm not sure why you're decrementing in your loop, because the performance gain would be infinitesimally small (it may even be slower, e.g. in Chrome/V8) and working in reverse order can get confusing further down the line, but that's your choice and I don't know enough about what you're doing to judge.
Either way, I don't think you'd want to decrement objlength before the loop begins as you are doing now. The whole point of a loop is to handle the incrementing/decrementing in the condition statement of the loop.
You would only decrement manually like that if you were going to move your if/else if/else statement into a closed over function and execute it recursively, decrementing the objlength from within the closure. Which would work, but it's unnecessarily complicated for what you're doing and you would gain nothing for rewriting the whole thing.
So, sticking with the looping approach, perhaps try either of these:
function Valid() {
objfieldid = ["userMail", "userCont"];
objboxid = ["cancelMail", "cancelCont"];
return objfieldid.every(callnonvalid);
}
function callnonvalid(id) {
var valid = false;
var objlength = objfieldid.length;
for(var i = 0; i < objlength; i++){
var cobj = document.getElementById(objboxid[i]).checked;
if (document.getElementById(id).value != "" ){
var obj = document.getElementById(id).value;
} else if (cobj == true) {
alert(i);
return true;
} else {
return false;
}
}
}
or, if you want to decrement, use while instead of for:
function Valid() {
objfieldid = ["userMail", "userCont"];
objboxid = ["cancelMail", "cancelCont"];
return objfieldid.every(callnonvalid);
}
function callnonvalid(id) {
var valid = false;
var i = objfieldid.length;
while(i--){
var cobj = document.getElementById(objboxid[i]).checked;
if (document.getElementById(id).value != "" ){
var obj = document.getElementById(id).value;
} else if (cobj == true) {
alert(i);
return true;
} else {
return false;
}
}
}
Because the array objboxid[] has only two elements, the first time through your loop objboxid[2] will be attempting to fetch an array index that is out-of-bounds.
You probably meant something like:
for (var i = objlength; i > 0; i--){
var cobj = document.getElementById(objboxid[i-1]).checked;
or perhaps
for (var i = objlength-1; i >= 0; i--){
var cobj = document.getElementById(objboxid[i]).checked;
Why am I getting a RangeError: Maximum call stack exceeded error? I am trying to parse through text to find math and solve it. It was working until I started to implement parenthesis'. I have tried to find the error but I just can't figure it out.
My Code:
var alg = {
calc: function(eq, solveFor) {
var out;
var sideOne = eq.substring(0, eq.indexOf('='))
var sideTwo = eq.substring(eq.indexOf('=') + 1)
if (sideOne === solveFor) {
alg.simplify(sideTwo);
}
if (sideTwo === solveFor) {
alg.simplify(sideOne);
}
},
simplify: function(eq) {
str = $.trim(eq);
if (str == undefined) {
console.error('Error: null string')
} else {
var charMatch = /^[\d\*\/\+\-\^\(\) ]+$/
if (charMatch.exec(str) === null) {
console.error('Error: Invalid char/expression')
} else {
alg.parMath('not');
alg.expRoot(solve);
alg.multDiv(solve);
alg.addSubtr(solve);
}
}
},
fromPar: function(par) {
alg.parMath(par);
alg.expRoot(solve);
alg.multDiv(solve);
alg.addSubtr(solve);
},
parMath: function(source) {
var reP = /\(([\d\*\/\+\-\^\(\) ]+)\)/
var exP = reP.exec(str)
if (source === 'par') {
str = str.replace(exP[0], solve)
}
if (exP !== null) {
use = 'par'
solve = exP[1]
} else {
use = 'not'
solve = str;
}
},
expRoot: function() {
var fracCon = /(\d+)\/(\d+)/
var reER = /(\d+)(\^)(\d+(\/\d)?)(?!\/)/
var exER = reER.exec(solve)
if (exER !== null) {
var exFC = fracCon.exec(exER[3])
if (exFC !== null) {
var rep = Math.pow(parseFloat(exER[1]),(parseFloat(exFC[1]) / parseFloat(exFC[2])))
} else {
var rep = Math.pow(parseFloat(exER[1]),parseFloat(exER[3]))
}
solve = solve.replace(exER[0], rep)
if (reER.exec(solve) !== null) {
alg.expRoot();
}
}
},
multDiv: function() {
var reMD = /(\d+(?:\.\d+)?) *([\*|\/]) *(\d+(?:\.\d+)?)/
var exMD = reMD.exec(solve);
if (exMD !== null) {
if (exMD[2] === "*") {
var rep = parseFloat(exMD[1]) * parseFloat(exMD[3]);
var rep = Math.round(rep * 1000000) / 1000000;
} else {
var rep = parseFloat(exMD[1]) / parseFloat(exMD[3]);
var rep = Math.round(rep * 1000000) / 1000000;
}
if (use !== 'par') {
solve = solve.replace(exMD[0], rep);
}
if (reMD.exec(solve) !== null) {
alg.multDiv();
}
}
},
addSubtr: function() {
var reAS = /(\d+(?:\.\d+)?) *([\+|\-]) *(\d+(?:\.\d+)?)/
var exAS = reAS.exec(solve); //Getting RangeError here
if (exAS !== null) {
if (exAS[2] === "+") {
var rep = parseFloat(exAS[1]) + parseFloat(exAS[3])
var rep = Math.round(rep * 1000000) / 1000000
} else {
var rep = parseFloat(exAS[1]) - parseFloat(exAS[3])
var rep = Math.round(rep * 1000000) / 1000000
}
if (use !== 'par') {
str = str.replace(exAS[0], rep)
}
if (exAS !== null) {
alg.addSubtr(solve);
} else {
if (use == 'not') {
out = solve;
} else {
alg.fromPar('par')
}
}
} else {
if (use == 'not') {
out = solve;
} else {
alg.fromPar('par')
}
}
}
};
console.log(alg.calc('x=(1+1)', "x"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I'm getting the error at the start of addSubtr function (marked by a comment). Can anyone help me find how to fix the error?
The problem is that your code goes into an infinite loop. Here is the relevant part of the logic
addSubtr: function() {
/* ommitted */
var exAS = reAS.exec(solve); //Getting RangeError here
if (exAS !== null) {
/* ommitted - logic here*/
if (exAS !== null) {
alg.addSubtr(solve);
/* ommitted */
}
}
}
You get the value for exAS by parsing solve through a regex.
If this returns a non-null value you've gotten a match
With that hen you go inside the if condition and do some logic
Still inside there, there is another if statement that checks if the regex matched anything. Now, by definition, this would be true - it can be easily seen with a lot of code removed - the same condition is checked for twice. There is nothing that would change the the outcome between the two ifs.
Since the conditional check passes you recursively call the same function again with the same input.
Because the input is the same, the logic will work the same so steps 1-5 are executed again and the function is called again.
This causes infinite recursion. Well, in reality there is a limit and that's the stack size for JavaScript, which is why you are getting the error. It's a bit misleading, since it's the regex that runs over the call stack size, not the recursive call to addSubtr, else it would have been a bit more clear what is going on.
For how to fix it - you need to restructure the logic so you don't get into infinite loops. I am not sure exactly what is the best way for your case but I'd suggest working it out yourself - it would be a useful exercise regardless. Here are some pointers
In point 4. I made, I mentioned that there was an essentially useless check. I assume that it is supposed to be useful.
- You may have intended the inner if to be outside of the outer one. As it stands now, the two are equivalent so the inner if can just be removed.
- maybe the condition of the inner if is incorrect - it could be that you only sometimes want to do the recursive call, not every time.
- perhaps there was supposed to be something that changes either exAS or solve or both. Thus either the condition would (potentially) yield a different result the second time it's checked, or the function would produce a different result when called recursively (which would make the recursive call useful) or both.
Your main problem is that you have an infinite loop which is here
if (exAS !== null) {
alg.addSubtr(solve);
}
This is a useless check for two reasons. One because as you notcie exAS is defined in addSubtr. So every time you call this function you are resetting the value.
Your other problems are you are create variables with the same names, and relying off of static variables/ void functions.
If pm me i cant help you workout the correct way to structure this this function and all the other functions .
I have a strange problem. I have to implement a function count(s) which inverts the getNumberSequence function that I have already create. (i.e: count(getNumberSequence(x)) == x, for all integers x > 0). I have my function and I have also the logic to resolve the problem but I don't know how to do it. In my case I want to call the previous string of ordered numbers, split them and call the last number. The problem is, how can I call the return of another method? Here are my codes:
function getNumberSequence(number) {
var result = "";
if (number <= 0) {
return result;
} else {
var first = true;
for (i = 1; i <= number; i++) {
if (first) {
first = false;
} else {
result += ", ";
}
result += i;
}
}
return result
}
Basically I need the variable result in this case to call it in the other function.
function count(s) {
var d = s. split(', ');
return d[-1];
}
I know that the second code is wrong but I don't know how to fix it. I have implemented a test that is:
test( "Count", function() {
for (i = 1; i<10000; i = i + 10) {
equal(count(getNumberSequence(i)) , i, "count(getNumberSequence(" +i + ")) should return " + i);
}
I know that the answer could be stupid but I started javascript yesterday for the first time. Hope you can help me. Thanks to everyone
If I understand you correctly you want to pass a number, say 10, into the first function and have that return a string of numbers up to 10 and then for count to read that string and return 10 (as an integer) again? This will do the trick. It takes the string, splits it, and pops out the last number converting it to an integer before it returns it.
function count(seq) {
return parseInt(seq.split(', ').pop(), 10);
}
I could rewrite it like this:
function count(seq) {
// create an array
var arr = seq.split(', ');
// grab the last element (a string)
var lastElement = arr.pop();
// convert the string to an integer
var convertedInteger = parseInt(lastElement, 10);
// return the integer
return convertedInteger;
}
If you wanted to use reverse and grab the first element, do this:
function count(seq) {
return parseInt(seq.split(', ').reverse()[0], 10);
}
Or use shift which does the same thing:
function count(seq) {
return parseInt(seq.split(', ').reverse().shift(), 10);
}
DEMO
Trying to make a simple count up timer in jQuery... this sort of works but is adding the numbers to the end of '0000' and I want it to go '0001' '0002' '0003' etc...
This is all happening in the jQuery onReady scope.
var i = '0000'
var timer = function doSomething ( )
{
i = i+= 1
$('.counter').text(i);
console.log(i);
}
setInterval (timer, 1000 );
Your "i" variable needs to be an integer. You can format it how you like when you want to print it somewhere.
$(document).ready(function() {
var i = 0;
var target = $('.counter');
var timer = function doSomething ( )
{
i++;
var output = pad(i,4);
target.text(output);
console.log(output);
}
setInterval (timer, 1000 );
});
function pad(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
}
Your current code is appending to a string, not addition to a number. It essentially looks like
i = '0000' + 1, i = '00001' + 1, i = '000011' + 1 ...
and so on. You'll need to keep it integer based to continue adding to the number. Here's an example with the formatting it looks like you wanted.
var pad = function(n) { return (''+n).length<4?pad('0'+n):n; };
jQuery.fn.timer = function() {
var t = this, i = 0;
setInterval(function() {
t.text(pad(i++));
}, 1000);
};
$('#timer').timer();
http://jsfiddle.net/jDaTK/
I would do something more like this:
// Make sure Date.now exists in your environment
Date.now = Date.now || function () { return Number(new Date()); };
var time = 0,
start = Date.now(),
intervalId;
intervalId = setInterval(function () {
var seconds, display;
// get the exact time since the timer was started
time = Date.now() - start;
// get the number or seconds, rounded down
seconds = Math.floor(time / 1000);
display = '' + seconds;
// pad the beginning of your display string with zeros
while (display.length < 4) {
display = "0" + display;
}
console.log(display);
}, 100);
setInterval is not exact. This code ensures that while the display could be up to nearly a second off (in theory), the actual time you are tracking is always the exact amount of time that has elapsed since you started the timer. But this code would update the display about once every tenth of a second, so it's not likely to ever be off more than a few milliseconds.
From here you can figure out smarter ways to update the display to ensure you have the level of accuracy you need. If this needs to be pretty accurate, then you could make sure you are displaying to the tenth of the second.
I really recommend the jQuery CountUp plugin. I tried a number of Javascript counters and this was one of the easiest to implement and came with lots of goodies:
<script type="text/javascript">
$(document).ready(function(){
$('#counter').countUp({
'lang':'en', 'format':'full', 'sinceDate': '22/07/2008-00::00';
});
});
</script>
<div id="counter"></div>