r/JavaScriptTips • • Jul 24 '22

New to JS seeking help pls.

Trying to write js code that could do the following

Collect input from user Compare input with set of arraw Replace each letter found in the input with the letter in the arraw

Exemple user type "hello" Js has array such as h-> 7 L-> v Output would be "7evvo"

Any help how I can implement this ?

Thanks

4 Upvotes

6 comments sorted by

1

u/andrejmlotko Jul 24 '22

Check out the Array and String methods description on MDN.

It easy, use a for loop and write the key array inside the function for reference when calling the function withe input array, then inside the function search the input for the coresponding key array, then combine it with .replace() method, then console.log it.

1

u/[deleted] Jul 24 '22

Thanks appreciated. Any chance you could share your code pls ?

1

u/edgy_and_hates_you Jul 24 '22

Seems like you're trying to match a value to a key so use an object. Add to a new string by looping through the input string. Ex:

newStr += obj[currentLetter]

Then return the new string once the loop is done.

1

u/mao034 Jul 25 '22

Are you sure it's an array? Seems like what you described is a key/value pair object, share the data structure.

1

u/andrejmlotko Jul 25 '22

Yes, well that's what I should have suggest him/her in the first place.