Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
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 Respuestas
Responde la pregunta

0

Use this:

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

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda