What is wrong with this javascript codes - javascript

see the code.
Thanks for the help everyone. Much appreciated for the valuable feedback. But it haven't helped. I really thank you.
$.get("http://nominatim.openstreetmap.org/search?q=pekin,+china&format=json&polygon=1&addressdetails=1").done(function(data)
{
var aJsonData = new Array();
var iBiggest = 0;
aJsonData = JSON.parse(data);
aData = aJsonData;
for(var i=0; i < aData.length; i++)
{
if(i != 0)
{
if((aData[i].polygonpoints.length) > (aData[iBiggest].polygonpoints.length))
{
iBiggest = i;
}
}
}
alert(iBiggest);
for(var j=0; j < aData[iBiggest].polygonpoints.length; j++)
{
//alert(aData[iBiggest].polygonpoints[j]);
}
});

Your for loop is wrong, the array index will start from 0 to length - 1 so i <= aData.length is wrong.
So the loop should be
$
.get("http://nominatim.openstreetmap.org/search?q=london,+england&format=json&polygon=1&addressdetails=1")
.done(function(data) {
var iBiggest = 0;
for (var i = 1; i < data.length; i++) {
if ((data[i].polygonpoints.length) > (data[iBiggest].polygonpoints.length)) {
iBiggest = i;
}
}
// this is not working
alert(iBiggest);
for (var j = 0; j < data[iBiggest].polygonpoints.length; j++) {
// alert(aData[iBiggest].polygonpoints[j]);
}
}, 'json');
Demo: Fiddle

I solved it.
$.getJSON("http://nominatim.openstreetmap.org/search?q=pekin,+china&format=json&polygon=1&addressdetails=1", function(data)
{
var iBiggest = 0;
for(var i = 1; i < data.length; i++)
{
if(data[i].polygonpoints != 'undefined' && data[i].polygonpoints)
{
if((data[i].polygonpoints.length) > (data[iBiggest].polygonpoints.length))
{
iBiggest = i;
}
}
}
alert(iBiggest);
for(var j = 0; j < data[iBiggest].polygonpoints.length; j++)
{
alert(data[iBiggest].polygonpoints[j]);
}
});

Related

can anyone explain what are these functions doing

function deleteParentNodeOnClick() {
for (var i = 0; i < deleteButton.length; i++) {
deleteButton[i].addEventListener("click", deleteNodeOnClick);
}
}
function deleteNodeOnClick(e) {
var trash = document.querySelectorAll("i");
for (var ind = 0; ind < trash.length; ind++) {
console.log(e);
this.parentNode.parentNode.remove();
}
}

Choose item by name and color

I want my bot to choose an item from this site https://www.supremenewyork.com/shop/all/t-shirts, by name and color, but I was able to make it work only when it's choosing by name or color, not both. The code of it looks like this
function pickItem() {
chrome.storage.sync.get("itemName", function(data) {
let items = document.getElementsByClassName("name-link");
for(i = 0; i < items.length; i++) {
if ((items[i].innerHTML).includes(data.itemName)) {
chrome.runtime.sendMessage({redirect: items[i].href});
break;
}
}
})
}
This code is supposed to choose both name and color, but isn't working. I would be very thankful for any hints.
function pickItem() {
let items = document.getElementsByClassName("name-link");
chrome.storage.sync.get(["itemName", "color"], function(data) {
for(i = 0; i < items.length; i++) {
if ((items[i].innerHTML).includes(data.itemName)) {
var name_item_found = items[i];
for(j= 0; j < name_item_found.length; j++) {
if((name_item_found[j].innerHTML).includes(data.color)) {
chrome.runtime.sendMessage({redirect: name_item_found[j].href});
break;
}
}
}
}
})
}
I've found a solution, so I'm posting it here if someone has the same problem in the future.
function pickItem() {
let items = document.getElementsByClassName("name-link");
chrome.storage.sync.get(["itemName", "color"], function(data) {
for(i = 0; i < items.length; i++) {
if(items[i].innerHTML == data.itemName) {
for(j = 0; j < items.length; j++) {
if(items[j].innerHTML == data.color) {
if(items[i].href == items[j].href) {
chrome.runtime.sendMessage({redirect: items[i, j].href})
}
}
}
}
}
})
}

How to pass a value from one function to another

var boxes = document.getElementsByName('toggle');
function markPreceding() {
var curIndex = null;
for (var j = 0; j < boxes.length; j++) {
if (boxes[j].checked) {
curIndex = j;
}
}
}
function checkInputs() {
for (var k = 0; k <= curIndex.length; k++) {
boxes[k].checked = true;
}
}
for (var i = 0; i <= boxes.length; i++) {
boxes[i].onchange = markPreceding;
boxes[i].onchange = checkInputs;
}
<input type="checkbox" id="product-1" name="toggle">
<input type="checkbox" id="product-2" name="toggle">
<input type="checkbox" id="product-3" name="toggle">
<input type="checkbox" id="product-4" name="toggle">
<input type="checkbox" id="product-5" name="toggle">
Have a problem passing this "curIndex" value to checkInputs function.
This should check inputs before checked input and get its value to do it.
Only ES5 synthax needed for this project.
EDIT: The ES5 Syntax way
const boxes = document.getElementsByName('toggle');
boxes.forEach(function(box, I) {
box.onclick = function(e) {
markPreceding(i);
};
});
function markPreceding(i) {
for (var j = 0; j < boxes.length; j++) {
if(j <= i) {
document.getElementById('product-' + (j + 1)).checked = true;
} else {
document.getElementById('product-' + (j + 1)).checked = false;
}
}
}
ORIGINAL:
Try using this:
const boxes = document.getElementsByName('toggle');
boxes.forEach((box, i) => {
box.onclick = (e) => {
markPreceding(i);
};
});
function markPreceding(i) {
for (var j = 0; j < boxes.length; j++) {
if(j <= i) {
document.getElementById(`product-${j + 1}`).checked = true;
} else {
document.getElementById(`product-${j + 1}`).checked = false;
}
}
}
For some reason, there seems to be an issue with updating the inputs through the NodeList array returned by document.getElementsByName. Not sure why, but this code has been verified. See working example here.

finding and placing a word from the text in an array

I am trying to find a word in the text with my name. The code requires me to first find the first character of the word and then subsequently push the remaining letters in the hits[] array. I am trying but got stuck.
var text = "This is just Rohit.";
var myName = "Rohit";
var hits = [];
for (var i = 0; i < text.length; i++)
{
if(text[i] === myName[0] )
{
for(j = 0; j < myName.length; j++)
{
hits.push(text[i]);
};
};
};
hits;
var text = "This is just Rohit.";
var myName = "Rohit";
var hits = [];
for (var i = 0; i < text.length; i++) {
if(text[i] === myName[0] ) {
for(var j = i, l = 0; l < myName.length; j++, l++) {
hits.push(text[j]);
}
}
}
console.log(hits.join(''));
Just ignore the searchtext and go with the searchterm.
var hits = [];
var i=0;
while (i<"Rohit".length) arr.push("Rohit"[i++]);
console.log(hits);
// ["R", "o", "h", "i", "t"]
Something like this should help.Let me know if a change is needed.
var text = "This is just Rohit.";
var myName = "Rohit.";
var hits = [];
var array1=text.split(" ");
for (var i = 0; i < array1.length; i++)
{
if(array1[i]==myName)
{
hits.push(array1[i].split(''));
}
}
THIS PART IS THE METHOD WHICH YOU WERE USING::
var text = "This is just Rohit.";
var myName = "Rohit";
var hits = [];
var x='';
for (var i = 0; i < text.length; i++)
{
if(text[i] === myName.charAt(0) && text[i-1]==" " )
{
for(j = i; j < (i+myName.length); j++)
{
console.log(text[j]);
x=x+text[j];
}
}
}
hits[0]=x;
alert(hits);
After looking at your comments for clarification, it looks like you just want your name as the only element in a new array.
Use this:
var text = 'Your name is Rohit.';
var name = 'Rohit';
var hits = [text.substring(text.indexOf(name), text.indexOf(name) + name.length)];
console.log(hits);
// ["Rohit"]
Can't imagine how this is helpful, but the code above will do what you want.
I solved it.
var text = "This is just Rohit.";
var myName = "Rohit";
var hits = [];
for (var i = 0; i < text.length; i++)
{
if(text[i] === myName[0] )
{
for(var j = i; j < (myName.length+i); j++)
{
hits.push(text[j]);
};
};
};
hits;

How to ignore an event in javascript?

So, I'm new to javascript.
My code is the following, it is based on a xaml file with a canvas and a couple of borders in it:
var defaultPage = null;
var aantalKliks;
var correcteBorders;
var incorrecteBorders;
var geenAntwBorders;
function onLoaded() {
defaultPage = document.getElementById('DefaultPage');
alert('In onloaded van Default.xaml.');
aantalKliks = 0;
aantalBorderKliks = 0;
correcteBorders = new Array();
for (var i = 0; i < 3; i++) {
correcteBorders[i] = defaultPage.content.findName('CorrecteBorder' + i);
}
incorrecteBorders = new Array();
for (var i = 0; i < 3; i++) {
incorrecteBorders[i] = defaultPage.content.findName('IncorrecteBorder' + i);
}
geenAntwBorders = new Array();
for (var i = 0; i < 3; i++) {
geenAntwBorders[i] = defaultPage.content.findName('GeenAntwBorder' + i);
}
}
function OnCanvasClicked() {
if (aantalKliks == 2) {
aantalKliks = 0;
}
if (aantalKliks == 0) {
for (var i = 0; i < correcteBorders.length; i++) {
correcteBorders[i].Visibility = 'Visible';
}
for (var i = 0; i < incorrecteBorders.length; i++) {
incorrecteBorders[i].Visibility = 'Visible';
}
for (var i = 0; i < geenAntwBorders.length; i++) {
geenAntwBorders[i].Visibility = 'Visible';
}
} else if (aantalKliks == 1) {
for (var i = 0; i < correcteBorders.length; i++) {
correcteBorders[i].Visibility = 'Collapsed';
}
for (var i = 0; i < incorrecteBorders.length; i++) {
incorrecteBorders[i].Visibility = 'Collapsed';
}
for (var i = 0; i < geenAntwBorders.length; i++) {
geenAntwBorders[i].Visibility = 'Collapsed';
}
aantalKliks++;
}
function borderClicked(sender) {
for (var i = 0; i < correcteBorders.length; i++) {
correcteBorders[i].Visibility = 'Collapsed';
}
for (var i = 0; i < incorrecteBorders.length; i++) {
incorrecteBorders[i].Visibility = 'Collapsed';
}
for (var i = 0; i < geenAntwBorders.length; i++) {
geenAntwBorders[i].Visibility = 'Collapsed';
}
sender['Visibility'] = 'Visible';
}
The function OnCanvasClicked is triggered when I click anywhere in the canvas and makes all borders disappear/reappear. The function borderClicked is triggered when I click a specific border. The function borderClicked does trigger when I click a specific border, however the OnCanvasClicked function also gets executed right after, which causes an unwanted result.I think I need some way to ignore the OnCanvasClicked function if I click on a border, I did google this but to be honest I didn't really understand what they meant in most of the solutions, so I was hoping someone could explain it to me in a simple way what I need to do (and what I'm doing).
You need to set event.stopPropagation() when borderClicked function is fire
Try this which will prevent Javascript to further execution
event.preventDefault()
#Harshit is correct
You need to set event.stopPropagation() when borderClicked function is fire
I just wanted to add this link/sample which I found very usefull to understand bubbling
http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/ie9_event_phases.htm

Categories