Last active
November 19, 2021 17:25
-
-
Save vladislavaSim/3321c9c175ea8bb4b9a05d4698fd1448 to your computer and use it in GitHub Desktop.
Homework3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1) | |
function squareNum(n) { | |
return Math.pow(2, n) | |
} | |
console.log('square number is ' + squareNum(5)); //32 | |
// 2) | |
function getFactorial(n) { | |
let x = 1; | |
for(let i = 1; i <= n; i++) { | |
x = x * i | |
} | |
return x; | |
} | |
console.log('factorial is ' + getFactorial(10)) | |
// 3) | |
function calc(n) { | |
for(let i = 1; i <= n; i++) { | |
let res = (1 + 1 / Math.pow(i, 2)); | |
return res*=res | |
} | |
} | |
console.log('task 3 answer is ' + calc(2)); | |
// 4) | |
function calc2(n) { | |
for(let i = 1; i <= n; i++) { | |
let res; | |
res = 1 / Math.sin(i); | |
return res += res; | |
} | |
} | |
console.log('task 4 answer is ' + calc2(2)); | |
// 5) | |
function calc3(n) { | |
let res = Math.sqrt(2); | |
for(let i = 1; i <= n; i++) { | |
for(let j = 0; j < i; j++) { | |
res += res; | |
} | |
} | |
return res | |
} | |
console.log('task 5 answer is ' + calc3(2)); | |
// 6) | |
function getMaxNum(x, y) { | |
return x < y ? y : x; | |
} | |
console.log(getMaxNum(100, 201)); | |
function getMinNum(x, y) { | |
return x < y ? x : y; | |
} | |
console.log(getMinNum(1, 0)) | |
function getMinMax(x, y) { | |
switch (true) { | |
case (x < y): | |
return `${x} is a less number and ${y} is a bigger one`; | |
break; | |
case (x > y): | |
return `${y} is a less number and ${x} is a bigger one`; | |
break; | |
case (x == y): | |
return `${y} and ${x} are equal`; | |
break; | |
default: | |
return 'enter a correct value' | |
} | |
} | |
console.log(getMinMax(1, 10)); | |
// 7) | |
function getTheBiggestNum(x, y, z) { | |
if(x > y && x > z) { | |
return x; | |
} else if(y > x && y > z) { | |
return y; | |
} else { | |
return z; | |
} | |
} | |
console.log(getTheBiggestNum(5, 20, 8)); | |
function getMinMaxOutOfThreeNums(x, y, z) { | |
let theBiggest = x > y ? x : y && y > z ? y : z; | |
let theLeast = x < y ? x : y && y < z ? y : z; | |
return `the biggest number is ${theBiggest} and the least is ${theLeast}` | |
} | |
console.log(getMinMaxOutOfThreeNums(100, 105, 200)); | |
// 8) | |
function getBiggerSumOrProd(x, y, z) { | |
let sum = x + y + z; | |
let prod = x * y * z; | |
return prod > sum ? prod : sum | |
} | |
console.log(getBiggerSumOrProd(2, 2, 3)); | |
function getSquaredMin(x, y, z) { | |
let sum = x + y + (z/2); | |
let prod = x * y * z; | |
let res = sum < prod ? sum +=1 : prod += 1; | |
console.log(res + 'res') | |
return Math.pow(res, 2); | |
} | |
console.log(getSquaredMin(2, 2, 2)) |
function calc(n) {
let res;
for(let i = 1; i <= n; i++) {
res = 1 / Math.sin(i);
}
return res += res;
}
Вынесла объявление переменной за пределы цикла, return - тоже. Теперь правильно?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function calc2(n) {
for(let i = 1; i <= n; i++) {
let res;
res = 1 / Math.sin(i);
return res += res; ---------------------------- оно выйдет на первой итерации, это ошибка
}
}