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

194
Views
Return previous parameter while calling another function in JS

How to achieve the below logic in JS

Example:

Let y = alertMyPreviousParam(“1”);

Let z= y(‘2’) 
// show output as  1

Let a = z(‘3’)
// show output as 2

Let b = a(‘ra’) 
// show output as 3

Let c = b(‘z’) 
// show output as ‘ra’,
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Despite of various Syntax error

1) string enclosed by ‘ and ’ is not valid javascript string, similarly with “ and ”. Use either " or '.

2) variable should be declared with let not Let.


SOLUTION

You can take advantage of closure and Higher order function(HOF) here

function alertMyPreviousParam(arg) {
  let last = arg;
  return function (curr) {
    console.log(last);
    last = curr;
    return alertMyPreviousParam(curr);
  };
}

let y = alertMyPreviousParam("1");

let z = y("2");
// show output as  1

let a = z("3");
// show output as 2

let b = a("ra");
// show output as 3

let c = b("z");
// show output as ‘ra’,

about 4 years ago · Juan Pablo Isaza Report

0

You’ll want to look into closures. Here’s an example:

let var = null
let next = null
const myFunc = (newnext) => {
  var = next
  next = newnext
  return var
}

Sorry for the formatting, I’m on mobile

about 4 years ago · Juan Pablo Isaza Report

0

the function alertMyPreviousParam, returns a new function, and outputs the previous input. This can be achieved as follows:

let alertMyPreviousParam = (x) => (y) => {
  console.log(x);
  return alertMyPreviousParam(y);
}
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!