Struggling to find a solution to iterate through a string and extract individual characters at specific positions for conversion

function generateFont() {
var userInput = document.getElementById("getName").value;
var fonts = {
  a: ['รฆ','ษ','แด','๐–†','๐“ช','๐•’','๊ช–','๐Ÿ„ฐ'],
  b: ['ษ“', 'ฦƒ','แดฏ','แดƒ','๐…ธ','๐–‡','๐’ท','๐“ซ','๐•“','๐Ÿ„ฑ'],
  c: ['ฦˆ','ษ”','ษ•','๐–ˆ','ๅŒš','๐“ฌ','๐•”','๐Ÿ„ฒ'],
  d: ['ฤ‘','ษ–','ษ—','ฦŒ','รฐ','ศก','๐…ค','ิ‚','๐–‰','๐','๐“ญ','๐••','๐Ÿ„ณ'],
  e: ['ว', 'ษ›','ษ˜','ษš','๐–Š','โ’บ','๐“ฎ','๐•–','๐Ÿ„ด'],
  f: ['ฦ’','ฦญ','ษŸ','๐…ฟ','๐–‹','๐“ฏ','๐•—','๐Ÿ„ต'],
  g: ['วฅ','ษ ','ฦฃ','๐–Œ','๐“–','๐“ฐ','๐•˜','๐Ÿ„ถ'],
  h: ['ฦ•','ฤง','๐–','๐“ฑ','๐•™','๊ซ','๐Ÿ„ท'],
  i: ['ษจ','ฤฑ','๐–Ž','๐“ฒ','๐•š','๐Ÿ„ธ'],
  j: ['๐–','๐•›','๐“ณ','๐•›','๐Ÿ„น'],
  k: ['ฦ™','สž','๐–','โ“š','๐“ด','๐•œ','๐Ÿ„บ'],
  l: ['ล‚','ฦš','๐–‘','๏ฝŒ','๐“ต','๐•','๐Ÿ„ป'],
  m: ['แตฏ','๐–’','โ“œ','๐“ถ','๐•ž','๐Ÿ„ผ'],
  n: ['ษฒ','ฦž','ล‹','แตฐ','ีผ','๐–“','๐•Ÿ','๐“ท','๐Ÿ„ฝ'],
  o: ['ล“','รธ','ษต','ศฃ','สŠ','๐–”','๐“ธ','๐• ','๐Ÿ„พ'],
  p: ['ฦฅ','แตฝ','๐–•','๐“น','๐•ก','ฯ','๐Ÿ„ฟ'],
  q: ['ฦ','๐––','๐“บ','๐•ข','๐Ÿ…€'],
  r: ['แตฒ','๐–—','๐•ฃ','๐“ป','๐•ฃ','๐Ÿ…'],
  s: ['ฦจ','๐–˜','๐•ค','๐“ผ','แฆ“','๐Ÿ…‚'],
  t: ['สˆ','ลง','๐–™','๐“ฝ','๐•ฅ','๐Ÿ…ƒ'],
  u: ['แตพ','๐–š','๐“พ','๐•ฆ','๐Ÿ…„'],
  v: ['ส‹','๐–›','๐“ฟ','๐•ง','๐Ÿ……'],
  w: ['๐“ฆ','๐”€','๐ฐ','๐•จ','แญ™','๐Ÿ…†'],
  x: ['ฯ‡','๐–','๐”','๐•ฉ','๐Ÿ…‡'],
  y: ['ฦด','ฦ›','๐”‚','๐•ช','๐Ÿ…ˆ'],
  z: ['ฦถ','ศฅ','๐•ซ','๐Ÿ…‰']
};
var text = "";
for (var i = 0; i < userInput.length; i++) {
    var character = userInput.charAt(i);
    var randomIndex = Math.floor(Math.random() * fonts[character].length);
    text += fonts[character][randomIndex];
}
console.log(text);
}

I'm attempting to convert each character of the input into randomly selected fancy fonts. For example, if the input is "abc", I want to get the value of each spot and convert it to the selected font.

I've encountered some difficulties and crashed a few times during my attempts.

Answer โ„–1

From what I gather from your query, you are looking to generate text from random indices of customized font arrays based on the provided text.

The following code appears to achieve this:

