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

131
Views
reduce and optimize a function with javascript language

Good morning, I have the following code with the programming language javascript. And the thing is, I've been asked to optimize the function. Reduce the lines of code, that is, simplify it. Any suggestion? Thanks

function translate(box){
    var row = box.substring(1,3);
    var column = box.substring(0,1);
    switch(column){
        case "a":
            return "1"+row;
        case "b":
            return "2"+row;
        case "c":
            return "3"+row;
        case "d":
            return "4"+row;
        case "e":
            return "5"+row;
        case "f":
            return "6"+row;
        case "g":
            return "7"+row;
        case "h":
            return "8"+row;
    }
    registraMoviment(box);
}

So far I haven't tried anything because I don't work with javascript or related languages, but I've been given this assignment anyway. Thank you

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

0

You can use this code:

const input = 'bRow';

function translate(box){
    var row = box.substring(1,3);
    var column = box.substring(0,1);
    
    return (column.charCodeAt(0) - 96) + row;
}

console.log(translate(input));

For more info please read:

  • ASCII table
  • String.prototype.charCodeAt()

P.S. I didn't clearly understand the logic of registraMoviment(box); so I didn't include it in my example

about 4 years ago · Juan Pablo Isaza Report

0

Try this:

function translate(box){
  var row = box.substring(1,3);
  var column = box.substring(0,1);
  var obj = {"a":"1", "b":"2", "c":"3", "d":"4", "e":"5", "f":"6", "g":"7", "h":"8" }
  for(item in obj){
    if(column === item){
      return obj[item] + row
    }
  }
    registraMoviment(box);
}
about 4 years ago · Juan Pablo Isaza Report

0

You can use the ASCII values of your letters to do this more mathy, but it certainly is much less readable.

function translate(box){
    const row = box.substring(1,3);
    const column = box.substring(0,1);
    if(column >= "a" && column <= "h") {
      const ASCII_VALUE_a = 97
      // The +1 is to make it 1-indexed. The -ASCII_VALUE_a is to shift it so that "a" then becomes 1. 
      return `${column.charCodeAt() + 1 - ASCII_VALUE_a}${row}`
    }
    registraMoviment(box);
}

const box = "b13" // expected to become "213"
console.log(translate(box)) // 213

Another solution is Justinas' comment:

function translate(box){
    const row = box.substring(1,3);
    const column = box.substring(0,1);
    const lookup = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6: g: 7, h: 8 }
    if(column in lookup) {
      return lookup[column] + row
    }
    registraMoviment(box);
}

const box = "b13" // expected to become "213"
console.log(translate(box)) // 213

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!