From 620f2fe27bcd89c771b487971c7bd5d66b9945e5 Mon Sep 17 00:00:00 2001 From: Paulo Henrique Scherer Date: Thu, 19 Oct 2017 11:30:02 -0200 Subject: [PATCH] added sort and append array algorithms with js --- append-array.js | 12 ++++++++++++ sort-peoples.js | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 append-array.js create mode 100644 sort-peoples.js diff --git a/append-array.js b/append-array.js new file mode 100644 index 0000000..ce77fbd --- /dev/null +++ b/append-array.js @@ -0,0 +1,12 @@ +// Append array of strings on value + +const appendValues = (value, appends) => { + if (appends.length === 0) { + return value + } + return value + appends.join('') +}; + +text = appendValues('A', ['m', 'azi', 'ng']); + +console.log(text); // Amazing diff --git a/sort-peoples.js b/sort-peoples.js new file mode 100644 index 0000000..38a7894 --- /dev/null +++ b/sort-peoples.js @@ -0,0 +1,18 @@ +const peoples = [ + { name: 'Paulo', age: 19 }, + { name: 'Hannah', age: 26 }, + { name: 'Lucio', age: 32 }, + { name: 'Waldo', age: 73 }, + { name: 'Larissa', age: 22 }, + { name: 'Brucer', age: 15 } +]; + +const sortNames = peoples.sort((a, b) => { + const firstName = a.name.toLowerCase(); + const secondName = b.name.toLowerCase(); + if (firstName < secondName) return -1 + if (firstName > secondName) return 1 + return 0 +}) + +console.log(sortNames);