Foros del Web » Programando para Internet » Javascript »

Problema con secciones

Estas en el tema de Problema con secciones en el foro de Javascript en Foros del Web. Buenas. Verán, tengo el siguiente código (léanlo después de leer la explicación que hay al terminar el código) : Código: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML ...
  #1 (permalink)  
Antiguo 02/03/2012, 22:07
 
Fecha de Ingreso: agosto-2010
Mensajes: 14
Antigüedad: 13 años, 7 meses
Puntos: 0
Problema con secciones

Buenas.

Verán, tengo el siguiente código (léanlo después de leer la explicación que hay al terminar el código) :

Código:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
	
	<head>
		
			<title>Calculadora para WoW PowerLeveling</title>
		
			<script language="JavaScript">
				
					$res = 0;
					$pSec1 = 1;
					$pSec2 = 3;
					$pSec3 = 5;
					$pSec4 = 8;
					$pSec5 = 10;
					$pSec6 = 14;
					$pSec7 = 18;
					$pSec8 = 25;
					$nSec1 = 0.10;
					$nSec2 = 0.30;
					$nSec3 = 0.50;
					$nSec4 = 0.80;
					$nSec5 = 1.10;
					$nSec6 = 1.50;
					$nSec7 = 2;
					$nSec8 = 3;
					$nSec9 = 4.5;
					$sec1 = 10;
					$sec2 = 20;
					$sec3 = 30;
					$sec4 = 40;
					$sec5 = 50;
					$sec6 = 60;
					$sec7 = 70;
					$sec8 = 80;
					$sec9 = 85;
					
					
					function iden() {
						
						$ini = document.getElementById("4");
						$inicial = $ini.value;
					    $fin = document.getElementById("5");
					    $final = $fin.value;
					    
					    
						if($inicial<=$sec1) {
							
							sec1();
							
						}
						else if($inicial<=$sec2 && $inicial>$sec1) {
							
							
							sec2();
							
						}
						
						else if($inicial<=$sec3 && $inicial>$sec2) {
							
							sec3();
							
						}
						
						else if($inicial<=$sec4 && $inicial>$sec3) {
							
							sec4();
							
						}
						
						else if($inicial<=$sec5 && $inicial>$sec4) {
							
							sec5();
							
						}
						
						else if($inicial<=$sec6 && $inicial>$sec5) {
							
							sec6();
							
						}
						
						else if($inicial<=$sec7 && $inicial>$sec6) {
							
							sec7();
							
						}
						
						else if($inicial<=$sec8 && $inicial>$sec7) {
							
							sec8();
							
						}
						
						else if($inicial<=$sec9 && $inicial>$sec8) {
							
							sec9();
							
						}
					}
					
					function sec1() {
						
						if($final>$sec1) {
							
							$aux = $inicial;
							$aux = $sec1 - $aux;
							$res = $res + ($aux * $nSec1);
							finalizar();
							
						}
						else {
							
							$aux = $final - $inicial;
							$res = $res + ($aux * $nSec1);
							document.formulario.precio.value = $res;
							
						}
												
					}
					function sec2() {
						
						if($final>$sec2) {
							
							$aux = $inicial;
							$aux = $sec2 - $aux;
							$res = $res + ($aux * $nSec2);
							finalizar();
							
						}
						else {
							
							$aux = $final - $inicial;
							$res = $res + ($aux * $nSec2);
							document.formulario.precio.value = $res;
							
						}
						
						
					}
					
					function sec3() {
						
						if($final>$sec3) {
							
							$aux = $inicial;
							$aux = $sec3 - $aux;
							$res = $res + ($aux * $nSec3);
							finalizar();
							
						}
						else {
							
							$aux = $final - $inicial;
							$res = $res + ($aux * $nSec3);
							document.formulario.precio.value = $res;
							
						}
						
					}
					
					function sec4() {
						
						if($final>$sec4) {
							
							$aux = $inicial;
							$aux = $sec4 - $aux;
							$res = $res + ($aux * $nSec4);
							finalizar();
							
						}
						else {
							
							$aux = $final - $inicial;
							$res = $res + ($aux * $nSec4);
							document.formulario.precio.value = $res;
							
						}
						
					}
					
					function sec5() {
						
						if($final>$sec5) {
							
							$aux = $inicial;
							$aux = $sec5 - $aux;
							$res = $res + ($aux * $nSec5);
							finalizar();
							
						}
						else {
							
							$aux = $final - $inicial;
							$res = $res + ($aux * $nSec5);
							document.formulario.precio.value = $res;
							
						}
						
					}
					
					function sec6() {
						
						if($final>$sec6) {
							
							$aux = $inicial;
							$aux = $sec6 - $aux;
							$res = $res + ($aux * $nSec6);
							finalizar();
							
						}
						else {
							
							$aux = $final - $inicial;
							$res = $res + ($aux * $nSec6);
							document.formulario.precio.value = $res;
							
						}
						
					}
					
					function sec7() {
						
						if($final>$sec7) {
							
							$aux = $inicial;
							$aux = $sec7 - $aux;
							$res = $res + ($aux * $nSec7);
							finalizar();
							
						}
						else {
							
							$aux = $final - $inicial;
							$res = $res + ($aux * $nSec7);
							document.formulario.precio.value = $res;
							
						}
						
					}
					
					function sec8() {
						
						if($final>$sec8) {
							
							$aux = $inicial;
							$aux = $sec8 - $aux;
							$res = $res + ($aux * $nSec8);
							finalizar();
							
						}
						else {
							
							$aux = $final - $inicial;
							$res = $res + ($aux * $nSec8);
							document.formulario.precio.value = $res;
							
						}
						
					}
					
					function sec9() {
									
							$aux = $final - $inicial;
							$res = $res + ($aux * $nSec9);
							document.formulario.precio.value = $res;
							
						
						
					}
					
					function finalizar() {
						
						if($final<=$sec2 && $final>$sec1) {
							$res = $res + $pSec1;
							$aux = $final;
							$aux = $aux - $sec1;
							$res = $res + ($aux * $nSec2);
							
							document.formulario.precio.value = $res;
						}
						
						else if($final<=$sec3 && $final>$sec2) {
							$res = $res + $pSec1 + $pSec2;
							$aux = $final;
							$aux = $aux - $sec2;
							$res = $res + ($aux * $nSec3);
							
							document.formulario.precio.value = $res;
							
						}
						
						else if($final<=$sec4 && $final>$sec3) {
							$res = $res + $pSec1 + $pSec2 + $pSec3;
							$aux = $final;
							$aux = $aux - $sec3;
							$res = $res + ($aux * $nSec4);
							
							document.formulario.precio.value = $res;
							
						}
						
						else if($final<=$sec5 && $final>$sec4) {
							$res = $res + $pSec1 + $pSec2 + $pSec3 + $pSec4;
							$aux = $final;
							$aux = $aux - $sec4;
							$res = $res + ($aux * $nSec5);
							
							document.formulario.precio.value = $res;
							
						}
						
						else if($final<=$sec6 && $final>$sec5) {
							$res = $res + $pSec1 + $pSec2 + $pSec3 + $pSec4 + $pSec5;
							$aux = $final;
							$aux = $aux - $sec5;
							$res = $res + ($aux * $nSec6);
							
							document.formulario.precio.value = $res;
							
						}
						
						else if($final<=$sec7 && $final>$sec6) {
							$res = $res + $pSec1 + $pSec2 + $pSec3 + $pSec4 + $pSec5 + $pSec6;
							$aux = $final;
							$aux = $aux - $sec6;
							$res = $res + ($aux * $nSec7);
							
							document.formulario.precio.value = $res;
							
						}
						
						else if($final<=$sec8 && $final>$sec7) {
							$res = $res + $pSec1 + $pSec2 + $pSec3 + $pSec4 + $pSec5 + $pSec6 + $pSec7;
							$aux = $final;
							$aux = $aux - $sec7;
							$res = $res + ($aux * $nSec8);
							
							document.formulario.precio.value = $res;
							
							
						}
						else if($final<=$sec9 && $final>$sec8) {
							$res = $res + $pSec1 + $pSec2 + $pSec3 + $pSec4 + $pSec5 + $pSec6 + $pSec7 + $pSec8;
							$aux = $final;
							$aux = $aux - $sec8;
							$res = $res + ($aux * $nSec9);
							
							document.formulario.precio.value = $res;
							
							
						}
						
					}
				
			</script>
		
	</head>
	
	<body>
		<center>
			<form name="formulario">
				
					<br />
					Nombre del personaje: &nbsp;<input type="text" id="1"> <br />
					Servidor: &nbsp;<input type="text" id="2"> <br />
					Facción: &nbsp;<input type="text" id="3"> <br />
					Nivel inicial: &nbsp;<input type="text" id="4"> <br />
					Nivel final: &nbsp;<input type="text" id="5"> <br />
					Correo electrónico: &nbsp;<input type="text" id="6"> <br /><br />
					
					<input type="button" value="Calcular" onclick="iden()"> <br /><br />
					
					Precio final: &nbsp;<input type="text" readonly="true" name="precio">
				
			</form>
		</center>
	</body>
	
</html>
Explico: Básicamente, introduces un nivel inicial y un nivel final. Con eso, el script primero calcula en qué sección se encuentra la variable $inicial (mediante los else if primeros) y una vez hecho eso, llama a la función correspondiente (hay una por variable). Dentro, creo una variable auxiliar ($aux) y resto la cantidad de niveles totales que tiene la sección (definido al principio). Luego, guardo en $res (resultado) el cálculo de multiplicar los niveles a subir en esa sección ($aux * $nSecX, donde la variable $nSecX es el precio por nivel en esa sección de niveles). Tras eso, llama a la función finalizar(), y he aquí el problema:

La idea es que realice una operación similar a la que se hace con $inicial, pero además sumando el precio de cada paquete de niveles (es decir, del 10 al 20 es un paquete con un precio definido en la variable $pSecX) y he ahí mi problema; No sé cómo hacer que no cuente desde cero para encontrar la sección en la que está $final (ya que entonces suma precios de paquetes que no debería sumar), ya que debería de empezar a contar desde el número albergado en $inicial.

Perdón por el embrollo :P Espero que puedan ayudarme.

Un saludo, gracias de antemano.

Etiquetas: formulario, html, input, secciones
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 06:02.