From 2fd568be9cf53d5aec27b6f481cb74fecc8f6bbb Mon Sep 17 00:00:00 2001 From: Dragan Cajic Date: Fri, 19 Mar 2021 23:50:32 +0100 Subject: [PATCH 1/3] reverse string --- exercises/reversestring/index.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/exercises/reversestring/index.js b/exercises/reversestring/index.js index fc909037d4..57f9e670f8 100644 --- a/exercises/reversestring/index.js +++ b/exercises/reversestring/index.js @@ -2,10 +2,21 @@ // Given a string, return a new string with the reversed // order of characters // --- Examples -// reverse('apple') === 'leppa' +// reverse('apple') === 'elppa' // reverse('hello') === 'olleh' // reverse('Greetings!') === '!sgniteerG' -function reverse(str) {} +function reverse(str) { + reversed = ''; + + for (let i = 0; i < str.length; i++) { + reversed += (str[str.length - 1 - i]); + } + + //console.log(reversed); + return reversed; +} + +console.log(reverse('Greetings!')); module.exports = reverse; From af7877ab5b5226837c5f3ee087f511b4af5090b5 Mon Sep 17 00:00:00 2001 From: Dragan Cajic Date: Tue, 30 Mar 2021 21:21:29 +0200 Subject: [PATCH 2/3] alternative solution --- exercises/reversestring/index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/exercises/reversestring/index.js b/exercises/reversestring/index.js index 57f9e670f8..1ecb679e75 100644 --- a/exercises/reversestring/index.js +++ b/exercises/reversestring/index.js @@ -7,11 +7,16 @@ // reverse('Greetings!') === '!sgniteerG' function reverse(str) { - reversed = ''; - + let reversed = ''; + /* for (let i = 0; i < str.length; i++) { reversed += (str[str.length - 1 - i]); } + */ + // ES2015 'for of' loop syntax + for (let character of str) { + reversed = character + reversed; + } //console.log(reversed); return reversed; From cc396b27d373309c630e20cf2666f021d15d950b Mon Sep 17 00:00:00 2001 From: Dragan Cajic Date: Thu, 1 Apr 2021 16:51:20 +0200 Subject: [PATCH 3/3] just a small typo --- completed_exercises/reversestring/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/completed_exercises/reversestring/index.js b/completed_exercises/reversestring/index.js index 5f036a9757..46ca2282b0 100644 --- a/completed_exercises/reversestring/index.js +++ b/completed_exercises/reversestring/index.js @@ -2,7 +2,7 @@ // Given a string, return a new string with the reversed // order of characters // --- Examples -// reverse('apple') === 'leppa' +// reverse('apple') === 'elppa' // reverse('hello') === 'olleh' // reverse('Greetings!') === '!sgniteerG'