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

164
Views
wrap a function in javascript
let str = "i am writing an algorithm.";

//function to count alphabets
const alphabet_count = (str) => str.length;

//function to count words
const word_count = (str) => str.split(" ").length;

//function to count vowel
const vowel_count = (str) => (str.match(/[aeiou]/gi)).length;

//here i am trying to wrap all three functions in one
const sentence_read() = {alphabet_count(), word_count(), vowel_count()};

I am trying to trying to wrap all three functions in one.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

const sentence_read = (str) => [alphabet_count(str), word_count(str), vowel_count(str)]

will return an array with your 3 results. Usage :

let str = "a word";
console.log(sentence_read(str)) // output : [6, 2, 2]
about 4 years ago · Santiago Trujillo Report

0

Using a template string

let str = "i am writing an algorithm.";

// function to count alphabets
const alphabet_count = (str) => str.length;

// function to count words
const word_count = (str) => str.split(" ").length;

//function to count vowel
const vowel_count = (str) => (str.match(/[aeiou]/gi)).length;

const sentence_read = (str) => `a_c : ${alphabet_count(str)}, w_c : ${word_count(str)}, v_c : ${vowel_count(str)}`

console.log(sentence_read(str)) // a_c : 26, w_c : 5, v_c : 8

If you want to group the functions in an object, you can use:

const str = "i am writing an algorithm.";

const counter = {
    alphabet: (s) => s.length,
    word: (s) => s.split(" ").length,
    vowel: (s) => (s.match(/[aeiou]/gi)).length
}

const count = (unit, str) => {
    if(!counter[unit]) throw Error('Unit does not exist')
    return counter[unit](str)
}

console.log(count('alphabet', str)) // 26
console.log(count('word', str))     // 5
console.log(count('vowel', str))    // 8

about 4 years ago · Santiago Trujillo 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!