Parse.com Overwrites my results when Javascript [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm not sure what am I doing wrong.
When running the attached code I would expect to see 100 rows inside Parse.com.
Instead, I only see only one.
For some reason, Parse.com is overwriting the same line 100 times.
And ideas? :/
Me.
function simplewrite() {
Parse.initialize("********************, ***********************");
var Numbers = Parse.Object.extend("Numbers");
var numbers = new Numbers();
for (var i=0; i<100; i++) {
numbers.set("number", i);
numbers.save(null, {
success: function(numbers) {
console.log("YES " + i);
},
error: function(numbers, error) {
console.log("no! " + i);
console.log(error);
}
}); // End
};
};

Your for loop is updating the same numbers variable over and over. Try moving the initialization of numbers to inside your for loop:
// ...
for (var i=0; i<100; i++) {
var numbers = new Numbers();
// ...

Related

Jquery loop compare not getting expected result [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This is my code. I will explain everything step by step :
myobj ={ month: 1, total: 2 }{ month: 3, total: 1 }
newArray = [];
for(i=0;i<=5;i++){
$.each(myobj.function(k,v){
if(i==v.month){
newArray.push(v.month)
}else{
newArray.push(0)
}
})
}
After what I am getting is : 1,0,0,0,0,3,0,0,0,0
expected output : 1,0,3,0,0
I don't know what I am missing here. Can anyone please help me related this? I am stuck here
Your inner loop is pushing onto the new array for every item in the array, not just if the desired month is found.
Don't use an inner loop. Use find() to find the matching month, and push 0 if you don't find it.
for (i = 1; i <= 5; i++) {
if (myobj.find(el => el.month == i)) {
newArray.push(i);
} else {
newArray.push(0);
}
}
if you want to push the totals instead of the months, assign the result of find() to a variable so you can get the total from it.
for (i = 1; i <= 5; i++) {
var found = myobj.find(el => el.month == i);
newArray.push(found ? found.total : 0);
}

Accessing An Array from within a function using JavaScript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I searched the forum but didn't see a similar question or answer for the question I am about to ask, however if it is duplicated please provide a link and I will view the answer in that post.
Is it possible to access objects in a array from within a closure/ function? I am attempting to do so for the purpose of experimentation.
Here is the code I put together in firebug. I am receive 'undefined'.
var checkers = [1, "string", null];
var i, txt = "";
function myFunction(checkers){
for(var i = 0; i < checkers.length; i++ ){
txt += checkers[i] + " ";
console.log(txt)
}
}
myFunction(checkers);
Absolutely possible, one of JavaScript's strenghts.
Your issue is that you aren't calling your function. Also you don't need a parameter to myFunction since it closes over the context and knows about checkers.
Just delete the paramter and after your code add myFunction() to call it.
var checkers = [1, "string", null];
var i, txt = "";
function myFunction(){
for(var i = 0; i < checkers.length; i++ ){
txt += checkers[i] + " ";
console.log(txt)
}
}
myFunction();
The reason you are getting undefined is because theres not value returned to the console when declaring a function, just like you'd get undefined if you ran:
var t = 'asd'
var checkers = [1, "string", null];
var i, txt = "";
function myFunction(checkers){
for(var i = 0; i < checkers.length; i++ ){
txt += checkers[i] + " ";
console.log(txt)
}
}
myFunction(checkers);
I just ran it and I get:
1
1 string
1 string null
undefined
Since the function doesn't return anything, Firebug displays undefined at the end.

Javascript - Passing from a DOM element to array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Hello and thank you for your time.
Here is the code :
<script>
var names = document.getElementsByClassName('xx');
var ar = [];
for (var i = 0, c = names.length ; i < c ; i++) {
ar[i] = names[i].innerHTML;
alert(ar[i]);// the results are : undefined
}
</script>`
I've tried to use the method tostring, or to push the results into the array but without success.
Thanks
Your main issue seems to be fixed. Make sure the DOM has been loaded before you try to run your code, and there is no need for two variables in your loop. Simplify it like below:
window.onload = function () {
var names = document.getElementsByClassName('xx');
var ar = [];
for (var i = 0 ; i < names.length ; i++) {
ar[i] = names[i].innerHTML;
alert(ar[i]);
}
};
Fiddle
ar.length equals 0, because you just declare the array, but dont put anything into it. I think what you wanted to do is the following:
var names = document.getElementsByClassName('xx');
var ar = [];
for (var i = 0 ; i < names.length ; i++) {
ar[i] = names[i].innerHTML;
alert(ar[i]);
}

Iterating through an array returns null when using getElementbyID [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm trying to loop through a array of six numbers and modify a corresponding form element like so..
for(var x in numbers) { document.getElementById('r' + numbers[x]).value++; }
However this doesn't update the text field and the debugger says Type Error: document.GetElementById(...) is null. However the following code works as expected
var t = 25;
document.getElementById('r' + t).value++;
Can anyone shed any light on this? I'm not really a javascript programmer and am cobbling something together from code examples and I just can't see why the second case works but not the first.
ps. And yes I've tried with the more correct for (var i=0; i < numbers.length; i++) {} format ;)
If this setup in jsbin is similar to what you have and it doesnt work...I'd suspect that your code is not finding the element in the DOM and you need to run the code when the DOM has actually loaded.
http://jsbin.com/fasibuse/1/edit?html,js,output
I think a more generic solution that would do the same thing ( increase the elements by 1 )...would be
var formElements = document.forms[0].getElementsByTagName( 'input' );
for (var el of formElements) {
el.value++;
}
Read code directly:
var numbers = [10,20,30,40];
for(var x in numbers) {
console.log(x); // <-- output 0,1,2,3 (the indexes, not the content)
}
You should use
numbers.forEach(function (x) {
document.getElementById('r' + numbers[x]).value++;
});
Something like this should work:
<!DOCTYPE html>
<html>
<body>
<input id="sparky1" value="1"><br>
<input id="sparky2" value="3"><br>
<input id="sparky3" value="5"><br>
<input id="sparky4" value="7"><br>
<input id="sparky5" value="9"><br>
<script>
for (var i=1; i < 6; i++) {
document.getElementById('sparky' + i).value = parseInt(document.getElementById('sparky' + i).value) + 1;
}
</script>
</body>
There is invalid access of array element, here is correct one. try this one.
for(var x in numbers) { document.getElementById('r' + x).value++; }
this will work fine.

Alert a line of code from a JavaScript file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a JavaScript error log and one of the columns is the line number where the error occurred. I think it would save me time from having to go through the source from the browser's perspective if I could just click the link and get a JavaScript alert with that line (or lines) of code.
So in short: how can I retrieve a specific line or lines of code from a JavaScript file that is included via a script element in the head of the code?
I don't use frameworks.
Modern day browsers ship with a debugging console. They log the error message and a stack trace whenever errors happen. Line numbers for the error and involved functions in the stack are usually provided and are clickable to take you the the line where it happened.
This solution isn't perfect as it creates an unnecessary request to the server however it does exactly what I wanted to do...
function getLines(haystack,from,toIncluding)
{
var i = 0
var j = 0;
var result = '';
--from;
while (from-- >0 && i !== -1)
{
--toIncluding, i = haystack.indexOf('\n', i + 1);
}
if (i===-1) {result = '';}
j = i;
while (toIncluding-->0 && j !== -1)
{
j = haystack.indexOf('\n', j + 1);
}
if (j === -1) {j = haystack.length;}
result = haystack.slice(i + 1, j);
return result;
}
...and then simply use...
if (window.XMLHttpRequest)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET',document.getElementsByTagName('script')[0].src,true);
xmlhttp.send(null);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState=='4')
{
alert(getLines(xmlhttp.responseText,2,7));
}
}
}

Categories

Resources