function fontGen() {
  const fontMap = {
    a: ['รฆ', 'ษ', 'แด', '๐–†', '๐“ช', '๐•’', '๊ช–', '๐Ÿ„ฐ'],
    b: ['ษ“', 'ฦƒ', 'แดฏ', 'แดƒ', '๐…ธ', '๐–‡', '๐’ท', '๐“ซ', '๐•“', '๐Ÿ„ฑ'],
    c: ['ฦˆ', 'ษ”', 'ษ•', '๐–ˆ', 'ๅŒš', '๐“ฌ', '๐•”', '๐Ÿ„ฒ'],
    d: ['ฤ‘', 'ษ–', 'ษ—', 'ฦŒ', 'รฐ', 'ศก', '๐…ค', 'ิ‚', '๐–‰', '๐', '๐“ญ', '๐••', '๐Ÿ„ณ'],
    e: ['ว', 'ษ›', 'ษ˜', 'ษš', '๐–Š', 'โ’บ', '๐“ฎ', '๐•–', '๐Ÿ„ด'],
    f: ['ฦ’', 'ฦญ', 'ษŸ', '๐…ฟ', '๐–‹', '๐“ฏ', '๐•—', '๐Ÿ„ต'],
    g: ['วฅ', 'ษ ', 'ฦฃ', '๐–Œ', '๐“–', '๐“ฐ', '๐•˜', '๐Ÿ„ถ'],
    h: ['ฦ•', 'ฤง', '๐–', '๐“ฑ', '๐•™', '๊ซ', '๐Ÿ„ท'],
    i: ['ษจ', 'ฤฑ', '๐–Ž', '๐“ฒ', '๐•š', '๐Ÿ„ธ'],
    j: ['๐–', '๐•›', '๐“ณ', '๐•›', '๐Ÿ„น'],
    k: ['ฦ™', 'สž', '๐–', 'โ“š', '๐“ด', '๐•œ', '๐Ÿ„บ'],
    l: ['ล‚', 'ฦš', '๐–‘', '๏ฝŒ', '๐“ต', '๐•', '๐Ÿ„ป'],
    m: ['แตฏ', '๐–’', 'โ“œ', '๐“ถ', '๐•ž', '๐Ÿ„ผ'],
    n: ['ษฒ', 'ฦž', 'ล‹', 'แตฐ', 'ีผ', '๐–“', '๐•Ÿ', '๐“ท', '๐Ÿ„ฝ'],
    o: ['ล“', 'รธ', 'ษต', 'ศฃ', 'สŠ', '๐–”', '๐“ธ', '๐• ', '๐Ÿ„พ'],
    p: ['ฦฅ', 'แตฝ', '๐–•', '๐“น', '๐•ก', 'ฯ', '๐Ÿ„ฟ'],
    q: ['ฦ', '๐––', '๐“บ', '๐•ข', '๐Ÿ…€'],
    r: ['แตฒ', '๐–—', '๐•ฃ', '๐“ป', '๐•ฃ', '๐Ÿ…'],
    s: ['ฦจ', '๐–˜', '๐•ค', '๐“ผ', 'แฆ“', '๐Ÿ…‚'],
    t: ['สˆ', 'ลง', '๐–™', '๐“ฝ', '๐•ฅ', '๐Ÿ…ƒ'],
    u: ['แตพ', '๐–š', '๐“พ', '๐•ฆ', '๐Ÿ…„'],
    v: ['ส‹', '๐–›', '๐“ฟ', '๐•ง', '๐Ÿ……'],
    w: ['๐“ฆ', '๐”€', '๐ฐ', '๐•จ', 'แญ™', '๐Ÿ…†'],
    x: ['ฯ‡', '๐–', '๐”', '๐•ฉ', '๐Ÿ…‡'],
    y: ['ฦด', 'ฦ›', '๐”‚', '๐•ช', '๐Ÿ…ˆ'],
    z: ['ฦถ', 'ศฅ', '๐•ซ', '๐Ÿ…‰']
  }
  const userName = document.getElementById("getName").value
  let customText = ''
  for (let i = 0; i < userName.length; i++) {
    const randomFontIndex = Math.floor(Math.random() * fontMap[userName[i]].length)
    customText += fontMap[userName[i]][randomFontIndex]
  }
}
  • Input Example: cnegfati
  • Result: ฦˆล‹๐–ŠฦฃษŸษสˆษจ

Answer โ„–2

To implement a font variation effect, create a mapping object that links letters to different font arrays. Then, you can easily retrieve the appropriate font array by looking up the character in the object.

const fontVariations = {
    a: aFonts,
    b: bFonts,
    ...
};
let characters = userName.split();
let styledText = characters.map(character => {
    let currentFont = fontVariations[character];
    if (currentFont) {
        return currentFont[Math.floor(Math.random()*currentFont.length)];
    } else {
        return character;
    }
}).join("");

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The file is being uploaded using the "button" type

I've been working on automating a website using Python with Selenium, and I need to upload a file as part of the process. Upon inspecting the "choose file" button, I noticed that it has a type attribute set to "file". However, in my case, the type was ...

Revise the reply within ExpressJS

I need help with editing the response to a request in Express. When the request is made via XHR, I want to encapsulate the body inside a JavaScript object. This way, different parts of the page can be accessed individually within the JavaScript object, suc ...

How can I use AngularJS to show a JSON value in an HTML input without any modifications?

