#how to convert array of strings into binary code

1 messages · Page 1 of 1 (latest)

tawny olive
#

like for each element of the strings array

deep pawn
#

You want each character of each string in the array of strings to be it's ASCII representation in binary?

tawny olive
#

yeah basically

#

so i have a list of string in an array and want to create a function that transforms all strings in that array

#

into an array of binary

deep pawn
#

Which part are you struggling with specifically?

#

Seems like a decent starting point.

tawny olive
deep pawn
#

Use a loop.

#

Another forEach perhaps.

#

First try printing each string in an array.

tawny olive
#
// console.log(words);

let binary = []

const generator = function (arr) {
    arr.forEach(el => {

    console.log(el.charCodeAt(0).toString(2)) + " ";
});

}

generator(words)
#

seems like im only getting the first letter of a string

covert swallow
#

That's what charCodeAt(0) does. If you want the second letter you need to use charCodeAt(1). The number in the parenthesis is the index of the letter you want the character code for.

gleaming verge
#

You'd use a nested loop

#

To access the subsequent elements, characters to get for each of the characters in every string in the whole collection, like in the textarea, got that?

Or use a array.forEach() to achieve the same