Javascript.RU

Создать новую тему Ответ
 
Опции темы Искать в теме
  #1 (permalink)  
Старый 28.07.2016, 17:54
Интересующийся
Отправить личное сообщение для Радиойод Посмотреть профиль Найти все сообщения от Радиойод
 
Регистрация: 28.07.2016
Сообщений: 11

Последовательное сравнение переменных
Имеем некоторую кучу переменных с некоторыми значениями. И хотим последовательно эти переменные сравнить и изменить их значение.
Вопрос: можно ли как-либо оптимизировать такой код:
if (b==a)
document.getElementById("B").value = a + b;
 if (b>c)
document.getElementById("C").value = c + b;
if (c>d)
document.getElementById("D").value = c + d;
if (d>e)
document.getElementById("E").value = d + e;
  if (e>f)
document.getElementById("F").value = e + f;
  if (f>g)
document.getElementById("G").value = f + g;
    if (g>h)
document.getElementById("H").value = g + h;
    if (h>i)
document.getElementById("I").value = h + i;
    if (i>j)
document.getElementById("J").value = i + j;
    if (j>a)
document.getElementById("A").value = j + a;
Ответить с цитированием
  #2 (permalink)  
Старый 28.07.2016, 18:40
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,064

Сообщение от Радиойод
if (b==a)
это условие верно?
Ответить с цитированием
  #3 (permalink)  
Старый 29.07.2016, 09:23
Интересующийся
Отправить личное сообщение для Радиойод Посмотреть профиль Найти все сообщения от Радиойод
 
Регистрация: 28.07.2016
Сообщений: 11

Да, там все условия верны )
Ответить с цитированием
  #4 (permalink)  
Старый 29.07.2016, 09:59
Аватар для ksa
ksa ksa вне форума
CacheVar
Отправить личное сообщение для ksa Посмотреть профиль Найти все сообщения от ksa
 
Регистрация: 19.08.2010
Сообщений: 14,118

Сообщение от Радиойод Посмотреть сообщение
можно ли как-либо оптимизировать такой
Как вариант...

summ('B',b,a,0);
summ('C',b,c);
summ('D',c,d);
summ('E',d,e);
summ('F',e,f);
summ('G',f,g);
summ('H',g,h);
summ('I',h,i);
summ('J',i,j)
summ('A',j,a);
function summ(Id,Val1,Val2,Type) {
	Type=Type||1;
	if (Type==0 && Val1!=Val2) return;
	if (Type==1 && Val1<=Val2) return;
	document.getElementById(Id).value = Val1 + Val2;
};
Ответить с цитированием
  #5 (permalink)  
Старый 29.07.2016, 11:06
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,064

Радиойод,
<!DOCTYPE html>

<html>
<head>
  <title>Untitled</title>
  <meta charset="utf-8">

</head>

<body>
<input type="text" id="A"><br>
<input type="text" id="B"><br>
<input type="text" id="C"><br>
<input type="text" id="D"><br>
<input type="text" id="E"><br>
<input type="text" id="F"><br>
<input type="text" id="G"><br>
<input type="text" id="H"><br>
<input type="text" id="I"><br>
<input type="text" id="J"><br>
<script>
function foo(data)
{
  var str = "ABCDEFGHIJ";
  data.reduce(function(a, b, i) {
  if(a > b || i == 1 && a == b) document.getElementById(str[i]).value = a + b;
  return b
},data.slice(-1)[0])
}
foo([10,10,9,8,7,6,5,4,3,100])
function test(a,b,c,d,e,f,g,h,i,j)
{
   if (b==a)
document.getElementById("B").value = a + b;
 if (b>c)
document.getElementById("C").value = c + b;
if (c>d)
document.getElementById("D").value = c + d;
if (d>e)
document.getElementById("E").value = d + e;
  if (e>f)
document.getElementById("F").value = e + f;
  if (f>g)
document.getElementById("G").value = f + g;
    if (g>h)
document.getElementById("H").value = g + h;
    if (h>i)
document.getElementById("I").value = h + i;
    if (i>j)
document.getElementById("J").value = i + j;  alert([i,j])
    if (j > a)
document.getElementById("A").value = j + a;
}
//test(10,10,9,8,7,6,5,4,3,100)
</script>

</body>
</html>


для проверки
<!DOCTYPE html>

<html>
<head>
  <title>Untitled</title>
  <meta charset="utf-8">

</head>

<body>
<input type="text" id="A"><br>
<input type="text" id="B"><br>
<input type="text" id="C"><br>
<input type="text" id="D"><br>
<input type="text" id="E"><br>
<input type="text" id="F"><br>
<input type="text" id="G"><br>
<input type="text" id="H"><br>
<input type="text" id="I"><br>
<input type="text" id="J"><br>
<script>
function foo(data)
{
  var str = "ABCDEFGHIJ";
  data.reduce(function(a, b, i) {
  if(a > b || i == 1 && a == b) document.getElementById(str[i]).value = a + b;
  return b
},data.slice(-1)[0])
}
//foo([10,10,9,8,7,6,5,4,3,100])
function test(a,b,c,d,e,f,g,h,i,j)
{
   if (b==a)
document.getElementById("B").value = a + b;
 if (b>c)
document.getElementById("C").value = c + b;
if (c>d)
document.getElementById("D").value = c + d;
if (d>e)
document.getElementById("E").value = d + e;
  if (e>f)
document.getElementById("F").value = e + f;
  if (f>g)
document.getElementById("G").value = f + g;
    if (g>h)
document.getElementById("H").value = g + h;
    if (h>i)
document.getElementById("I").value = h + i;
    if (i>j)
document.getElementById("J").value = i + j;  
    if (j > a)
document.getElementById("A").value = j + a;
}
test(10,10,9,8,7,6,5,4,3,100)
</script>

</body>
</html>
Ответить с цитированием
Ответ



Опции темы Искать в теме
Искать в теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Как и где задать цикл? Blondinka Events/DOM/Window 2 10.06.2014 15:29
Избавление от глобальных переменных при проходе алгоритма через несколько ивентов shoo Общие вопросы Javascript 4 13.11.2012 12:07
Назначение переменных из вне qazibum Общие вопросы Javascript 4 16.08.2012 02:00
Значение переменных из JavaScript в PHP pr43unknown Общие вопросы Javascript 3 18.01.2012 18:37
Сравнение переменных diiimonn Общие вопросы Javascript 2 08.11.2010 11:21