Duvida Javascript

Asked

Viewed 54 times

-2

The structure below is a guide to how the information is arranged. Implement the function in javascript listAllDoctors() which is responsible for storing the structure below containing the respective names and specialties. Consider that the names and specialties will be listed in the DOM, as an example below, and the listing should remain even before the closure and reopening of the browser.

var doctors = [
'rem',
'clack',
'bruce',
'jack'
];

var especialidades = [
'nefrologia',
'cardiologia',
'ortopedia',
'otorrino'
];

Output Example:

Professionals - Specialties

  • Dr.rem-nephrology
  • Dr.clack-cardiology
  • Dr.Ruce-orthopedics
  • Dr.jack-ENT
  • This seems to be an exercise statement. Did you even try to solve it? Could [Dit] and add your attempt describing the difficulty found?

1 answer

1

Welcome to Stack Overflow! To learn more, take a look at code of conduct.

Did you translate this or is the text so? Information seems to be missing. The statement is not very clear. I believe that the function that does what is asked in the statement would be something like this:

var doctors = [ 'rem', 'clack', 'bruce', 'jack' ];
var especialidades = [ 'nefrologia', 'cardiologia', 'ortopedia', 'otorrino' ];

localStorage.setItem('doctors', JSON.stringify(doctors));
localStorage.setItem('especialidades', JSON.stringify(especialidades));

function listAllDoctors(){
     let doctors = localStorage.getItem('doctors') ? JSON.parse(localStorage.getItem('doctors')) : [];
     let especialidades = localStorage.getItem('especialidades') ? JSON.parse(localStorage.getItem('especialidades')) : [];

     for (let i = 0; i < doctors.length; i++){
         let doctor = doctors[i];
         let espec = especialidades[i];

         document.write("Dr." + doctor + "-" + espec);
     }
}

listAllDoctors();

You can see the example working here: https://jsfiddle.net/adrianprado/3sxf1bc4/


I say that the statement is not clear on some issues:

  1. The statement states that the function listAllDoctors will store but not print in DOM.
  2. Will the arrays with the elements be sent by parameters to the function, or will they be fixed in this example? If they are fixed, the use of Local Storage is discarded for this.
  3. So as in the example I showed, the results will always be the same, but the text does not clearly say what I did.

In short, the function would be this, or some variation of it, but it is already a good starting point.


References:

How to user Local Storage with Javascript

Browser other questions tagged

You are not signed in. Login or sign up in order to post.