Помогите начинающему программисту решить задание.
Try to complete below tasks as soon as you can, but ensure code quality first. Send each solution separately once ready.
1. In JavaScript write a function reduce2 working the same as Array.reduce (use cycle): Array.prototype.reduce2 = function... ← Your code goes here function add(a, b) { return a + b } function mul(a, b) { return a * b } function foo(a, b) { return a.concat(b) } var a = [1, 2, 3, 4] console.log(a.reduce(add), a.reduce2(add)) // 10 10 console.log(a.reduce(add, 10), a.reduce2(add, 10)) // 20 20 console.log(a.reduce(mul), a.reduce2(mul)) // 24 24 console.log(a.reduce(foo, ''), a.reduce2(foo, '')) // 1234 1234 Hints: this[i] arguments.length 2. Write the same reduce2 recursively (do not simulate a cycle through recursion) Hints: slice |
С помощью цикла, как вариант
http://codepen.io/mogafk/pen/EgjZKw?editors=0010 Это больше похоже на какое-то маленькое тестовое задание. Если так, то просить других людей это решить просто зашквар ужасный. Хотя это мог бы быть и видеоурок, но зачем тогда их смотреть если не можешь решить задания? |
Да! Это было тестовое задание которое я не смог выполнить из за отсутствия опыта. Теперь я хочу спросить у опытных программистов как бы они его выполнили (на будущее мне). Я не смог найти достаточно информации про Reduce и поэтому я провалился.
|
Объясни пожалуйста, логику своих действий в задании?
|
:write:
<script src="http://stacksnippets.net/scripts/snippet-javascript-console.min.js?v=1"></script> <script> Array.prototype.reduce2 = function(c, a) { var b = 0; for (void 0 == a && (a = this[b], b = 1); b < this.length; b++) a = c(a, this[b]); return a }; function add(a, b) { return a + b } function mul(a, b) { return a * b } function foo(a, b) { return a.concat(b) } var a = [1, 2, 3, 4] console.log(a.reduce(add), a.reduce2(add)) // 10 10 console.log(a.reduce(add, 10), a.reduce2(add, 10)) // 20 20 console.log(a.reduce(mul), a.reduce2(mul)) // 24 24 console.log(a.reduce(foo, ''), a.reduce2(foo, '')) // 1234 1234 </script> |
Цитата:
<script src="http://stacksnippets.net/scripts/snippet-javascript-console.min.js?v=1"></script> <script> Array.prototype.reduce2 = function(c, a) { var i = 0; for (arguments.length == 1 && (a = this[i], i = 1); i < this.length; i++) a = c(a, this[i]); return a }; function add(a, b) { return a + b } function mul(a, b) { return a * b } function foo(a, b) { return a.concat(b) } var a = [1, 2, 3, 4] console.log(a.reduce(add), a.reduce2(add)) // 10 10 console.log(a.reduce(add, 10), a.reduce2(add, 10)) // 20 20 console.log(a.reduce(mul), a.reduce2(mul)) // 24 24 console.log(a.reduce(foo, ''), a.reduce2(foo, '')) // 1234 1234 </script> |
js for reduce
Цитата:
<script src="http://stacksnippets.net/scripts/snippet-javascript-console.min.js?v=1"></script> <script> Array.prototype.reduce2 = function(c, a) { var b = this.slice(0); 1 == arguments.length && (a = b.shift()); b.length && (a = c(a, b.shift()), a = b.reduce2(c, a)); return a }; function add(a, b) { return a + b } function mul(a, b) { return a * b } function foo(a, b) { return a.concat(b) } var a = [1, 2, 3, 4] console.log(a.reduce(add), a.reduce2(add)) // 10 10 console.log(a.reduce(add, 10), a.reduce2(add, 10)) // 20 20 console.log(a.reduce(mul), a.reduce2(mul)) // 24 24 console.log(a.reduce(foo, ''), a.reduce2(foo, '')) // 1234 1234 </script> |
Спасибо друг! Сразу видно знающего человека. Можешь коротко описать логику действий? И что дает здесь Reduce?
|
Vjacheslav143,
Цитата:
|
Действительно не нужна. Просто привычка вызывать так колбеки ;)
|
Часовой пояс GMT +3, время: 22:23. |