Here i have javascript code where is 'animals' object and 'people' array of objects. User selects the data from select box and accordingly table fills with selected data(which is array of objects) and when user wants to reset everything to the state before selected data, user just clicks 'backtozero' button. the code is a little complicated to understand for beginners specially two 'for loops'(i know foreach but maybe someting else ?) and also information which is inside of 'addEventListeners function' any suggestion for making whole thing easier to read for beginners ? feel free to make changes :
let animals
let animalCols = ['Animal', 'Animal 2']
let peopleCols = ['Person', 'Person 2']
function myFunction() {
paivitys(animals, animalCols)
}
function paivitys(dataa, arvvoja) {
console.log(dataa);
//----
if (dataa.hasOwnProperty("animal")) {
document.getElementById("1name").innerHTML = dataa.animal;
} else {
document.getElementById("1name").innerHTML = dataa.person;
}
//----
if (dataa.hasOwnProperty("animal2")) {
document.getElementById("2name").innerHTML = dataa.animal2;
} else {
document.getElementById("2name").innerHTML = dataa.person2;
}
document.getElementById("1name1").innerHTML = arvvoja[0];
document.getElementById("2name1").innerHTML = arvvoja[1];
//-----
document.getElementById("id").innerHTML = dataa.id;
}
function paivitaselekt(araytassa, arvvoja) {
var i, j;
for (i = 0; i < araytassa.length; i++) {
var ssellecct = document.getElementById("Select");
var oppttion = document.createElement("option");
for (j = 0; j < arvvoja.length; j++) {
oppttion.textContent += araytassa[i][arvvoja[j]] + " ";
}
ssellecct.appendChild(oppttion);
}
}
animals = {
"animal": "tiger",
"animal2": "lion",
"id": "54321",
"dole": {
"Key": "fhd699f"
}
}
paivitys(animals, animalCols);
let infoarray;
people = [{
"person": "kaka",
"person2": "julle",
"id": "9874",
},
{
"person": "Ronaldo",
"person2": "jussi",
"id": "65555",
}
]
infoarray = people;
paivitaselekt(infoarray, ["person", "id"]);
document.getElementById("Select").addEventListener("change", function(event) {
const chosenid = event.target.value.split(" ")[1];
const choseninfo = infoarray.filter((dataa) => dataa.id === chosenid)[0];
paivitys(choseninfo, peopleCols);
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
/>
<link
rel="stylesheet"
href="https://use.fontawesome.com/releases/v5.6.3/css/all.css"
integrity="sha384UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/"
crossorigin="anonymous"
/>
<style>
</style>
</head>
<body>
<div class="">
<table class="table ">
<thead>
<tr>
<th id="1name1" class="table-success">Animal</th>
<th id="2name1" class="table-success">Animal</th>
<th class="table-success">id</th>
</tr>
</thead>
<tbody>
<th id="1name"></th>
<th id="2name"></th>
<th id="id"></th>
</tbody>
</table>
<select id="Select" ></select>
<button onclick="myFunction()">backtozero</button>
</div>
</body>
</html>
-
1\$\begingroup\$ codereview.stackexchange.com/q/240745 ?? \$\endgroup\$CertainPerformance– CertainPerformance2020年04月28日 20:30:09 +00:00Commented Apr 28, 2020 at 20:30
-
2\$\begingroup\$ Does this answer your question? Suggestion for improving this code to be more readable and easy for beginners? \$\endgroup\$CertainPerformance– CertainPerformance2020年04月28日 20:31:34 +00:00Commented Apr 28, 2020 at 20:31
-
2\$\begingroup\$ Better to edit the earlier identical question to make it clearer rather than to post a new one \$\endgroup\$CertainPerformance– CertainPerformance2020年04月28日 21:44:29 +00:00Commented Apr 28, 2020 at 21:44
-
\$\begingroup\$ (You could try and use a spelling checker.) \$\endgroup\$greybeard– greybeard2020年04月29日 05:15:39 +00:00Commented Apr 29, 2020 at 5:15
-
1\$\begingroup\$ @Peilonrayz Yes, but the proper way to handle this would be for the original question to be improved with the updates and go through the reopen process. \$\endgroup\$pacmaninbw– pacmaninbw ♦2020年04月29日 13:06:23 +00:00Commented Apr 29, 2020 at 13:06
1 Answer 1
- Indent your code!
Naming convention
- Only use English
- paivitys -> update -> though really this should be with a subject, so
updateObjekt
? - dataa -> data
- arvvoja -> values
- paivitaselekt -> updateSelection
- paivitys -> update -> though really this should be with a subject, so
- Dont mispel
- ssellecct -> selectElement
- oppttion -> option
Use lowerCamelCase
- infoarray -> infoArray
- chosenid -> chosenId
- choseninfo -> chosenInfo
myFunction -> this is an unfortunate name, it does not say anything about its functionality
- The names of your HTML elements id's can be improved
- Only use English
- Dont mix
var
andconst
/let
- Don't
console.log()
in production code - Comments should improve understanding,
//------
does not do that, just use newlines/whitespace - Dont assign onclick events in html with
onclick
, useaddEventLister()
- It seems
people
is a global variable, don't create global variables - It seems you create
people
andinfoarray
, then assignpeople
toinfoarray
, and then never usepeople
again. It does not seem to make sense