Optimiser un algo en javascript

Fermé
Alex - 5 mars 2012 à 12:26
Bonjour,

Je travaille sur une application web , dont je voudrais faire un questionnaire à un client, je lui propose de répondre à un certain nombre de questions conditionnées (càd une fois le client réponde par une réponse conditionnée l'application doit afficher une question fille qui dépende de la réponse cochée ) voici un code en javascipt que je viens d'élaborer et que je voudrais l'optimiser :


question = {label,position,multiplesResponse,comment,answers[],subQuestions[]}

subQuestion = { label, position, multipleResponse,comment,linkedAnswers[],answers[]}




for (var i = 0; i < question.subQuestions.length; i++)
	{
		if (typeof question.subQuestions[i].linkedAnswers !== "undefined")
		{
			for (var j = 0; j < question.subQuestions[i].linkedAnswers.length; j++)
			{
				if (question.subQuestions[i].linkedAnswers[j] == position)
				{
						var exist = -1;
						for (var k = 0; k < questionInput.subQuestions.length; k++)
						{
							if (questionInput.subQuestions[k].json.position == question.subQuestions[i].position)
							{
								exist = k;
								break;
							}
						}
						if (input.is(':checked') && (exist == -1))
						{
							var subQuestionInput = that.getQuestionDom(question.subQuestions[i]);
							subQuestionsContainer.append(subQuestionInput.field);
							questionInput.subQuestions.push(subQuestionInput);
						}
						if (!input.is(':checked') && exist != -1)
						{
							questionInput.subQuestions[exist].field.remove();
							questionInput.subQuestions.splice(exist, 1);
						}
				}
			}		



et merci d'avance !
A voir également: