I'm trying to get the minimum value from an Array, and i come across this weird behavior. My expectation is to get 89 as the minimumDistance, instead i get 100. Can anyone explain this ?
// List
var nodes = {
"node": [
{
"name": "test",
"id": "2",
"edges": {
"edge": [
{
"to": "4",
"distance": "89"
},
{
"to": "6",
"distance": "100"
},
{
"to": "8",
"distance": "500"
}
]
}
}]
}
// Initialization
startNode = 0
currentNode = startNode;
edge = nodes.node[currentNode].edges.edge;
var minimumDistance = 99999;
// Loop through the Neighbors of one Node
for (x= 0; x<edge.length; x++) {
if (edge[x].distance < minimumDistance) {
minimumDistance = edge[x].distance;
}
document.write('Neighbor: ' + edge[x].to + ', Distance: ' + edge[x].distance);
document.write('</br>');
}
document.write('</br>');
document.write('Minimum Distance: ' + minimumDistance );