0

I have a very simple problem. I have a user input, and the text from the user input gets pushed into an array, then it is (in theory) turned into a string and then split into an array of each and every single character from the string. My question is how do I split a string in an array into an array 1 character long substrings.

let plaintext = document.getElementById("plaintext");
let startB = document.getElementById("start");

let plain = [];
let encryptStorage = [];

startB.addEventListener('click', () => {
  plain.push(plaintext.value);
  plain.toString();
  encryptStorage.push(plain.split(''));
  console.log(encryptStorage);
});
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>CryptoMatic</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <input type="text" id="plaintext" placeholder="Plaintext">
  <div id="start">
    <div id="startT">Start</div>
  </div>
  <script src="app.js"></script>
</body>

</html>

3
  • You're not asking a question here. But I can see that you should do ` encryptStorage.push(plaintext.value.split(''))` Commented Apr 9, 2019 at 0:25
  • And the question is? Commented Apr 9, 2019 at 0:25
  • @Ele I edited so there's a question Commented Apr 9, 2019 at 0:27

1 Answer 1

1

You don't need to call .toString() or the array. Just use the value directly:

const startB = document.querySelector("#start");

const encryptStorage = [];

startB.addEventListener('click', () => {
    const plaintext = document.querySelector('#plaintext');

    encryptStorage.push(plaintext.value.split(''));
    console.log(encryptStorage);
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>CryptoMatic</title>
    <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <input type="text" id="plaintext" placeholder="Plaintext">
                <div id="start">
                <div id="startT">Start</div></div>
       <script src="app.js"></script>
    </body>
</html>

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah didn't even notice that I don't need the plain array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.