作业

// 打印菱形、空心菱形,行数做参数



function rhombus(rowCout) {
    if (rowCout <= 0 || rowCout % 2 == 0) {
        return;
    }
    for (i = 1; i <= rowCout; i++) {
        let line = ' ';
        let blackCount = Math.abs((Math.ceil(rowCout / 2) - i));
        for (let j = 1; j <= blackCount; j++) {
            line += ' ';
        }
        // 4 3 2 1 2 3 4
        // 1 2 3 4 3 2 1
        // 1 3 5 7 5 3 1
        let startCount = 
        (Math.ceil(rowCout / 2) - blackCount) * 2 - 1;
        for (let j =1; j <= startCount; j++) {
            line += '*';
        }
        console.log(line);
    }
    return;
}
rhombus(11);
console.log(rhombus(11));

 

// n位数逆转

 
function reverseNumber(n) {
    let m = 0;
    while (n > 0) {
        m = m * 10 + n % 10;
        n = parseInt(n / 10);
    }
    return m;
}
console.log(reverseNumber(123456));
 
 
// 折半查找, 找不到返回 -1
 
    // 数组传参复制的是地址
function binarySeek(array, target) {
    let start = 0;
    let end = array.length - 1;
    while (start <= end) {
        let mid = parseInt((start + end) / 2);
        if (array[mid] < target) {
            end = mid - 1;
        } else { 
            return mid;
        }
    }
    return -1;
}
 
 
 
 
// 判断一个数组是否有序 返回结果: 乱序 0 升序 1 降序 2 全相等 3 

function isOrder(array) {
    let ascCount = 0; // 升序计数器
    let descCount = 0; // 降序计数器
    for (i = 0; i < array.length - 1; i++) {
        if (array[i] <= array[i + 1]) {
            ascCount++;
        }
        if (array[i] >= array[i + 1]) {
            descCount++;
        }
    }

    if (ascCount == array.length - 1 && descCount == array.length - 1) {
        return 3;
    } else if (ascCount == array.length - 1) {
        return 1;
    } else if (descCount == array.length - 1) {
        return 2;
    }
    return 0;
}