예상대로 작동하지 않는 JS 코드

크리스 해피

이것을 디버그하려고 시도하지만 그것은 나를 넘어서는 것입니다. 저는 Calendar Drupal 모듈에append() 의해 생성 된 td에 대해 jQuery를 HTML로 사용하여 학교 일정을 프로그래밍하고 있습니다.

  • 테스트, 조사 및 수업이 있습니다.
  • 수업 5 개마다 테스트하고 10 개마다 조사합니다.
  • 월요일, 수요일, 금요일에는 두 개의 "물건"(2 개의 레슨, 테스트 또는 조사, 레슨 및 테스트 또는 조사 및 레슨)을받습니다.
  • 화요일과 목요일에는 하나의 레슨, 테스트 또는 조사 만받습니다.
  • 레슨 1, 테스트 1 또는 inv 1에서 시작하지 않고 레슨 72, 테스트 14 및 조사 8에서 시작합니다.

내 코드는 다음과 같습니다.

jQuery(document).ready(function($) {

  /* Schedule */

  last = "lesson";
  lesson = 71;
  test = 13;
  inv = 7;

  function m10() {
    if (lesson % 10 == 0) {
      return true;
    } else {
      return false;
    }
  }

  function m5() {
    if (lesson % 5 == 0) {
      return true;
    } else {
      return false;
    }
  }

  function pMath1() {
    if (m10() == false && last !== "test") {

      lesson++;
      last = "lesson";
      return "Lesson " + lesson;

    } else if (m5() == true && last !== "test") {

      test++;
      last = "test";
      return "Test " + test;

    } else if (m10() == true && last == "test") {

      inv++;
      last = "inv";
      return "Inv " + inv;

    } else {
      console.log("pMath1 doesn't work");
    }
  }


  function pMath2() {
    if (m10() == false) {

      lesson++;
      last = "lesson";
      return "Lesson " + lesson;

    } else if (m5() == true && last !== "test") {

      test++;
      last = "test";
      return "Test " + test;

    } else if (m10() == true && last == "test") {

      inv++;
      last = "inv";
      return "Inv " + inv;

    } else {
      console.log("pMath2 doesn't work");
    }
  }

  $(".view-id-school .single-day td:gt(6)").each(function() {

    var datePrep = $(this).attr("data-date");
    var dateSplit = datePrep.split("-");
    var dateStr = new Date(dateSplit[0], dateSplit[1], dateSplit[2]);
    var dateStr2 = new Date(2016, 12, 03);

    var classN = $(this).attr("headers");

    if (classN == "Monday" || classN == "Wednesday" || classN == "Friday") {

      $(this).removeClass("no-entry");

      var mathC = "M: " + pMath1() + ", " + pMath2();

    } else if (classN == "Tuesday" || classN == "Thursday") {

      $(this).removeClass("no-entry");

      var mathC = "M: " + pMath1();

    } else {
      mathC = "";
    };

    content = mathC;

    if (dateStr > dateStr2) {
      $(this).find(".inner").empty().append('<div class="calendar monthview"><div class="views-field views-field-title"><span class="field-content">' + content + '</span></div><div class="cutoff" /></div></div></div>');
    }
  });


});

JS 바이올린

업데이트 : 문제는 pMath1 ()이 항상 "Inv 8"을 반환하고 pMath2 ()가 정의되지 않은 상태로 유지된다는 것입니다.

업데이트 2 : 좋아,이 코드로 작동합니다. 모든 도움을 주신 XufoxRimon Habib 에게 특별히 감사드립니다 .

