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

293
Views
Si es verdadero, devuelve una cadena; de lo contrario, devuelve otra cadena.

El código contiene errores: SyntaxError no capturado: token inesperado 'if' en indefinido: 18 : 22

Al subir a Greasyfork tengo ese error. El archivo javascript es:

 // ==UserScript== // @name Docubuilder // @namespace Editors // @match *://* // @grant none // @version 1.0 // @author Josiah // @description Josiah's Docubuilder builds anything on the current document. This also works on HTML files! // ==/UserScript== console.log("Starting to make builder's button so hang on!") function buildermain() { tobuild = prompt("Insert any valid HTML element, cancel or input nothing and a tab will open with a list of HTML elements.") if (tobuild == '' || tobuild == null) { window.open("https://developer.mozilla.org/en-US/docs/Web/HTML/Element") } else { mynew = document.createElement(tobuild) myhtml = prompt("Text to display...") mynew.innerHTML = if (!(myhtml == null)) { return myhtml } else { return 'A random ' + tobuild + '.'} document.body.insertBefore(mynew, null) alert("Done! Check the bottom of the page!") } } const builderbutton = document.createElement('button') builderbutton.onClick = "buildermain()" builderbutton.innerHTML = "Start building some stuff!" document.body.insertBefore(builderbutton, document.body.firstChild)
Tal vez fue el si, así que estoy tratando de encontrar una manera de devolver una cadena a una variable en una sola línea.

¿Alguna ayuda aquí?

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

0

El operador condicional (ternario) puede ayudar: mynew.innerHTML = (!(myhtml == null)) ? myhtml : 'A random ' + tobuild + '.';

about 4 years ago · Juan Pablo Isaza Report

0

El problema es que if() no devuelve un valor. No asignará un valor a la propiedad innerHTML .

Hay algunas opciones para solucionar esto.

  1. Crear una función

     function buildermain() { // [...] mynew.innerHTML = getHtml(myhtml, tobuild); // [...] } const getHtml = (myhtml, tobuild) => { if (myhtml !== null) { return myhtml; } return `A random ${tobuild}.`; }
  2. Usar el operador ternario

     function buildermain() { // [...] mynew.innerHTML = myhtml !== null ? myhtml : `A random ${tobuild}.`; // [...] }
  3. Use if, else (correcto) sin asignación directa a innerHtml

     function buildermain() { // [...] if (myhtml !== null) { mynew.innerHTML = myhtml; } else { mynew.innerHTML = `A random ${tobuild}.`; } // [...] }
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!