diff --git a/homeword1.js b/homeword1.js new file mode 100644 index 0000000000000000000000000000000000000000..5b2b86ae0e0307642a6f849e935e3a20c2a36f9f --- /dev/null +++ b/homeword1.js @@ -0,0 +1,100 @@ +// 创建一个函数用以找到两个数字中较大的数 +function one(num1, num2) { + if (num1 > num2) + return num1; + return num2 +} + +// 创建一个函数用以判断一个数字是否是质数 +function two(num) { + for (let i = 2; i < num; i++) { + if (num % i === 0) + return false + } + return true +} + +// 创建一个函数,运用第2题的函数找到给定区间内的质数 +function three(begin, end) { + for (let i = begin; i < end+1; i++) { + + if (two(i)){ + console.log(i) + } + } +} + +// 创建一个函数,用以找到三个数中最大的数并返回 +function four(num1, num2, num3) { + max = num1; + if (num2 > max) + max = num2; + + if (num3 > max) + max = num3; + + return max +} + +// 创建一个函数用以判断一个数字是否是水仙花数。 +function five(num) { + // 百位 + let num1 = parseInt(num / 100); + // 十位 + let num2 = parseInt(num % 100 / 10); + // 个位 + let num3 = num % 10; + + let num_result = num1*num1*num1 + num2*num2*num2 + num3*num3*num3; + + return num_result === num; + +} + +// 创建一个函数,运用第5题的函数找到给定区间内的所有水仙花数。 +function six(begin, end) { + for (let i = begin; i < end+1; i++) { + if (five(i)) + console.log(i) + } +} + +//创建一个函数,能将给定的字符串反转后返回。例如:给函数传递参数'abc',返回'cba'。 +function seven(str) { + return str.split('').reverse().join('') +} +// 创建一个函数用以判断给定的数字是否是偶数。例如:给函数传递参数12,返回true。 +function eight(num) { + return num % 2 === 0; +} +// 创建一个函数用以计算出给定区间内的偶数的数量。例如:给函数传递参数1、10,返回5。 +function nine(begin, end) { + count = 0; + + for (let i = begin; i < end+1; i++) { + if (i % 2 === 0) + count ++ + } + + return count +} +// 创建一个函数用以将给定的三个数字按从小到大的顺序输出。例如:给函数传递6、4、5,函数运行时会输出4 5 6。 +function ten(num1, num2, num3) { + nums = []; + nums.push(num1); + nums.push(num2); + nums.push(num3); + + for (let j = 0; j < nums.length - 1; j++) { + for (let i = j + 1; i < nums.length; i++) { + if (nums[j] > nums[i]){ + let temp = nums[j]; + nums[j] = nums[i]; + nums[i] = temp; + } + } + } + + console.log(nums) +} + diff --git a/homework2/index.html b/homework2/index.html new file mode 100644 index 0000000000000000000000000000000000000000..9014bd774d9811d2953ad68589499cbfa8757493 --- /dev/null +++ b/homework2/index.html @@ -0,0 +1,41 @@ + + +
+ + + +
+