$scope.categories = [ { "advertiser_id": "2", "tier_id": 1, "tier_name": "1", "base_cpm_price": "", "retarget_cpm": "", "gender": "", "location": "", "ageblock1": "", "ageblock2": "", "ageblock3": ...

Jquery scaling settings

Can you explain why I am unable to set the scale for this div? var a = 0.1; var b = 0.1; var scale = a+b; $(".item").css('scale', scale) .item { width: 100px; height: 100px; background: blue; } <script src="https://cdnjs.cloudflar ...

The response from AJAX delivers HTML data

I recently implemented an AJAX call in my CodeIgniter project. Let me show you the code: Here is the view section: $('#forgotPassword').click(function() { var base_url = '<?php echo base_url()?>'; $('#forgo ...

Check if any element within a class satisfies the specified condition using Jquery

I am looking to determine if any element within a specific class meets a set of conditions. For example: $(document).on('click','.modalInner_form_nav', function(){ var input = $(this).parents('.modalInner_form').find(' ...

Learning how to invoke a JavaScript function from a Ruby on Rails layout

In the file app/views/download.js.erb, I have defined a javascript function named timeout(). This function continuously polls a specific location on the server to check if a file is ready for download. I am currently running this function as a background ...

Images are failing to show up in the iPhone design

Encountering issues with displaying images on an iPhone? You can replicate the problem by minimizing your browser window horizontally. Here is a link showcasing the problem: here. To temporarily fix this, try zooming out the browser (Ctrl+-). You can see a ...

Tips on causing a forEach loop to pause for a regex substitution to occur

I have a project in progress for an email web app where I am working on replacing certain keywords (first name, last name, email) with the actual properties of the user. As of now, I am going through a list of recipients and modifying the email content to ...

Transmitting information following successful password verification

Currently, I am working on a form to insert users into a database. Along with this, I have two scripts in place - one for password validation and the other for AJAX retrieval of PHP results. As of now, these two scripts are functioning independently. How ...

The Escargot Expedition: A One-Person Learning Adventure

Attempting to crack the 23rd challenge in the SOLOLEARN JavaScript course (known as The Snail in the Well), I came up with this code that worked when the input was 128 but failed when it was 42. What adjustments should be made to ensure the code works succ ...

Is it possible to override tags on a Bootstrap landing page?

Seeking assistance with a body tag override issue I am currently facing. Here is the webpage in question: I am attempting to implement a responsive full-screen background image on this page (homepage only) by utilizing the full.css file linked to the page ...

Angular component.html does not compile due to a check that includes inline array creation

There is an enum called Status: export enum Status { SOME_VAL = "SOME_VAL", SOME_VAL_2 = "SOME_VAL_2", SOME_VAL_3 = "SOME_VAL_3"; } Also, I have an interface named SomeInterface: export SomeInterface { status? ...

Utilizing Node Mailer and Sendgrid to send emails in an Angular MEAN stack project with the angular-fullstack framework generated by Yeoman

Trying to figure out the best location for the code responsible for sending an email through a contact form in my Angular App using angular-fullstack MEAN stack from Yeoman. I managed to implement the email sending logic in the app.js file on the server s ...

Is there an alternative to Captcha?

Seeking suggestions for a lightweight and simple anti-bot/spam protection method for a basic registration form on my website. I find Captcha annoying and time-consuming. Any alternative suggestions that are easy to integrate and effective against spam? ...

Using a Sequelize where statement with a date condition

Currently, I have my backend set up with Sequelize as the ORM. I am interested in performing some WHERE operations on a Date field. To be more precise, I am looking to retrieve all data where the date falls between now and 7 days ago. The issue arises fr ...

An issue arose while attempting to pin a file on IPFS: A TypeError [ERR_INVALID_ARG_TYPE] was encountered, specifying that the argument "url" must be a string data type

Adding the files to another project resulted in an error that I am struggling to understand. When running "node scripts1/runScript.js", the error message received is as follows: Error occurred while pinning file to IPFS: TypeError [ERR_INVALID_ARG_TYPE]: ...

BeautifulSoup retrieves the entire DIV element, excluding its contents

Apologies if this is a simple question, but I seem to be struggling with finding the right way to ask or locate the answer. My goal is to extract a specific value from a website (18.73kWh) in this particular case. > <div class="itemized-bill-header ...

Troubleshooting issues with NodeJS and Express authentication middleware functionality

I am currently working on implementing the isUserAuthenticated function to run on every server request. This function is required in the app.js file and utilized using app.use(authenticate.isUserAuthenticated). In my project, there is an /authenticate rou ...

Arrangement of elements on a page

I may not be an HTML pro, but I'm curious to find out how I can determine if one element is near another in a web page. For example, how can I tell if a div is close to another div, or if a table is adjacent to an image or another table? ...