|
|
AIWORKÈí¼þÊý×é¸ß¼¶Ê¾Àý
- /**
- *🍎½»Á÷QQȺ711841924Ⱥһ£¬Æ»¹ûÄÚ²âȺ£¬528816639
- * AIWROK ƽ̨Êý×é·½·¨¸ß¼¶Ó¦ÓÃʾÀý
- * ÊÊÓÃÓÚ Android ƽ̨µÄ JavaScript ÒýÇæ Rhino
- * רעÓÚÊý×é·½·¨µÄ¸´ÔÓÓ¦ÓÃ
- */
-
- print.log(logWindow.show());
- print.log(logWindow.clear());
- print.log(logWindow.setHeight(2500));
- print.log(logWindow.setWidth(1500));
- print.log(logWindow.setNoClickModel());
- print.log(logWindow.setColor('red'));
- // Êý×é´´½¨ºÍ³õʼ»¯
- var arr1 = [1, 2, 3, 4, 5];
- var arr2 = new Array(5); // ´´½¨³¤¶ÈΪ5µÄ¿ÕÊý×é
- var arr3 = ['apple', 'banana', 'orange', 'grape', 'kiwi'];
- var arr4 = [
- {id: 1, name: 'ÕÅÈý', age: 25, city: '±±¾©'},
- {id: 2, name: 'ÀîËÄ', age: 30, city: 'ÉϺ£'},
- {id: 3, name: 'ÍõÎå', age: 22, city: '¹ãÖÝ'},
- {id: 4, name: 'ÕÔÁù', age: 28, city: 'ÉîÛÚ'},
- {id: 5, name: 'ËïÆß', age: 35, city: 'º¼ÖÝ'}
- ];
- // 1. push() ºÍ pop() ·½·¨ - Ìí¼ÓºÍÒÆ³ýÊý×éÄ©Î²ÔªËØ
- printl("=== push() ºÍ pop() ·½·¨ ===");
- var fruits = ['apple', 'banana'];
- printl("³õʼÊý×é: " + JSON.stringify(fruits));
- fruits.push('orange'); // Ìí¼Óµ½Ä©Î²
- printl("pushºó: " + JSON.stringify(fruits));
- var removed = fruits.pop(); // ÒÆ³ýÄ©Î²ÔªËØ
- printl("pop·µ»ØÖµ: " + removed + ", Êý×é±äΪ: " + JSON.stringify(fruits));
- // 2. unshift() ºÍ shift() ·½·¨ - Ìí¼ÓºÍÒÆ³ýÊý×é¿ªÍ·ÔªËØ
- printl("\n=== unshift() ºÍ shift() ·½·¨ ===");
- var numbers = [2, 3, 4];
- printl("³õʼÊý×é: " + JSON.stringify(numbers));
- numbers.unshift(1); // Ìí¼Óµ½¿ªÍ·
- printl("unshiftºó: " + JSON.stringify(numbers));
- var shifted = numbers.shift(); // ÒÆ³ý¿ªÍ·ÔªËØ
- printl("shift·µ»ØÖµ: " + shifted + ", Êý×é±äΪ: " + JSON.stringify(numbers));
- // 3. slice() ·½·¨ - ÌáÈ¡Êý×鯬¶Î
- printl("\n=== slice() ·½·¨ ===");
- var colors = ['red', 'green', 'blue', 'yellow', 'purple'];
- printl("ÔÊý×é: " + JSON.stringify(colors));
- printl("slice(1, 3): " + JSON.stringify(colors.slice(1, 3))); // ÌáÈ¡Ë÷Òý1µ½2µÄÔªËØ
- printl("slice(2): " + JSON.stringify(colors.slice(2))); // ´ÓË÷Òý2¿ªÊ¼µ½½áÊø
- // 4. splice() ·½·¨ - Ìí¼Ó¡¢É¾³ý»òÌæ»»Êý×éÔªËØ
- printl("\n=== splice() ·½·¨ ===");
- var months = ['Jan', 'March', 'April', 'June'];
- printl("ÔÊý×é: " + JSON.stringify(months));
- // ÔÚË÷Òý1´¦É¾³ý0¸öÔªËØ£¬È»ºóÌí¼Ó'Feb'
- months.splice(1, 0, 'Feb');
- printl("splice(1, 0, 'Feb'): " + JSON.stringify(months));
- // ´ÓË÷Òý4¿ªÊ¼É¾³ý1¸öÔªËØ£¬È»ºóÌí¼Ó'May'
- months.splice(4, 1, 'May');
- printl("splice(4, 1, 'May'): " + JSON.stringify(months));
- // 5. concat() ·½·¨ - Á¬½ÓÊý×é
- printl("\n=== concat() ·½·¨ ===");
- var arrA = [1, 2, 3];
- var arrB = [4, 5, 6];
- var arrC = [7, 8];
- var concatenated = arrA.concat(arrB, arrC, [9, 10]); // ¿ÉÒÔÁ¬½Ó¶à¸öÊý×é
- printl("concat½á¹û: " + JSON.stringify(concatenated));
- // 6. forEach() ·½·¨ - ±éÀúÊý×é
- printl("\n=== forEach() ·½·¨ ===");
- var products = [
- {name: '±Ê¼Ç±¾µçÄÔ', price: 5000},
- {name: 'ÊÖ»ú', price: 3000},
- {name: 'ƽ°å', price: 2000}
- ];
- products.forEach(function(product, index) {
- printl((index + 1) + ". " + product.name + ": ¥" + product.price);
- });
- // 7. map() ·½·¨ - ´´½¨ÐÂÊý×é
- printl("\n=== map() ·½·¨ ===");
- var prices = products.map(function(product) {
- return product.price;
- });
- printl("¼Û¸ñÊý×é: " + JSON.stringify(prices));
- // ¼ÆËãÕǼۺó¼Û¸ñ£¨ÕǼÛ10%£©
- var increasedPrices = products.map(function(product) {
- return {
- name: product.name,
- originalPrice: product.price,
- increasedPrice: product.price * 1.1
- };
- });
- printl("ÕǼۺóÊý¾Ý: " + JSON.stringify(increasedPrices));
- // 8. filter() ·½·¨ - ¹ýÂËÊý×éÔªËØ
- printl("\n=== filter() ·½·¨ ===");
- var expensiveProducts = products.filter(function(product) {
- return product.price > 2500;
- });
- printl("¸ß¼ÛÉÌÆ·: " + JSON.stringify(expensiveProducts));
- // 9. reduce() ·½·¨ - ¹éÔ¼²Ù×÷
- printl("\n=== reduce() ·½·¨ ===");
- var totalCost = products.reduce(function(sum, product) {
- return sum + product.price;
- }, 0);
- printl("×ܼ۸ñ: ¥" + totalCost);
- // 10. sort() ·½·¨ - ÅÅÐò
- printl("\n=== sort() ·½·¨ ===");
- var sortedByPrice = products.slice(); // ´´½¨¸±±¾
- sortedByPrice.sort(function(a, b) {
- return a.price - b.price; // ÉýÐòÅÅÁÐ
- });
- printl("°´¼Û¸ñÉýÐò: " + JSON.stringify(sortedByPrice));
- // °´ÐÕÃûÅÅÐò
- var sortedByName = arr4.slice();
- sortedByName.sort(function(a, b) {
- var nameA = a.name.toUpperCase();
- var nameB = b.name.toUpperCase();
- if (nameA < nameB) return -1;
- if (nameA > nameB) return 1;
- return 0;
- });
- printl("°´ÐÕÃûÅÅÐò: " + JSON.stringify(sortedByName));
- // 11. find() ºÍ findIndex() ·½·¨
- printl("\n=== find() ºÍ findIndex() ·½·¨ ===");
- var personOver30 = arr4.find(function(person) {
- return person.age > 30;
- });
- printl("ÄêÁä´óÓÚ30µÄÈË: " + JSON.stringify(personOver30));
- var indexPerson = arr4.findIndex(function(person) {
- return person.city === '¹ãÖÝ';
- });
- printl("¾ÓסÔÚ¹ãÖݵÄÈ˵ÄË÷Òý: " + indexPerson);
- // 12. some() ºÍ every() ·½·¨
- printl("\n=== some() ºÍ every() ·½·¨ ===");
- var hasYoungPerson = arr4.some(function(person) {
- return person.age < 25;
- });
- printl("ÊÇ·ñÓÐÄêÁäСÓÚ25µÄÈË: " + hasYoungPerson);
- var allAdults = arr4.every(function(person) {
- return person.age >= 18;
- });
- printl("ËùÓÐÈËÊÇ·ñ¶¼³ÉÄê: " + allAdults);
- // 13. Êý×éºÏ²¢¸ß¼¶Ó¦Óà - ¸´ÔÓÊý¾Ý´¦ÀíʾÀý
- printl("\n=== Êý×éºÏ²¢¸ß¼¶Ó¦Óà ===");
- // ¼ÙÉèÎÒÃÇÓÐÒ»¸ö¶©µ¥ÏµÍ³£¬ÐèÒª´¦ÀíÓû§¶©µ¥Êý¾Ý
- var orders = [
- {userId: 1, productId: 101, quantity: 2, price: 50},
- {userId: 1, productId: 102, quantity: 1, price: 30},
- {userId: 2, productId: 101, quantity: 3, price: 50},
- {userId: 2, productId: 103, quantity: 1, price: 80},
- {userId: 3, productId: 102, quantity: 4, price: 30}
- ];
- var users = [
- {id: 1, name: 'Alice', email: 'alice@example.com'},
- {id: 2, name: 'Bob', email: 'bob@example.com'},
- {id: 3, name: 'Charlie', email: 'charlie@example.com'}
- ];
- // ¼ÆËãÿ¸öÓû§µÄ×ÜÏû·Ñ¶î
- var userSpending = users.map(function(user) {
- var userOrders = orders.filter(function(order) {
- return order.userId === user.id;
- });
-
- var totalSpent = userOrders.reduce(function(total, order) {
- return total + (order.quantity * order.price);
- }, 0);
-
- return {
- userId: user.id,
- userName: user.name,
- totalSpent: totalSpent,
- orderCount: userOrders.length
- };
- });
- printl("Óû§Ïû·Ñͳ¼Æ: " + JSON.stringify(userSpending));
- // °´Ïû·Ñ½ð¶îÅÅÐò
- userSpending.sort(function(a, b) {
- return b.totalSpent - a.totalSpent;
- });
- printl("°´Ïû·Ñ½ð¶îÅÅÐò: " + JSON.stringify(userSpending));
- // ÕÒ³öÏû·Ñ×î¸ßµÄÓû§
- var topConsumer = userSpending[0];
- printl("Ïû·Ñ×î¸ßµÄÓû§: " + JSON.stringify(topConsumer));
- // 14. ¶àάÊý×é²Ù×÷
- printl("\n=== ¶àάÊý×é²Ù×÷ ===");
- var matrix = [
- [1, 2, 3],
- [4, 5, 6],
- [7, 8, 9]
- ];
- // ½«¶þάÊý×é±âƽ»¯
- var flatArray = matrix.reduce(function(acc, row) {
- return acc.concat(row);
- }, []);
- printl("±âƽ»¯¾ØÕó: " + JSON.stringify(flatArray));
- // ¼ÆËã¾ØÕóÿÐеĺÍ
- var rowSums = matrix.map(function(row) {
- return row.reduce(function(sum, num) {
- return sum + num;
- }, 0);
- });
- printl("ÿÐеĺÍ: " + JSON.stringify(rowSums));
- // 15. Êý×éÈ¥ÖØ
- printl("\n=== Êý×éÈ¥ÖØ ===");
- var duplicates = [1, 2, 2, 3, 4, 4, 5, 6, 6, 7];
- var unique = duplicates.filter(function(item, index, array) {
- return array.indexOf(item) === index;
- });
- printl("È¥ÖØÇ°: " + JSON.stringify(duplicates));
- printl("È¥ÖØºó: " + JSON.stringify(unique));
- // ¶ÔÏóÊý×éÈ¥ÖØÊ¾Àý
- var people = [
- {id: 1, name: 'ÕÅÈý', age: 25},
- {id: 2, name: 'ÀîËÄ', age: 30},
- {id: 1, name: 'ÕÅÈý', age: 25}, // ÖØ¸´
- {id: 3, name: 'ÍõÎå', age: 28},
- {id: 2, name: 'ÀîËÄ', age: 30} // ÖØ¸´
- ];
- var uniquePeople = people.filter(function(person, index, array) {
- return array.findIndex(function(p) {
- return p.id === person.id;
- }) === index;
- });
- printl("¶ÔÏóÊý×éÈ¥ÖØ: " + JSON.stringify(uniquePeople));
- // 16. ×ÛºÏʾÀý£ºÑ§Éú³É¼¨·ÖÎö
- printl("\n=== ѧÉú³É¼¨·ÖÎö×ÛºÏʾÀý ===");
- var students = [
- {name: 'СÃ÷', scores: [85, 92, 78, 96, 88]},
- {name: 'Сºì', scores: [95, 87, 92, 89, 94]},
- {name: 'С¸Õ', scores: [78, 82, 85, 79, 83]},
- {name: 'СÃÀ', scores: [90, 94, 88, 92, 96]}
- ];
- // ¼ÆËãÿ¸öѧÉúµÄƽ¾ù³É¼¨
- var studentAverages = students.map(function(student) {
- var average = student.scores.reduce(function(sum, score) {
- return sum + score;
- }, 0) / student.scores.length;
-
- return {
- name: student.name,
- average: Math.round(average * 100) / 100, // ±£ÁôÁ½Î»Ð¡Êý
- highestScore: Math.max.apply(Math, student.scores),
- lowestScore: Math.min.apply(Math, student.scores)
- };
- });
- printl("ѧÉúƽ¾ù³É¼¨: " + JSON.stringify(studentAverages));
- // ÕÒ³öƽ¾ù·Ö×î¸ßµÄѧÉú
- var topStudent = studentAverages.reduce(function(best, current) {
- return current.average > best.average ? current : best;
- });
- printl("×î¸ßƽ¾ù·ÖѧÉú: " + JSON.stringify(topStudent));
- // ¼ÆËã°à¼¶ÕûÌ寽¾ù·Ö
- var classAverage = studentAverages.reduce(function(sum, student) {
- return sum + student.average;
- }, 0) / studentAverages.length;
- printl("°à¼¶Æ½¾ù·Ö: " + Math.round(classAverage * 100) / 100);
- // ͳ¼ÆÓÅÐãѧÉú£¨Æ½¾ù·Ö>=90£©
- var excellentStudents = studentAverages.filter(function(student) {
- return student.average >= 90;
- });
- printl("ÓÅÐãѧÉúÈËÊý: " + excellentStudents.length + "/" + studentAverages.length);
- printl("ÓÅÐãѧÉúÃûµ¥: " + JSON.stringify(excellentStudents));
- printl("✅ Êý×é·½·¨¸ß¼¶Ó¦ÓÃʾÀýÖ´ÐÐÍê±Ï");
¸´ÖÆ´úÂë
|
|