Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 80337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:17:13+00:00 2026-05-10T21:17:13+00:00

This is kind of a brainteaser question, since the code works perfectly fine as-is,

  • 0

This is kind of a brainteaser question, since the code works perfectly fine as-is, it just irritates my aesthetic sense ever so slightly. I’m turning to Stack Overflow because my own brain is failing me right now.

Here’s a snippet of code that looks up an address using the Google Maps JS API and places a marker on a map. However, sometimes the initial lookup fails, so I want to repeat the process with a different address.

geocoder.getLatLng(item.mapstring, function(point) {     if (!point) {         geocoder.getLatLng(item.backup_mapstring, function(point) {             if (!point) return;             map.setCenter(point, 13);             map.setZoom(7);             map.addOverlay(new GMarker(point));         })         return;     }     map.setCenter(point, 13);     map.setZoom(7);     map.addOverlay(new GMarker(point)); }) 

(The second parameter to getLatLng is a callback function.)

Of course you can see that the three lines that center and zoom the map and add the marker are duplicated, once in the primary callback and once in the ‘fallback callback’ (ha ha). Can you find a way to express the whole thing without any redundancy? You earn bonus points, and my adulation, if your solution works for an arbitrary number of backup map strings.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:17:13+00:00Added an answer on May 10, 2026 at 9:17 pm

    The other answers are good, but here’s one more option. This allows you to keep the same form you started with but uses the trick of naming your lambda function so that you can refer to it recursively:

    mapstrings = ['mapstring1', 'mapstring2', 'mapstring3'];  geocoder.getLatLng(mapstrings.shift(), function lambda(point) {    if(point) {         // success         map.setCenter(point, 13);         map.setZoom(7);         map.addOverlay(new GMarker(point));     }     else if(mapstrings.length > 0) {         // Previous mapstring failed... try next mapstring         geocoder.getLatLng(mapstrings.shift(), lambda);     }     else {         // Take special action if no mapstring succeeds?     } }) 

    The first time the symbol ‘lambda’ is used, it is to introduce it as a new function literal name. The second time it is used, it is a recursive reference.

    function literal naming works in Chrome, and I assume it works in most modern browsers, but I haven’t tested it and I don’t know about older browsers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This kind of code would normally work in PHP, but since the scope is
I have this kind of code: main() { for(i=0;i<100;i++) { if(cond1) func(1); //Just some
I perform this kind of query in my Python code to conn = rdbms.connect(instance=_INSTANCE_NAME,
I have this kind of question. In my form, i got this as a
I have this kind of problem. I have Clojure code contained in Eclipse plug-in
I asked this kind of question before, but didn't receive any good answers, perhaps
I am using this kind of code in JavaScript. if(typeof a[x] != undefined &&
This kind of builds up on Already asked question ... However here, say, I'm
This question is kind of related to our web application and it is bugging
Well this kind of n00b question but I still can't figure it out. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.