Estoy tratando de resaltar SOLO las variables: principal, tasa, interés, año en la declaración de devolución de la función de cálculo. Se requiere de mí que todo el estilo debe estar solo en el archivo CSS. No se permiten hojas de estilo en línea o internas. Todo el código debe estar en el archivo JavaScript solamente. Creé la clase de resaltado en CSS, pero esto resalta toda la oración y no sé cómo resaltar solo partes de una declaración de devolución sin usar en línea.
¿Podría alguien ayudarme/guiarme para saber cómo se hace porque soy nuevo en el uso de estos idiomas? Sería muy apreciado.
Encuentre el código HTML, CSS y JavaScript a continuación.
CÓDIGO HTML
<!--- On clicking the button it computes the interest by calling the compute() function ---> <button onclick="compute()">Compute Interest</button><br><br> <!--- Show computation result ---> <span id="result" class = "highlight"></span>CÓDIGO CSS
/* Highlights the result */ .highlight { background-color: yellow; }CÓDIGO JAVASCRIPT
/* Function that computes and returns the result */ function compute() { /* some code */ return document.getElementById("result").innerHTML="If you deposit "+principal+",\<br\>at an interest rate of "+rate+"%\<br\>You will receive an amount of "+interest+",\<br\>in the year "+year+"\<br\>"; }Para que esto funcione, necesitaría elementos de intervalo con el resaltado de clase alrededor de sus variables. Para la semántica, podría ser mejor si el elemento con el resultado de id es un elemento p o algo similar.
Además, como señaló @ user1599011, su Javascript necesita hacer algo, necesita ejecutar un comando, no devolverlo.
Una solución de trabajo:
let principal = '1000'; let rate = '10'; let interest = '100'; let year = '2022'; /* Function that computes and returns the result */ function compute() { document.getElementById("result").innerHTML= 'If you deposit <span class="highlight">' +principal+ '</span>, <br>at an interest rate of <span class="highlight">' +rate+ '%</span><br>You will receive an amount of <span class="highlight">' +interest+ '</span>, <br>in the year <span class="highlight">' +year+ '</span><br>'; } /* Highlights the result */ .highlight { background-color: yellow; } <!--- On clicking the button it computes the interest by calling the compute() function ---> <button onclick="compute()">Compute Interest</button> <!--- Show computation result ---> <p id="result"></p>