jQuery(document).ready(function ($) {

  /* Schedule */

  dateStr2 = new Date(2016, 12, 03);

  last = "lesson";
  lesson = 71; //Lesson you want to start on minus 1
  test = 13; //Test you want to start on minus 1
  inv = 7; //Investigation you want to start on minus 1

  function m10 () {  if ( lesson % 10 == 0) {  return true;  } else {  return false; }  }

  function m10M1 () {  if ( ( (lesson % 10) - 1) == 0 && last == "inv" ) {  return true;  } else {  return false; }  }

  function m5 () {  if ( lesson % 5 == 0) {  return true;  } else {  return false; }  }

  function pMath1 () {  
    if ( m10M1() ) { //Check if Lesson is x1 (e.g. 81) and minus 1 from it
      last = "lesson";
      return "Lesson " + lesson;

    } else if ( m10() == false && last !== "test") { //Add Lesson

      lesson++;
      last = "lesson";
      return "Lesson " + lesson;

    } else if ( m5() == true && last !== "test") { //Add Test

      test++;
      last = "test";
      return "Test " + test;

    } else if ( m10() == true && last == "test") { //Add Inv

      inv++;
      lesson++;
      last = "inv";
      return "Inv " + inv;

    } else {
      console.log("pMath1 doesn't work");
    }
  }


  function pMath2 () {  
    if ( m10M1() ) { //Check if Lesson is x1 (e.g. 81) and minus 1 from it

      last = "lesson";
      return "Lesson " + lesson;

    }else if ( m10() == false ) { //Add Lesson

      lesson++;
      last = "lesson";
      return "Lesson " + lesson;

    } else if ( m5() == true && last !== "test") { //Add Test

      test++;
      last = "test";
      return "Test " + test;

    } else if ( m10() == true && last == "test") { //Add Inv

      inv++;
      lesson++;
      last = "inv";
      return "Inv " + inv;

    } else {
      console.log("pMath2 doesn't work");
    }
  }

  $(".view-id-school .single-day td:gt(6)").each(function () {

    var datePrep = $(this).attr("data-date");
    var dateSplit = datePrep.split("-");
    var dateStr = new Date(dateSplit[0], dateSplit[1], dateSplit[2]);

    var classN = $(this).attr("headers");

    if ( classN == "Monday" || classN == "Wednesday" || classN == "Friday") {

      $(this).removeClass("no-entry");

      var mathC = "M: " + pMath1 () + ", " + pMath2 ();

    } else if (classN == "Tuesday" || classN == "Thursday") {

      $(this).removeClass("no-entry");

      var mathC = "M: " + pMath1 ();

    } else {
      mathC = "";
    };

    content = mathC;  

    if ( dateStr > dateStr2 ) {
      $(this).find(".inner").empty().append('<div class="calendar monthview"><div class="views-field views-field-title"><span class="field-content">' + content + '</span></div><div class="cutoff" /></div></div></div>');
    }

    console.log("Last: " + last + " <br /> Lesson: " + lesson + " <br /> inv: " + inv + " <br /> test: " + test + " <br /> " + "Content: " + content);
  });


});
리몬 하비브

좋습니다. 함수와 return 문에 대한 기본은 함수의 어느 지점에서든 return을 호출하면 return 문이 실행되지 않은 후에 해당 함수의 코드가 남아 있다는 것입니다.

따라서 pMath1 함수의 경우 inv = 8의 초기 값;

pMath1 () 함수를 발견하면

function pMath1 () {  

  if ( m10() == false && m5() == false && last !== test) {

    return "Lesson " + lesson; // code execution stops here as return called
    lesson++; // lesson value is not gonna increment
    last = "lesson"; // neither last value will update

  } else if ( m10() == false && m5() == true) {

    return "Test " + test; // again, code execution stops here
    test++; // test value will not increment
    last = "test"; // neither this will be executed

  } else if ( m10() == true ) {

    return "Inv " + inv; // and here is our inv variable, it was 8, we are returning 8, not giving chance inv to increase as stated line below 
    inv++;
    last = "inv";

  } else {
    console.log("pMath1 doesn't work");
  }
}

따라서 내 의견을 따르면 무엇을 해야할지 알 수 있지만 pMath1 ()을 조정하고 있습니다.

function pMath1 () {  

  if ( m10() == false && m5() == false && last !== test) {

    lesson++; // now lesson value will increase
    last = "lesson"; // and this one will also execute

    return "Lesson " + lesson; // and when we are done, we can return and halt execution here; by this time our variables are processed.

  } else if ( m10() == false && m5() == true) {


    test++; 
    last = "test"; 

    return "Test " + test; // same approach again


  } else if ( m10() == true ) {

    inv++;
    last = "inv";

    return "Inv " + inv; // and here is our holy grail, we gave chance inv to increase first, now we are returning its incremented value. it will be no longer stick with 8.

  } else {
    console.log("pMath1 doesn't work");
  }
}

자, 이제 우리의 inv 변수가 8로 고착되어서는 안됩니다. return을 사용하는 방법을 알고 있으므로 동일한 실수가 있으므로 pMath2 ()에서 동일한 작업을 시도 할 수 있습니다. 그것으로 재미를 :)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

예상대로 작동하지 않는 간단한 Java 코드

분류에서Dev

Handlebars.js 예상대로 작동하지 않는 경우

분류에서Dev

노드 항목을 연결하는 D3.js가 예상대로 작동하지 않습니다.

분류에서Dev

예상대로 작동하지 않는`XML` 파일 업로드

분류에서Dev

Vue JS : 내 코드에서 제대로 작동하지 않는 함수 제거

분류에서Dev

timeago.js가 예상대로 작동하지 않음

분류에서Dev

때때로 내 코드가 예상대로 작동하지 않는 이유는 무엇입니까?

분류에서Dev

노드 js 이벤트 리스너가 예상대로 작동하지 않습니다.

분류에서Dev

내 Node JS 앱에서 예상대로 작동하지 않는 약속

분류에서Dev

