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

212
Views
how can I use a variable value to define a new variable in JavaScript?

I have a list

const StudentCode = {
  Jack: 32658495,
  Rose: 35621548,
  Lita: 63259547,
  Seth: 27956431,
  Cathy: 75821456,
};

how can I use these peoples numbers if their names matches another variable result?

We have

var name = *selected server-side student names*

and by these * signs I mean it's a great list that name gives up but it only gives up one name out of that list at the time we call it.

If one of these students is selected by name variable, how can I use the number defined in front of that name in const StudentCode to generate a url?

Suppose you get Rose! Then the number for Rose is: 35621548 and the url for example will be https://www.35621548.com. What code can we use to generate this url for example in console?

console.log(url)
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Use this:

if (StudentCode.hasOwnProperty(name){
    const url = `https://www.${StudentCode[name]}.com`
}
about 4 years ago · Santiago Trujillo Report

0

This function will return you the url on the basis of the student name that is passed in this function.

function returnURL(studentName){
    const StudentCode = {
      Jack: 32658495,
      Rose: 35621548,
      Lita: 63259547,
      Seth: 27956431,
      Cathy: 75821456,
    };
    if (!!!StudentCode[studentName]) return "";
    return "https://" + StudentCode[studentName] + ".com";
}

console.log(returnURL("Rose"));

Hope, this helps!!

about 4 years ago · Santiago Trujillo Report

0

export const studentUrlModule = (function() {
   const studentCode = {
     Jack: 32658495,
     Rose: 35621548,
     Lita: 63259547,
     Seth: 27956431,
     Cathy: 75821456,
   };

   const url = 'https://$code.com'

   const generateUrl = function(code = '') {
     if (!url) {
       throw new Error('Url is not defined')
     } else if (!code) {
       throw new Error('Code is not defined')
     };

     return url.replace('$code', `${code}`);
   }

   function getCode(name = '') {
     const code = studentCode[name];

     if (!code) {
       throw new Error(`There is no code with name(${name}).`);
     }

     return code;
   }

   function getUrl(name = '') {
     if (!name) {
       throw new Error('StudentName is undefined')
     };

     const code = getCode(name);
     const studentUrl = generateUrl(code);

     return studentUrl;
   }

   return {
     generateUrl,
     getCode,
     getUrl,
   };
 }());

Maybe it helps. If the code was not founded then it throws an error. Please use try, catch error handlers to handle errors.

-- Update I updated the code and as you can see it's a module and you need to import it everywhere you like to use this.

if you are not familiar with modules and how to use them inside the browser check documents.

Mozila documents

Super Simple Start to ESModules in the Browser

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!