js配列の並び替えと再帰的
1281 ワード
//
var arr = [7, 4, 6, 8, 9, 13, 2, 3, 5, 4, 6];
Array.prototype.unique = function () {
var arr = [];
for (var i = 0; i < this.length; i++) {
if (arr.indexOf(this[i]) == -1) {
arr.push(this[i]);
}
}
return arr;
}
document.write(" :"+arr.unique() + "
");
//配列の並べ替え
var arr2 = [7, 4, 6, 8, 9, 13, 2, 3, 5];
function sortArr(arr) {
var temp = "";
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length - 1-i;j++)
{
if (arr[j] > arr[j + 1]) {
temp = arr[j+1];//4
arr[j + 1] = arr[j];//7
arr[j] = temp;//4
}
}
}
return arr;
}
document.write(" :"+sortArr(arr2)+"
");
//再帰する
function f(n) {
if (n==0)
{
return 0;
}
else if (n==1)
{
return 1;
}
else
{
return f(n-1)+f(n-2);
}
}
document.write(" :"+f(5));