I want to optimize this, but i don't know how. I tried with a "for", but i couldn't. Is there any function to shrink this ? Sorry if the response is easy, i'm a newbie with coding.
if (avis1 == 1) {
avis1Phrase = 'Très insatisfait';
}
if (avis1 == 2) {
avis1Phrase = 'Insatisfait';
}
if (avis1 == 3) {
avis1Phrase = 'Moyen';
}
if (avis1 == 4) {
avis1Phrase = 'Satisfait';
}
if (avis1 == 5) {
avis1Phrase = 'Très satisfait';
}
if (avis2 == 1) {
avis2Phrase = 'Très insatisfait';
}
if (avis2 == 2) {
avis2Phrase = 'Insatisfait';
}
if (avis2 == 3) {
avis2Phrase = 'Moyen';
}
if (avis2 == 4) {
avis2Phrase = 'Satisfait';
}
if (avis2 == 5) {
avis2Phrase = 'Très satisfait';
}
if (avis3 == 1) {
avis3Phrase = 'Très insatisfait';
}
if (avis3 == 2) {
avis3Phrase = 'Insatisfait';
}
if (avis3 == 3) {
avis3Phrase = 'Moyen';
}
if (avis3 == 4) {
avis3Phrase = 'Satisfait';
}
if (avis3 == 5) {
avis3Phrase = 'Très satisfait';
}
if (avis4 == 1) {
avis4Phrase = 'Très insatisfait';
}
if (avis4 == 2) {
avis4Phrase = 'Insatisfait';
}
if (avis4 == 3) {
avis4Phrase = 'Moyen';
}
if (avis4 == 4) {
avis4Phrase = 'Satisfait';
}
if (avis4 == 5) {
avis4Phrase = 'Très satisfait';
}
if (avis5 == 1) {
avis5Phrase = 'Très insatisfait';
}
if (avis5 == 2) {
avis5Phrase = 'Insatisfait';
}
if (avis5 == 3) {
avis5Phrase = 'Moyen';
}
if (avis5 == 4) {
avis5Phrase = 'Satisfait';
}
if (avis5 == 5) {
avis5Phrase = 'Très satisfait';
}
if (avis6 == 1) {
avis6Phrase = 'Très insatisfait';
}
if (avis6 == 2) {
avis6Phrase = 'Insatisfait';
}
if (avis6 == 3) {
avis6Phrase = 'Moyen';
}
if (avis6 == 4) {
avis6Phrase = 'Satisfait';
}
if (avis6 == 5) {
avis6Phrase = 'Très satisfait';
}
if (avisGeneral == 1) {
avisGeneralPhrase = 'Très insatisfait';
}
if (avisGeneral == 2) {
avisGeneralPhrase = 'Insatisfait';
}
if (avisGeneral == 3) {
avisGeneralPhrase = 'Moyen';
}
if (avisGeneral == 4) {
avisGeneralPhrase = 'Satisfait';
}
if (avisGeneral == 5) {
avisGeneralPhrase = 'Très satisfait';
}
Using an array should help a lot. Code example (incomplete):
var avis1 = 5; var phrases = ['Très insatisfait', 'Insatisfait', 'Moyen', 'Satisfait', 'Très satisfait']; var avis1Phrase = phrases[avis1 - 1]; // javascript arrays always start at [0] console.log(avis1Phrase);
Maybe you could change your values from 0 to 4 instead 1 to 5 and use an array as following:
var phrase = [
'Très insatisfait',
'Insatisfait',
'Moyen',
'Satisfait',
'Très satisfait'
];
Then you can get the appropiate phrase by using:
var avis1Phrase = phrase[avis1];
Where avis1 should be a value between 0 to 4.
So, if you don't want to change your values then you could use a JSON hash instead of array as follow:
var phrase = {
1:'Très insatisfait',
2:'Insatisfait',
3:'Moyen',
4:'Satisfait',
5:'Très satisfait'
};
In this case, you can get the appropiate phrase by using:
var avis1Phrase = phrase[avis1];
Where avis1 should be a value between 1 to 5.
Also, maybe you should to consider make an array or JSON hash for your vaiables avis1, ..., avis6, avisGeneral. I mean, something like this:
var avis = {
1: my1stValue,
...
6: my6thValue,
General: myGeneralVaulue
};
i think you can possible use a switch statement
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
You could use an object.
var text = {
1: 'Très insatisfait',
2: 'Insatisfait',
3: 'Moyen',
4: 'Satisfait',
5: 'Très satisfait'
};
Access
avis1Phrase = text[avis1];
// Since all the phrases for all the 'avis' are the same, you can reuse the same array.
var phrases = [
'Très insatisfait',
'Insatisfait',
'Moyen',
'Satisfait',
'Très satisfait'
],
// index starts at 0, so avis1 minus 1 gives the correct index.
avis1Phrase = phrases[ avis1 - 1 ],
avis2Phrase = phrases[ avis2 - 1 ],
avis3Phrase = phrases[ avis3 - 1 ],
avis4Phrase = phrases[ avis4 - 1 ],
avis5Phrase = phrases[ avis5 - 1 ],
avis6Phrase = phrases[ avis6 - 1 ],
avisGeneralPhrase = phrases[ avisGeneral - 1 ];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.