JavaScript作业习题

//1、已知一个梯形,面积是150,高是15,上底边是3,那么下底边是多少?

var s = 150, height = 15, up = 3, down;
down = s * 2 / height - up;
console.log("下底" + down);

//2、比较两数大小、输出较小的。

var a = 10, b = 9;
console.log("较小的数" + a > b ? b : a);

//3、分别输出一个三位数的个位、十位、百位。

var ran = 123;
var a, b, c;
a = ran % 10;
console.log("个位" + a);
ran /= 10;
b = parseInt(ran % 10);
console.log("十位" + b);
ran /= 10;
c = parseInt(ran % 10);
console.log("百位" + c);

//4、已知三角形的三条边的长度为a,b,c,求三角形的面积S。海伦公式

var a = 3, b = 4, c = 5;
var p = (a + b + c) / 2;
console.log("三角形面积:" + Math.sqrt(p * (p - a) * (p - b) * (p - c)));

//5、输出一个数字的绝对值。不要使用abs

var num = -1;
if (num < 0) {
    console.log(num + "绝对值" + -num);
}
else {
    console.log(num + "绝对值" + num);
}

//6、如果考试采用等级制,即将百分制转化为A、B、C、D、E五个等级,

// 设成绩为x,则x>=90为A,x>=80为B,x>=70为C,x>=60为D,否则为E。

//编写一个程序,将分数转化为对应的等级并输出。

var score = 90;
if (score >= 90) {
    console.log("A");
}
else if (score >= 80) {
    console.log("B");
}
else if (score >= 70) {
    console.log("C");
}
else if (score >= 60) {
    console.log("D");
}
else {
    console.log("E");
}

//7、整数猜想:对于任意一个给定的>1的正整数,如果他是一个偶数,

//请将其除以2,若是奇数,将其乘以3加1,对其运算结果,如果它不是1,则

//重复以上操作过程。经过若干步操作后,总会得到结果1.

var inter = 9;
while (inter != 1 && inter > 1) {
    if (inter % 2 == 0) {
        inter /= 2;
    }
    else {
        inter = inter * 3 + 1;
    }
}
console.log(inter);

//8、获取数字的位数。例如123为3位数。

var num2 = 11111;
function bit(x) {
    var count = 0;
    while (x > 1) {
        x /= 10;
        count++;
    }
    return count;
}
console.log(bit(num2));

//9、输出2~10000范围的所有同构数。同构数是这样的数字,这个数

//会出现在它平方数的右端。例如,5是25的同构数,25是625的同构数。

var str = "";
for (let i = 2; i <= 10000; i++) {
    if ((i * i) % Math.pow(10, bit(i)) == i) {
        str = str + i + " ";
    }
}
console.log(str);

猜你喜欢

转载自blog.csdn.net/qq_35134066/article/details/82355820