-
-
Save pawelkl-zz/a7796166c27d8c49a906 to your computer and use it in GitHub Desktop.
| // Answers | |
| // 1 # Looping a triangle | |
| // # http://eloquentjavascript.net/02_program_structure.html#p_pP646YLlGy | |
| var output = ""; | |
| for (var i = 1; i < 10; i++) { | |
| output += "#"; | |
| console.log(output); | |
| } | |
| // 2 # FizzBuzz | |
| // # http://eloquentjavascript.net/02_program_structure.html#p_i0Cvwf75cQ | |
| function fizzBuzz(number) { | |
| for (var i = 0; i <= number; i++) { | |
| var out = ""; | |
| if (number % 3 === 0) { | |
| out += "Fizz"; | |
| } | |
| if (number % 5 === 0) { | |
| out += "Buzz"; | |
| } | |
| console.log(out || number); | |
| } | |
| } | |
| // 3 # Chess board | |
| // # http://eloquentjavascript.net/02_program_structure.html#p_uH3DV6RVnV | |
| function grid(size) { | |
| var first = "#"; | |
| var second = "_"; | |
| for (var i = 0; i <= size; i++) { | |
| var out = ""; | |
| for (var k = 0; k <= size; k++) { | |
| out += (k % 2 === 0) ? first : second; | |
| } | |
| c.log(out); | |
| var temp = first; | |
| first = second; | |
| second = temp; | |
| } | |
| } | |
| grid(6); | |
| // 4 # Minimum | |
| // # http://eloquentjavascript.net/03_functions.html#p_aW/Uoj4mDd | |
| function getMinimum(a, b) { | |
| return (a < b) ? a : b; | |
| } | |
| // 5 # Recursion # recursed even check | |
| // # http://eloquentjavascript.net/03_functions.html#p_iDq2OgBOGw | |
| function isEven(number) { | |
| if (number === 0) { | |
| return "even"; | |
| } | |
| if (number === 1) { | |
| return "odd"; | |
| } | |
| // else { # niepotrzebne bo nie ma już gdzie pójść | |
| return iseven(number - 2); | |
| // } | |
| } | |
| // 6 # Bean counting | |
| // # http://eloquentjavascript.net/03_functions.html#p_8y74cOkS91 | |
| function countBs(string, charToFind) { | |
| var count = 0; | |
| for (var i = 0; i < string.length; i++) { | |
| if (string.charAt(i) === charToFind) { | |
| count++; | |
| } | |
| } | |
| return count; | |
| } | |
| // CHAPTER 04 | |
| // 7 # The sum of a range | |
| // # http://eloquentjavascript.net/04_data.html#p_fpyyiv/hm1 | |
| function range(start, end) { | |
| out = []; | |
| for (var i = start; i <= end; i++) { | |
| out.push(i); | |
| } | |
| return out; | |
| } | |
| function sum(array) { | |
| var sum = 0; | |
| for (var i = 0; i < array.length; i++) { | |
| sum += array[i]; | |
| } | |
| return sum; | |
| } | |
| function range_v2(start, end, step) { | |
| out = []; | |
| for (var i = start; i <= end; i += step || 1) { | |
| out.push(i); | |
| } | |
| return out; | |
| } | |
| function range_v3(start, end, step) { | |
| out = []; | |
| if (start < end) { | |
| for (var i = start; i <= end; i += step || 1) { | |
| out.push(i); | |
| } | |
| } else { | |
| for (var k = start; k >= end; k += step || -1) { | |
| out.push(k); | |
| } | |
| } | |
| return out; | |
| } | |
| // 8 # Reversing an array | |
| // # http://eloquentjavascript.net/04_data.html#p_0ysB6LgssH | |
| function reverseArray(array) { | |
| var out = []; | |
| for (var i = 0; i < array.length; i++) { | |
| out[array.length - i] = array[i]; | |
| } | |
| return out; | |
| } | |
| function reverseArray_opt1(array) { | |
| var out = []; | |
| for (var i in array) { | |
| out[array.length - i] = array[i]; | |
| } | |
| return out; | |
| } | |
| function reverseArray_opt2(array) { | |
| var out = []; | |
| var lol = array.slice(0, (array.length / 2).floor); | |
| for (var i in lol) { | |
| out[lol.length - i] = array[i]; | |
| } | |
| return out; | |
| } | |
| // 8B # Reversing an array in place | |
| // 9 # A list | |
| // # http://eloquentjavascript.net/04_data.html#p_iPlgVCeZGh | |
| // 10 # Deep comparison | |
| // # http://eloquentjavascript.net/04_data.html#p_xTwbRlqHNJ | |
| // CHAPTER 5 | |
| // 1 # Flattening | |
| // # http://eloquentjavascript.net/05_higher_order.html#p_RqAkArolEa | |
| // DATA | |
| var arrays = [[1, 2, 3], [4, 5], [6]]; | |
| console.log(arrays.reduce(function(a,b){ return a.concat(b) ;})); | |
| // 2 # Mother-child age difference | |
| // # http://eloquentjavascript.net/05_higher_order.html#p_a44KIXR4aT | |
| // variant 1 | |
| function hasKnownMother(person){ | |
| return byName[person.mother] !== null ? person.mother : null; | |
| } | |
| console.log(average(ancestry.filter(function(){return f !== null;}))); | |
| // variant 2 | |
| function ageDifference(person){ | |
| var mother = byName[person.mother]; | |
| return mother ? person.born - mother.born : null; | |
| } | |
| console.log(average(ancestry.map(ageDifference).filter(function(f){return f !== null;}))); | |
| // 3 # Historical life expectancy | |
| // # http://eloquentjavascript.net/05_higher_order.html#p_ENbLeUYppS | |
| // 3 # Every and then some | |
| // # http://eloquentjavascript.net/05_higher_order.html#p_BZKBxgsPML | |
| // Your code here. | |
| function every(array,check){ | |
| for (var i = 0; i < array.length; i++){ | |
| if ( !check(array[i]) ) { return false; } | |
| } | |
| return true; | |
| } | |
| function some(array,check){ | |
| for (var i = 0; i < array.length; i++){ | |
| if ( check(array[i]) ) {return true; } | |
| } | |
| return false; | |
| } | |
| // TEST DATA | |
| console.log(every([NaN, NaN, NaN], isNaN)); | |
| // → true | |
| console.log(every([NaN, NaN, 4], isNaN)); | |
| // → false | |
| console.log(some([NaN, 3, 4], isNaN)); | |
| // → true | |
| console.log(some([2, 3, 4], isNaN)); | |
| // → false | |
z reverse to w [] powinno być jeszcze -1 "ale" jakbym nie testował to mi wychodziło w pewnym momencie bez różnicy.. no i zgłupiałem, miałem zrobić więcej testów.. bo różnica musi być, albo coś mi REPL chrzanił głupoty.
dlatego "_opt1" że użycie właśnie wbrew zastosowaniu, też żeby zobaczyć jak to się zachowuje, bo wewnętrznie to pewnie jest pętla, ale zawsze lepiej sobie puścić, na własne oczy zobaczyć że głupoty są i wiedzieć żeby nie używać (jak człowieka raz w życiu prąd z gniazdka nie popieści to... ;) hihi ) -> dlatego wolę to robić w kontrolowany środowisku małego przykładu aniżeli kiedyś później pomyśleć "czemu nie"... ;)
przy testowaniu z reverse wychodzi Ci blad. spojrz na konsoli w chrome.
pierwszy element w tablicy jest pozniej undefined.
jezeli podajesz funkcje w callbacku to styluj je wcieciami. inaczej ciezko to sie czyta.
nie poprawiles tez tych arrayReverse.
natywne funkcje
przejrzyj prosze API arraya (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) i przepisz funkcje sum(), reverseArray() uzywajac tych funkcji.
i odnosnie reverseArray_opt1, _opt2 - nigdy tak nie rob. tzn. nigdy nie uzywaj in'a do iteracji po tablicach, gdyz in nie zachowuje kolejnosci funkcji.
btw. reverseArray, reverseArray_opt1, reverseArray_opt2 - wszystkie dzialaja zle. dlaczego?