您的位置:首页 > 编程语言 > PHP开发

php和js中数组中分别循环找出最大的数。

2015-07-16 21:47 721 查看
<html>

<?php

header("Content-type: text/html; charset=utf-8");

function test($arr){

$maxvalue = $arr[0];

$index = 0;

for($i = 1;$i<count($arr);$i++){

if($maxvalue<$arr[$i]){

$maxvalue = $arr[$i];

$index = $i;

}

}

return $maxvalue;

}

$arr = Array(1,5,3);

$max = test($arr);

echo $max;

?>

<body>

<script type ="text/javascript">

function test(arr){

var max = arr[0];

var index = 0;

for(var i=1;i<arr.length;i++){

if(max<arr[i]){

max = arr[i];

index=i;

}

}

return max;

console.log(max);

}

array = [3,9,5];

document.write(test(array));

</script>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: