Javascript循环来比较不同的数字

时间:2017-01-11 15:13:03

标签: javascript loops comparison

我从stackoverflow搜索了这类帖子,但我还不是很熟悉JS,所以我还是不确定如何解决我的问题。 事情是我有两个阵列(第一个包括“名称”,另一个包括“高度”)。我做了一个循环,在网页上显示有关它们的一些信息。但现在我想制作一个比较这些值的循环。 我知道一个解决方案就是我写,如果,否则,如果,其他句子,但似乎不是很聪明...... 我的代码看起来像这样:

<!doctype html>
<html>
<head></head>
<body>
<p id="test2">...</p>
<p id="test3">...</p>
<p id="test4">...</p>
<p id="test5">...</p>
<script>

// Arrays. 1st with names. 2nd with heigths in cm.

var names = ["name1", "name2", "name3", "name4"];
var height = [150, 165, 160, 165, 170];

/* Loop to show some info on page.
E.g name1 is 150cm
name2 is 155cm */

var text = "";

for(var i = 0; i < (names.length); i++) {
text += names[i] + " is " + height[i] + "cm tall <br>";
} 
document.getElementById("test2").innerHTML = text;

// Comparison by heights
var a = height[0];
var b = height[1];
var c = height[2];
var d = height[3];

if(a < b) {
document.getElementById("test3").innerHTML = names[0] + " is shorter than " + names[1];
} 
else if (a > b){
document.getElementById("test3").innerHTML = names[0] + " is taller than " + names[1];
}
else {
    document.getElementById("test3").innerHTML = names[0] + " is equal to " + names[1] + " height";
}

if(b < c) {
document.getElementById("test4").innerHTML = names[1] + " is shorter than " + names[2];
} 
else if (b > c){
document.getElementById("test4").innerHTML = names[1] + " is taller than " + names[2];
}
else {
    document.getElementById("test4").innerHTML = names[1] + " is equal to " + names[2] + " height";
}
</script>
</body>
</html>

我已经尝试了一些我想到的解决方案,但由于“不是那种非常好的javascript知识”,我仍然不知道该怎么做...... 如果有人可以帮助我,我将非常感激。

3 个答案:

答案 0 :(得分:1)

一个简单的解决方案是创建一个比较两个高度的函数,然后输出一个可以分配给DOM元素的字符串。您可能还希望将信息存储在对象中,这样您就不会有多个数组:

&#13;
&#13;
function comparePeople(person1, person2) {
  if (person1.height < person2.height) {
    return person1.name + " is shorter than " + person2.name;
  } else if (person1.height > person2.height){
    return person1.name + " is taller than " + person2.name;
  } else {
    return person1.name + " is equal to " + person2.name + " height";
  }
}

var people = [
  { name: 'name1', height: 150 },
  { name: 'name2', height: 165 },
  { name: 'name3', height: 160 },
  { name: 'name4', height: 165 }
];

var text = "";
people.forEach(function(person) {
  text += person.name + ' is ' + person.height + 'cm tall <br>';
});

document.getElementById("test1").innerHTML = text;
document.getElementById("test2").innerHTML = comparePeople(people[0], people[1]);
document.getElementById("test3").innerHTML = comparePeople(people[1], people[2]);
&#13;
<p id="test1">...</p>
<p id="test2">...</p>
<p id="test3">...</p>
<p id="test4">...</p>
&#13;
&#13;
&#13;

现在您需要做的就是使用循环并调用comparePeople

people.forEach(function(person1) {
  people.forEach(function(person2) {
    if (person1 !== person2) {
      comparePeople(person1, person2); // do something with this value
    }
  });
});

答案 1 :(得分:0)

您可以将每个元素与Sub ripitems(ByVal elementCollection As System.Collections.ObjectModel.ReadOnlyCollection(Of IWebElement), ByVal fHandle As TextWriter) '[... irrelevant code to find items and prices ...] For i = 0 To elementCollection.Count - 1 fHandle.Write(thisPN(i) & "," & thisPrice(i)) Next End Sub 的每个元素进行比较,然后打印出它的结果。

height
function append(string) {
    var node = document.getElementById('result');
    node.appendChild(document.createTextNode(string));
    node.appendChild(document.createElement('br'));
}

var names = ["name1", "name2", "name3", "name4"];
var height = [150, 165, 160, 165, /* 170 no name */];
var text = "",
    i, j;

for (var i = 0; i < (names.length); i++) {
    text += names[i] + " is " + height[i] + "cm tall <br>";
}

document.getElementById("test2").innerHTML = text;

for (i = 0; i < height.length - 1; i++) {
    for (j = i + 1; j < height.length; j++) {
        if (height[i] < height[j]) {
            append(names[i] + " is shorter than " + names[j]);
        } else if (height[i] > height[j]) {
            append(names[i] + " is taller than " + names[j]);
        } else {
            append(names[i] + " is equal to " + names[j]);
        }
    }
}

答案 2 :(得分:0)

你可以使用这样的对象数组:

<!doctype html>
<html>
<head></head>
<body>
<p id="test1">...</p>
<p id="test2">...</p>
<p id="test3">...</p>
<p id="test4">...</p>
<p id="test5">...</p>
<script>

// Arrays. 1st with names. 2nd with heigths in cm.

var names = ["name1", "name2", "name3", "name4"];
var height = [150, 165, 160, 165, 170];

var obj = [
    {id: 1, name: "name1", compareId: 2, height: 150, resultId: 3},
    {id: 2, name: "name2", compareId: 3, height: 165, resultId: 4},
    {id: 3, name: "name3", compareId: 4, height: 160, resultId: 5},
    {id: 4, name: "name4", compareId: 1, height: 165, resultId: 1}
]

var text = "";

for(var i = 0; i < (obj.length); i++) {
    text += obj[i].name + " is " + obj[i].height + "cm tall <br>";
}
document.getElementById("test2").innerHTML = text;

for(var i = 0; i < (obj.length); i++) {
        var item = getById(obj[i].compareId);
        if(obj[i].height < item.height)
            document.getElementById("test" + obj[i].resultId).innerHTML = obj[i].name + " is shorter than " + item.name;
        else if(obj[i].height > item.height)
            document.getElementById("test" + obj[i].resultId).innerHTML = obj[i].name + " is taller than " + item.name;
        else 
            document.getElementById("test" + obj[i].resultId).innerHTML = obj[i].name + " is equal than " + item.name;
}

function getById (id) {
    for(var i = 0; i < (obj.length); i++) {
        if  (obj[i].id === id)
            return obj[i];
    }
}

</script>
</body>
</html>