123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- // a
- function aSample() {
- function a(string){
- alert(string);
- }
- a("Привет!");
- }
- // cube
- function cubeSample() {
- function cube(number){
- return Math.pow(number, 3)
- }
- console.log(cube(9)); // 729
- }
- // avg2
- function avg2Sample() {
- function avg2(number1, number2) {
- return (number1 + number2) / 2;
- }
- console.log(avg2(1, 2)); // 1.5
- console.log(avg2(10, 5)) // 7.5
- }
- // sum3
- function sum3Sample() {
- function sum3(...rest) {
- return Array.from(rest).reduce((a, b) => a + b);
- }
- console.log(sum3(1,2,3)) // => 6
- console.log(sum3(5,10,100500)) // => 100515
- console.log(sum3(5,10)) // => 15
- }
- // intRandom
- function intRandomSample() {
- function intRandom(maximalValue, minimalValue = 0) {
- return Math.floor(Math.random() * (maximalValue - minimalValue + 1) + minimalValue);
- }
- console.log(intRandom(2, 15));
- console.log(intRandom(-1, -1));
- console.log(intRandom(0, 1));
- console.log(intRandom(10));
- }
- // greetAll
- function greetAllSample() {
- function greetAll(...params) {
- let greetString = '';
- Array.from(params).forEach(element => {
- greetString += `${element}, `;
- });
- return alert(`Hello ${greetString.slice(0, -2)}`);
- }
- greetAll("Superman"); // Hello Superman
- greetAll("Superman", "SpiderMan"); // Hello Superman, SpiderMan
- greetAll('Superman', 'SpiderMan', 'Captain Obvious'); // Hello Superman, SpiderMan, Captain Obvious
- }
- // sum
- function sumSample() {
- function sum() {
- let sum = 0;
- for (let item of arguments) {
- sum += item;
- }
- return sum;
- }
- console.log(sum(1)) // 1
- console.log(sum(2)) // 2
- console.log(sum(10, 20, 40, 100)) // 170
- }
- // Union
- let sample = prompt("Введите название задания");
- switch (sample.toLowerCase()) {
- case "a":
- aSample();
- break;
- case "cube":
- cubeSample();
- break;
- case "avg2":
- avg2Sample();
- break;
- case "sum3":
- sum3Sample();
- break;
- case "intrandom":
- intRandomSample();
- break;
- case "greetall":
- greetAllSample();
- break;
- case "sum":
- sumSample();
- break;
- default:
- alert('Неверное название задания');
- }
- // Union declarative
- let sampleDeclarative = prompt("Введите название задания");
- const callSample = (sampleType) => {
- const sampleObj = {
- 'a': aSample,
- 'cube': cubeSample,
- 'avg2': avg2Sample,
- 'sum3': sum3Sample,
- 'intrandom': intRandomSample,
- 'greetall': greetAllSample,
- 'sum': sumSample,
- 'default': () => {
- alert('Неверное название задания');
- }
- };
- return (sampleObj[sampleType] || sampleObj['default'])();
- }
- callSample(sampleDeclarative);
|