Foros del Web » Programando para Internet » Javascript »

Resultado dependiendo de valor maximo

Estas en el tema de Resultado dependiendo de valor maximo en el foro de Javascript en Foros del Web. Hola, me gustaria que me orientaran en como puedo hacer lo siguiente: El usario responde in group de preguntas selecionando una de Las opciones (valor ...
  #1 (permalink)  
Antiguo 05/03/2018, 10:42
Avatar de aeberdion  
Fecha de Ingreso: enero-2009
Ubicación: Petal, MS
Mensajes: 136
Antigüedad: 15 años, 2 meses
Puntos: 8
Resultado dependiendo de valor maximo

Hola, me gustaria que me orientaran en como puedo hacer lo siguiente:

El usario responde in group de preguntas selecionando una de Las opciones (valor 1-5)

El resultado de determinadas preguntas se human ( digamos preguntas 1 y 3 se suman)

Dependiendo del valor maximo de cada variable se seleciona el resultado

Digamos que hay 10 preguntas, Los resultados de la preguntas 1, 3, 5 se suman y Los resultados de 7, 9 se suman y Los resultados de 2, 4, 6 se suman y Los resultados de 8 y 10 se suman. El resultado final que es una linea de texto de pende de qua results to sea mayor. (el valor del resultado no se muestra)

Espero que sea clara mi explicacion
__________________
Recuerda siempre que eres único… Exactamente igual que todos los demás.
Ideas Sordas
  #2 (permalink)  
Antiguo 05/03/2018, 11:56
Avatar de Fuzzylog  
Fecha de Ingreso: agosto-2008
Ubicación: En internet
Mensajes: 2.511
Antigüedad: 15 años, 7 meses
Puntos: 188
Respuesta: Resultado dependiendo de valor maximo

Puedes usar esto

https://gist.github.com/dtx/3791420

así introduces valores numPregunta y Valor asociado

Luego vas leyendo el hashmap en función de los numPregunta (index) que indicas y vas haciendo la suma y comparando.
__________________
if (fuzzy && smooth) {
fuzzylog = "c00l";
return true;
}
  #3 (permalink)  
Antiguo 05/03/2018, 12:06
Avatar de aeberdion  
Fecha de Ingreso: enero-2009
Ubicación: Petal, MS
Mensajes: 136
Antigüedad: 15 años, 2 meses
Puntos: 8
Respuesta: Resultado dependiendo de valor maximo

Encontre esto en github. el html ya esta modificado para lo que necesito pero sigo con dudas en el js
Cita:
<div id="wrapper">
<h1>What is your spiritual gift?</h1>
<p>Take this questionnaire to find out!</p>

<form id="quiz">
<!-- Question 1 -->
<h2>I like organizing Services</h2>
<!-- Choices -->
<label><input type="radio" name="q1" value="c1">
Almost Never
</label><br />
<label><input type="radio" name="q1" value="c2">
Rarely
</label><br />
<label><input type="radio" name="q1" value="c3">
Sometimes
</label><br />
<label><input type="radio" name="q1" value="c4">
Almost Always
</label><br />
<label><input type="radio" name="q1" value="c5">
Always
</label><br />

<!-- Question 2 -->
<h2>I enjoy starting new churches</h2>
<!-- Choices -->
<label><input type="radio" name="q2" value="c1">
Almost Never
</label><br />
<label><input type="radio" name="q2" value="c2">
Rarely
</label><br />
<label><input type="radio" name="q2" value="c3">
Sometimes
</label><br />
<label><input type="radio" name="q2" value="c4">
Almost Always
</label><br />
<label><input type="radio" name="q2" value="c5">
Always
</label><br />

<!-- Question 3 -->
<h2>Working with my hands is fun for me</h2>
<!-- Choices -->
<label><input type="radio" name="q3" value="c1">
Almost Never
</label><br />
<label><input type="radio" name="q3" value="c2">
Rarely
</label><br />
<label><input type="radio" name="q3" value="c3">
Sometimes
</label><br />
<label><input type="radio" name="q3" value="c4">
Almost Always
</label><br />
<label><input type="radio" name="q3" value="c5">
Always
</label><br />

<!-- Question 4 -->
<h2>I can tell when someone is insincere</h2>
<!-- Choices -->
<label><input type="radio" name="q4" value="c1">
Almost Never
</label><br />
<label><input type="radio" name="q4" value="c2">
Rarely
</label><br />
<label><input type="radio" name="q4" value="c3">
Sometimes
</label><br />
<label><input type="radio" name="q4" value="c4">
Almost Always
</label><br />
<label><input type="radio" name="q4" value="c5">
Always
</label><br />

<!-- Question 5 -->
<h2>I pray for the lost dayly</h2>
<!-- Choices -->
<label><input type="radio" name="q5" value="c1">
Almost Never
</label><br />
<label><input type="radio" name="q5" value="c2">
Rarely
</label><br />
<label><input type="radio" name="q5" value="c3">
Sometimes
</label><br />
<label><input type="radio" name="q5" value="c4">
Almost Always
</label><br />
<label><input type="radio" name="q5" value="c5">
Always
</label><br />

<!-- Question 6 -->
<h2>Encouraging others is a high priority in my life</h2>
<!-- Choices -->
<label><input type="radio" name="q6" value="c1">
Almost Never
</label><br />
<label><input type="radio" name="q6" value="c2">
Rarely
</label><br />
<label><input type="radio" name="q6" value="c3">
Sometimes
</label><br />
<label><input type="radio" name="q6" value="c4">
Almost Always
</label><br />
<label><input type="radio" name="q6" value="c5">
Always
</label><br />

<button type="submit" id="submit" onclick="tabulateAnswers()">Submit Your Answers</button>
<button type="reset" id="reset" onclick="resetAnswer()">Reset</button>
</form>


<div id="answer"></div>
</div>
</body>
</html>
Lo que quiero que evalue es el valor total de la suma de ciertas preguntas y me de el resultado correspondiente ( digamos que q1 + q3 = primer resultado y q2 + q4 = segundo resultado)
Cita:
// function to calculate the result of the survey
function tabulateAnswers() {
// initialize variables for each choice's score
var c1score = 0;
var c2score = 0;
var c3score = 0;
var c4score = 0;
var c5score = 0

// get a list of the radio inputs on the page
var choices = document.getElementsByTagName('input');
// loop through all the radio inputs
for (i=0; i<choices.length; i++) {
// if the radio is checked..
if (choices[i].checked) {
// add 1 to that choice's score
if (choices[i].value == 'c1') {
c1score = c1score + 1;
}
if (choices[i].value == 'c2') {
c2score = c2score + 2;
}
if (choices[i].value == 'c3') {
c3score = c3score + 3;
}
if (choices[i].value == 'c4') {
c4score = c4score + 4;
}
if (choices[i].value == 'c5') {
c4score = c5score + 5;
}
// If you add more choices and outcomes, you must add another if statement below.
}
}

// Find out which choice got the highest score.
// If you add more choices and outcomes, you must add the variable here.
var maxscore = Math.max(c1score,c2score,c3score,c4score,c5score);

// Display answer corresponding to that choice
var answerbox = document.getElementById('answer');
if (c1score == maxscore) {
alert ("You are a computer researcher! You will enjoy developing algorithms, and doing things with computers no one else has done before. For example, researchers sent a robot to the moon, built a computer to beat the best humans in Jeopardy, and are creating robots to do your chores for you. Computer researchers typically go to college and work at universities, or as a part of the research and development team in companies.");
}
if (c2score == maxscore) {
alert ("You are an altruistic coder! You love to help people and feel the positive impact of your work every day. Altrustic coders are out there every day making the world a better place. Computer scientists write software to more effectively help doctors diagnose illnesses such as cancer, connect people in third world countries to education and medical resources on the internet, code websites and software for nonprofit organizations, and much more!");
}
if (c3score == maxscore) {
alert ("You are a developer! Developers create games, apps, social media, movies, and all sorts of fun programs that people enjoy. These coders work on projects such as Minecraft, Poptropica, and Youtube. Developers need sharp coding skills, are great debuggers, and need to work well in a team of other developers.");
}

if (c4score == maxscore) {
alert ("You are the future CEO of a new startup! You enjoy taking risks and building the next big thing that no one has even thought of before. For example, billionare Mark Zuckerberg founded Facebook in 2004, a project he started inside his dorm room in college which eventually turned into a social networking revolution that changed the world.");
}

if (c5score == maxscore) {
alert ("You are the future CEO of a new startup! You enjoy taking risks and building the next big thing that no one has even thought of before. For example, billionare Mark Zuckerberg founded Facebook in 2004, a project he started inside his dorm room in college which eventually turned into a social networking revolution that changed the world.");
}
// If you add more choices, you must add another response below.
}
__________________
Recuerda siempre que eres único… Exactamente igual que todos los demás.
Ideas Sordas

Última edición por aeberdion; 05/03/2018 a las 20:06
  #4 (permalink)  
Antiguo 06/03/2018, 08:35
Avatar de Fuzzylog  
Fecha de Ingreso: agosto-2008
Ubicación: En internet
Mensajes: 2.511
Antigüedad: 15 años, 7 meses
Puntos: 188
Respuesta: Resultado dependiendo de valor maximo

Bien eso te sirve de base.

Puedes dar a tus inputs agrupables el mismo valor del atributo name.

Luego

var choices_1_3 = document.getElementsByName('grupoq_1_3');
// recorres y sumas sus valores
var choices_2_4 = document.getElementsByName('grupoq_2_4');
// recorres y sumas sus valores

y luego

var maxscore = Math.max(grupoq_1_3_score,grupoq_2_4_score);
__________________
if (fuzzy && smooth) {
fuzzylog = "c00l";
return true;
}
  #5 (permalink)  
Antiguo 06/03/2018, 11:04
Avatar de aeberdion  
Fecha de Ingreso: enero-2009
Ubicación: Petal, MS
Mensajes: 136
Antigüedad: 15 años, 2 meses
Puntos: 8
Respuesta: Resultado dependiendo de valor maximo

entiendo lo que dices pero no estoy seguro de como comprobar los resultados

<QUOTE> var choices = document.getElementsByTagName('input');
// loop through all the radio inputs
for (i=0; i<choices.length; i++) {
// if the radio is checked..
if (choices[i].checked) {
// add 1 to that choice's score
if (choices[i].value == 'c1') {
c1score = c1score + 1;
}
if (choices[i].value == 'c2') {
c2score = c2score + 2;
}
if (choices[i].value == 'c3') {
c3score = c3score + 3;
}
if (choices[i].value == 'c4') {
c4score = c4score + 4;
}
if (choices[i].value == 'c5') {
c5score = c5score + 5;
}
// If you add more choices and outcomes, you must add another if statement below.
}
}
</QUOTE>
var c1, c2, c3, c4, c5 son las respuestas y cada una tiene un valor diferente como hago para que los valores de la preguntas que necesitan ser sumadas se sumen dependiendo de las respuestas
__________________
Recuerda siempre que eres único… Exactamente igual que todos los demás.
Ideas Sordas
  #6 (permalink)  
Antiguo 06/03/2018, 11:11
Avatar de Fuzzylog  
Fecha de Ingreso: agosto-2008
Ubicación: En internet
Mensajes: 2.511
Antigüedad: 15 años, 7 meses
Puntos: 188
Respuesta: Resultado dependiendo de valor maximo

No vale con copiar las cosas sin más, hay que entender lo que hacen y hacia donde quieres llegar para poder adaptarlo.

luego investiga sobre los elementos para ver que propiedades puedes utilizar para lograr tu objetivo. Haz pruebas esperando un resultado X y si no se cumple averigua si está mal tu predicción o el desarrollo (tienes un maravilloso F12 en el chrome y el console.log de javascript). Y a partir de ahí avanza.
__________________
if (fuzzy && smooth) {
fuzzylog = "c00l";
return true;
}

Etiquetas: dependiendo, maximo, resultado, valor
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 15:10.