2015-07-24 3 views
0

Я относительно новичок в JavaScript и пытаюсь улучшить, поэтому я практиковал и решает некоторые сложные вопросы по CodeFights, это ссылка: https://codefights.com/challenge/5woihS52FBamZvyYg (Я не хотел, чтобы копировать проблему все путь сюда)Анализ игрового алгоритма

Я пробовал пару раз, но не смог, тогда я сдался и начал ждать времени, чтобы увидеть решения.

Это один является кратчайшим решением для JavaScript:

/* CREDITS TO "sinpamov" */ 

function target_game(a) {  
    b = t = 0 
    for (i in a) { 
     c = b + a[i] 
     b = t 
     t = c > t ? c : t 
    } 
    return t 
} 

Конечно, автор пытался сократить все, чтобы получить самое короткое решение, я получить его. У меня нет проблем с пониманием синтаксиса здесь. Но семантически ... Я не понимаю, как работает этот алгоритм и как его анализировать. Я был бы признателен за каждое кристально чистое объяснение, спасибо заранее!

ответ

0

//Let's say TheListofGameYouWantToInsert contains 
 
//index 0 •Assasin's Creed 2 or 2 
 
//index 1 •FallOut 4   or 4 
 
//index 2 •Batman Arkham Knight or 1 
 

 

 
function target_game(TheListofGameYouWantToInsert) {  
 
    b = t = 0 
 
    //b = 0 and t = 0 why? because b and t are empty and only the "0" is a defined value which will prioritize first 
 
    // What will happen would be t is also 0 because b = t where t is 0 
 
    //b = (t = 0); 
 
    //b = (0 = 0); 
 
    //b = 0; 
 
    for (index in TheListofGameYouWantToInsert) { 
 
    //for everytime the code runs on each list of "TheListofGameYouWantToInsert" 
 
    //index usually starts with 0; 
 
     c = b + a[i] 
 
     //c is equal to b which is 0 and add TheListofGameYouWantToInsert[index] 
 
     //c = 0 + TheListofGameYouWantToInsert[0] 
 
     //c = 0 + 2  the Assasin's Creed 2 
 
     //c =(0 + 2) 
 
     //c = 2; 
 
     b = t 
 
     //b is again became t which is 0 
 
     //b = 0 
 
     t = c > t ? c : t 
 
     //t is equal to whichever makes the condition true 
 
     //t = is c greater than t? if yes then give c or if not, then give t 
 
     //t = is 2 greater that 0? if yes return 2 or if not, return 0 
 
     //t = 2 > 0? 2 : 0 
 
     //t = 2; 
 
    } 
 
    return t // output 2 
 
} 
 

 
//Next Index which is 1 
 
     c = b + a[i] 
 
     //c = 0 + TheListofGameYouWantToInsert[1] 
 
     //c = 0 + 4  the FallOut 4 
 
     //c =(0 + 4) 
 
     //c = 4; 
 
     b = t 
 
     //b is again became t which is 0 
 
     //b = 0 
 
     t = c > t ? c : t 
 
     //t = 4 > 0? 4 : 0 
 
     //t = 4; 
 

 
//Next Index which is 2 
 
     c = b + a[i] 
 
     //c = 0 + TheListofGameYouWantToInsert[2] 
 
     //c = 0 + 1  the Batman Arkham Knight 1 
 
     //c =(0 + 1) 
 
     //c = 1; 
 
     b = t 
 
     //b is again became t which is 0 
 
     //b = 0 
 
     t = c > t ? c : t 
 
     //t = 1 > 0? 1 : 0 
 
     //t = 1; 
 
     
 
//Until the last list is processed

+0

Может быть, я выбрал неправильные слова, чтобы объяснить себе. Я понимаю, что делает каждая строка кода, и я могу отслеживать каждую переменную. Я не понимаю, почему. Почему этот код дает оптимальный результат? Как это устроено? Например, почему кодер написал 'c = b + a [i]'? Какова логика и мышление? – Motun

Смежные вопросы