我的代码无效,我需要从列表中找到最小值,我必须使用,但由于某种原因它并不总是有效
<script>
/* Example one */
function findMin()
{
list = numbersBox.value.split(","); // gets the input into an array form by splitting on ","
var min = list[0]; // assuming initially that min element is the first element
for(var i = 0; i < list.length; i++) // for loop to go over all list elements starting from 0 to list.length
{
if(list[i] < min) min = list[i]; // setting the min value appropriately
}
minBox.value = min; // setting answer into the minBox
}
</script>
<label>Write some numbers separated by commas:</label>
<br><br>
<input type="text" id="numbersBox" />
<br><br>
<label>Minimum Value:</label>
<br><br>
<input type="text" id="minBox" />
<br><br>
找到最小
答案 0 :(得分:0)
您可以使用Math.min()
:
<label>Write some numbers separated by commas:</label>
<br><br>
<input type="text" id="numbersBox" value="9,6,12,67"/>
<br><br>
<label>Minimum Value:</label>
<br><br>
<input type="text" id="minBox" />
<br><br>
<button onclick="findMin()">Find min</button>
public class makeMyTrip {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\jarFiles\\geckodriver-v0.14.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.makemytrip.com/");
driver.findElement(By.cssSelector("a[href*='holidays-international']")).click();
String MainWindow=driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
driver.close();
// Switching to Parent window i.e Main Window.
driver.switchTo().window(MainWindow);
//driver.findElement(By.xpath("//[@placeholder='Type Departure City']")).sendKeys("New Delhi");
}
}
答案 1 :(得分:0)
尝试以下方法:
function findMin()
{
list = $('#numbersBox').val().split(","); // gets the input into an array form by splitting on ","
var min = list[0]; // assuming initially that min element is the first element
Array.min = function( array ){
return Math.min.apply( Math, array );
};
$('#minBox').val(Array.min(list));
}
$('#btnClick').click(function(){
findMin();
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Write some numbers separated by commas:</label>
<br><br>
<input type="text" id="numbersBox" />
<br><br>
<label>Minimum Value:</label>
<br><br>
<input type="text" id="minBox" />
<input type="button" value="Result" id="btnClick"/>
<br><br>
&#13;