Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Changing the value of Timeout depending on the array value

I want to print alphabet from array every 0,5 second and everytime its a vowel every 2 seconds.

I managed to write a code printing array elements every 0,5 second

var alphabet = ['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 i = 0;

for (let i = 0; i < alphabet.length; i++){
        setTimeout(function(){
            console.log(alphabet[i]);
            i++;
        }, 500*i)
}

How do I implement the rest

if (alphabet[i] == 'a' | 'e' | 'i' | 'o' | 'u'){
    setTimeout(function(){
        console.log(alphabet[i]);
        i++;
    }, 2000*i)
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using Promise(), you can use the Promise.all () command, where Promise.all() waits for all Promises in the array to be processed.

function setIntervalForItem(input = [], vowels = ["a", "e", "i", "o", "u"], defaultInterval = [500, 2000])
{
  const [interval, _interval] = defaultInterval;
  var resultPromiseArray = new Array();

  for (const char of input) {
    const delay = vowels.indexOf(char) === -1 ? interval : _interval;
    resultPromiseArray.push(new Promise(resolve => {
      setTimeout(() => {
        resolve({value: char, time: delay});
      }, delay);
    }));
  }
  return Promise.all(resultPromiseArray);
}

var alphabet = [
  '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'
];

// Then just call:
setIntervalForItem(alphabet)
  .then(result => {
    for (const object of Object.entries(result)) {
      const {value, time} = object[1];
      console.log(`The delay for character [ ${value} ] was ${time} ms`);
    }
  });

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!