0

So I am trying to make a simple translator. I am just having some issues with it. For example, I want it to register the letter B and then it will write bob. Now when I write b in the translate to box (which is the normal variable) it says in the translated to box (which is theifTalk) [object HTMLTextAreaElement]bob. And in the console it says: Cannot read property 'repeat' of undefined. Anyways here's my code:

const theifTalk = document.getElementById('theiftalk');
const normal = document.getElementById('normal');
const translateBtn = document.getElementById('translateBtn');


translateBtn.onclick = function() {
    theifTalk.value = "";
    translate();
}

function translate() {

    if (normal.value.includes("b")) {
        var bCount = normal.value.match(/b/g)||[].length;

        b().repeat(bCount.length);        
    }

}

function b() {
    theifTalk.value = theifTalk + "bob";
}

HTML CODE:

<textarea type="text" id="normal" rows="15" cols="80" placeholder="Normal text goes in here..."></textarea>
<textarea type="text" id="theiftalk" rows="15" cols="80" placeholder="Theif Talk text goes out here..."></textarea>
<br>
<button id="translateBtn">translate</button>
4
  • 2
    b() function is not returning anything, so when you call b().repeat the result of the function is undefined, so it is impossible to use repeat Commented Feb 19, 2020 at 16:38
  • b is a function that returns undefined and somehow you are saying it returns a method called repeat. Commented Feb 19, 2020 at 16:39
  • @CalvinNunes, is there anything else I could do to make this work then? Commented Feb 19, 2020 at 16:40
  • 1
    What is "repeat" supposed to do? Commented Feb 19, 2020 at 17:03

1 Answer 1

1

As you are not returning anything from function explicitly undefined is returned. You have return the value from function. Also, theifTalk is the element itself, you should take the value from that:

function b() {
  theifTalk.value = theifTalk.value + "bob";
  return theifTalk.value;
}

You also should set the value back to the theifTalk:

theifTalk.value = b().repeat(bCount.length);

const theifTalk = document.getElementById('theiftalk');
const normal = document.getElementById('normal');
const translateBtn = document.getElementById('translateBtn');

translateBtn.onclick = function() {
  translate();
}
function translate() {
  if (normal.value.includes("b")) {
    var bCount = normal.value.match(/b/g)||[].length;
    theifTalk.value = b().repeat(bCount.length);        
  }
}

function b() {
  theifTalk.value = theifTalk.value + "bob";
  return theifTalk.value;
}
<textarea type="text" id="normal" rows="15" cols="80" placeholder="Normal text goes in here..."></textarea>
<textarea type="text" id="theiftalk" rows="15" cols="80" placeholder="Theif Talk text goes out here..."></textarea>
<br>
<button id="translateBtn">translate</button>

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

3 Comments

I guess something is missing here: theifTalk + "bob", theifTalk is not a string. Ah, you've noticed already! LOL
@Mamun That works, but just one problem. If i write 2 b's it still only says bob and not bobbob
@lasseesharp, please update the question with relevant HTML....

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.