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

108
Views
I am trying to concatenate the second parameter to the first in JS

I'm pretty stuck, It should evaluate to ["present", "pretty", "precede"]

function addTooStart(x, y) {
  let a = [];
  let b = "";
  for (let i in x) {
    a = x.push(y);
    a = x[b]
  }
  return a
}

let abc = addTooStart(["sent", "tty", "cede"], "pre");
console.log(abc)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm not sure what your a and b are attempting to do here. Some problems from your code:

  1. x.push(y) adds the element y to the end of the array x, and then returns the new length of the array, so now a is a number.
  2. x[b] will always be an invalid call, since b equals the empty string and is never changed, arrays are integer indexed.

The general approach here would be to loop through the array x, like you did, then for each element, set it equal to "y + current element". I have attached a working version below.

function addToStart(x,y){
  for (let i = 0; i < x.length; i++) {
    x[i] = y + x[i]
  }
  return x
}

addToStart([ "sent", "tty", "cede" ], "pre"); // -> [ 'present', 'pretty', 'precede' ]
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!