123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- // //cube
- // let cube = n => n*n*n
- // alert(cube(10))
- // //avg2
- // let avg2 = (a,b) => (a+b)/2
- // alert (avg2 (10,7))
- // //sum3
- // let sum3 = (a,b,c) => {
- // a = a || 0;
- // b = b || 0;
- // c = c || 0;
- // var result = a+b+c
- // alert (result)
- // }
- // sum3(10,5,17)
- // sum3(10,5)
- // //intrandom
- // let intRandom = (low,high) =>Math.round(Math.random()*((high || 0) - (low ||0)) + (low || 0))
- // alert(intRandom(10,5))
- // //greetAll
- // let greetAll = (arguments) => {
- // let str = " "
- // for (let i = 0;i < arguments.length;i++){
- // str += arguments[i]
- // }
- // str.slice(0 , -1)
- // alert ("Hello" + "," + str)
- // }
- // greetAll("SuperMan")
- // //sum (//nerabotaet)
- // let sum = (arguments) =>{
- // var result = 0;
- // for (var i = 0;i <arguments.length; i++){
- // result += arguments[i]}
- // }
- // alert(sum(1,5,6,7,10))
- //sort
- let persons = [
- {name: "Иван", age: 17},
- {name: "Мария", age: 35},
- {name: "Алексей", age: 73},
- {name: "Яков", age: 12},
- ]
- function sort(field, boolean = true) {
- if (boolean) {
- return (a, b) => a[field] > b[field] ? 1 : -1
- }else {
- return (a, b) => a[field] < b[field] ? 1 : -1
- }
- }
- console.log(persons.sort(sort('age', false)))
- //arr.map
- let arr = ["1", {}, null, undefined, "500", 700];
- let newArr = arr.map(function(a){
- return typeof(a) === 'string' ? parseInt(a) : a;
- });
- console.log(arr);
- console.log(newArr);
- //arr.reduce
- var arr = ["0", 5, 3, "string", null]
- var result = arr.reduce(function(previousValue, currentValue){
- if(typeof(currentValue)==='number'){
- previousValue*=currentValue;
- };
- return previousValue
- }, 1)
- alert(result);
- //object filter
- var phone = {
- brand: "meizu",
- model: "m2",
- ram: 2,
- color: "black",
- };
- function filter (object , name ,value) {
- let result = {}
- for (key in object) {
- if (name == key || object[key] == value){
- result[key] = object [key]
- }
- }
- return result
- }
- console.log (filter(phone,'color',2))
- //object map
- let obj = map({name: "Иван", age: 17},function(key,value){
- var result = {};
- result[key+"_"] = value + "$";
- return result;
- })
- function map (object , callback){
- let obj = {}
- for(let key in object) {
- Object.assign(obj,callback(key, object[key]));
- }
- return obj
- }
- console.log(obj);
- //рекурсия (считает сумму до какого-то числа)
- function sumTo(n) {
- if (n == 1) return 1;
- return n + sumTo(n - 1);
- }
-
- alert( sumTo(10) );
-
|