이 C # 코드가 예상대로 작동하지 않는 이유는 무엇입니까?

분류에서Dev

다음 코드가 예상대로 작동하지 않는 이유는 무엇입니까?

분류에서Dev

OpenGL,이 코드가 예상대로 작동하지 않는 이유는 무엇입니까?

분류에서Dev

웹팩 코드 분할이 예상대로 작동하지 않는 이유는 무엇입니까?

분류에서Dev

MSXML 제거 노드가 예상대로 작동하지 않음

분류에서Dev

jQuery Mobile 그리드가 예상대로 작동하지 않음

분류에서Dev

문제가있는 AS3 코드 / 내 보낸 swf가 예상대로 작동하지 않음

분류에서Dev

예상대로 작동하지 않는 두 필드에 의한 장고 순서

분류에서Dev

blast.js 예제는 코드 펜에서 완벽하게 작동하지만 로컬에서는 작동하지 않습니다.

분류에서Dev

예상대로 실행되지 않는 비동기 대기 코드

분류에서Dev

롤링 평균 쌍별 상관-코드가 예상대로 작동하지 않습니다.

분류에서Dev

ExecutorService 스레드가 RxJava 코드에서 예상대로 작동하지 않습니다.

분류에서Dev

d3js Force 레이아웃이 예상대로 작동하지 않음

분류에서Dev

Meteor JS Jade 템플릿이 예상대로 작동하지 않습니다.

분류에서Dev

Node.js requre가 예상대로 작동하지 않습니다.

분류에서Dev

JS 문자열이 예상대로 작동하지 않습니다.

분류에서Dev

Knockout.js-$ parent가 예상대로 작동하지 않습니다.

분류에서Dev

예상대로 작동하지 않는 'with'바인딩을 사용하는 녹아웃 js 구성 요소

분류에서Dev

부트 스트랩 4.5 코드가 예상대로 작동하지 않습니다.

분류에서Dev

매개 변수를 사용한 코드 생성이 예상대로 작동하지 않습니다.

Related 관련 기사

  1. 1

    예상대로 작동하지 않는 간단한 Java 코드

  2. 2

    Handlebars.js 예상대로 작동하지 않는 경우

  3. 3

    노드 항목을 연결하는 D3.js가 예상대로 작동하지 않습니다.

  4. 4

    예상대로 작동하지 않는`XML` 파일 업로드

  5. 5

    Vue JS : 내 코드에서 제대로 작동하지 않는 함수 제거

  6. 6

    timeago.js가 예상대로 작동하지 않음

  7. 7

    때때로 내 코드가 예상대로 작동하지 않는 이유는 무엇입니까?

  8. 8

    노드 js 이벤트 리스너가 예상대로 작동하지 않습니다.

  9. 9

    내 Node JS 앱에서 예상대로 작동하지 않는 약속

  10. 10

    이 C # 코드가 예상대로 작동하지 않는 이유는 무엇입니까?

  11. 11

    다음 코드가 예상대로 작동하지 않는 이유는 무엇입니까?

  12. 12

    OpenGL,이 코드가 예상대로 작동하지 않는 이유는 무엇입니까?

  13. 13

    웹팩 코드 분할이 예상대로 작동하지 않는 이유는 무엇입니까?

  14. 14

    MSXML 제거 노드가 예상대로 작동하지 않음

  15. 15

    jQuery Mobile 그리드가 예상대로 작동하지 않음

  16. 16

    문제가있는 AS3 코드 / 내 보낸 swf가 예상대로 작동하지 않음

  17. 17

    예상대로 작동하지 않는 두 필드에 의한 장고 순서

  18. 18

    blast.js 예제는 코드 펜에서 완벽하게 작동하지만 로컬에서는 작동하지 않습니다.

  19. 19

    예상대로 실행되지 않는 비동기 대기 코드

  20. 20

    롤링 평균 쌍별 상관-코드가 예상대로 작동하지 않습니다.

  21. 21

    ExecutorService 스레드가 RxJava 코드에서 예상대로 작동하지 않습니다.

  22. 22

    d3js Force 레이아웃이 예상대로 작동하지 않음

  23. 23

    Meteor JS Jade 템플릿이 예상대로 작동하지 않습니다.

  24. 24

    Node.js requre가 예상대로 작동하지 않습니다.

  25. 25

    JS 문자열이 예상대로 작동하지 않습니다.

  26. 26

    Knockout.js-$ parent가 예상대로 작동하지 않습니다.

  27. 27

    예상대로 작동하지 않는 'with'바인딩을 사용하는 녹아웃 js 구성 요소

  28. 28

    부트 스트랩 4.5 코드가 예상대로 작동하지 않습니다.

  29. 29

    매개 변수를 사용한 코드 생성이 예상대로 작동하지 않습니다.

뜨겁다태그

보관