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:
- The statement states that the function
listAllDoctors
will store but not print in DOM.
- 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.
- 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
This seems to be an exercise statement. Did you even try to solve it? Could [Dit] and add your attempt describing the difficulty found?
– Woss