-2

Seen some solutions using a regular for loop, but was interested in the recursive way

maybe we can define the arr as the alphabet

let arr = ['abcdefghij(...rest_of_alphabet)`]

found this on the interwebs, would this work?

Call map() using the array [ ‘a’, ‘b’, ‘c’ ] Create a new array that holds the result of calling fn(‘a’) Return [ ‘A’ ].concat( map([ ‘b’, ‘c’ ]) ) Repeat steps 1 through 3 with [ ‘b’, ‘c’ ] Repeat steps 1 through 3 for [ ‘c’ ] Eventually, we call map() with an empty array, which ends the recursion.

zunny
  • 1
  • 3
  • 1
    It's not quite clear what you mean by "print the alphabet". Can you post/link the solutions you've seen? Can you show your attempts at a recursive version? – Bergi Aug 20 '22 at 00:03
  • 4
    Sounds like an School assignment question... do your own work! – Patrick Forget Aug 20 '22 at 00:13
  • A recursive way usually goes like this: you call a function, passing it the entire data. Then that function only runs a task on the first element, and then calls itself with the remaining elements until none are left. That should be a good starting point – blex Aug 20 '22 at 00:29

3 Answers3

2

Here's a simple recursive technique using a single charCode parameter -

const alphabet = (charCode = 97) =>
  charCode > 122
    ? ""
    : String.fromCharCode(charCode) + alphabet(charCode + 1)
    
console.log(alphabet())

// abcdefghijklmnopqrstuvwxyz

Notice the exit charCode is hard-coded to 122. We would make the function more flexible by outputting a generic charRange where the caller specifies the beginning and end of the range -

const charRange = (min, max) =>
  min > max
    ? ""
    : String.fromCharCode(min) + charRange(min + 1, max)
    
console.log(charRange(97, 122))
// abcdefghijklmnopqrstuvwxyz

console.log(charRange(65, 90))
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
Mulan
  • 129,518
  • 31
  • 228
  • 259
1

Here is a way:

const alphabet = Array.from({length: 26}, (x, i) => String.fromCharCode(97 + i));

function printEach(arr) {
  if (arr.length === 0) return; // If arr is empty, return
  console.log(arr.shift());     // Remove the first letter from arr and print it
  return printEach(arr);        // Deal with the remaining elements
}

printEach(alphabet);
blex
  • 24,941
  • 5
  • 39
  • 72
  • can explain the first line please? const alphabet = Array.from({length: 26}, (x, i) => String.fromCharCode(97 + i)); – zunny Aug 20 '22 at 00:44
  • It creates an Array with length 26, and uses a callback function to fill it. Here, the function just returns, the character corresponding to the ASCII code `97 + i`, because `a`'s ASCII code is `97` (see [this table](https://www.rapidtables.com/code/text/ascii-table.html)). It is equivalent to writing `['a', 'b', 'c', ...]` – blex Aug 20 '22 at 00:47
  • I see i is the index, however what is x? – zunny Aug 20 '22 at 01:03
  • @AhsunKhan `x` refers to the current element of the array in the callback function, which is `undefined` in this example. See [`Array.from()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from) – jsejcksn Aug 20 '22 at 01:04
1

A simplification of this answer, without the array:

function printAlphabet (codePoint = 97) {
  if (codePoint > 122) return; // If code point is greater than "z"
  console.log(String.fromCodePoint(codePoint));
  return printAlphabet(codePoint + 1);
}

printAlphabet();
jsejcksn
  • 27,667
  • 4
  • 38
  • 62