JS代码没有按预期工作

时间:2016-12-04 04:15:25

标签: javascript jquery debugging drupal-7

尝试调试这个,但它超出了我的范围。我正在为学校编制时间表,使用jQuery来append()生成的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>'); } }); }); HTML到。{/ p>

  • 有测试,调查和课程。
  • 每10节课进行一次测试,每10次进行一次调查。
  • 周一,周三和周五,你得到两件事&#34;&#34; (两个课程,测试或调查,课程和测试,或调查和课程)
  • 在星期二和星期四,你只能上一节课,考试或调查
  • 它不是从第1课,第1课或第1课开始,而是在第72课,第14课和第8课。

这是我的代码:

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);
  });


});

Calendar Drupal module

更新:问题是pMath1()始终返回&#34; Inv 8&#34;和pMath2()仍未定义

更新2 :好的,让它使用此代码。特别感谢JS FiddleXufox提供的所有帮助:

import UIKit

class MenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

lazy var collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    layout.minimumLineSpacing = 0
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = .red
    cv.dataSource = self
    cv.delegate = self
    cv.isPagingEnabled = true
    return cv
}()


let cellId = "cellId"

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(collectionView)

    //use autolayout instead
    collectionView.anchorToTop(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor)

    collectionView.register(PageCell.self, forCellWithReuseIdentifier: cellId)
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 4
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath as IndexPath)

    return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: view.frame.height)
  }

}

extension UIView {

func anchorToTop(top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil) {

    anchorWithConstantsToTop(top: top, left: left, bottom: bottom, right: right, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0)
}

func anchorWithConstantsToTop(top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0) {

    translatesAutoresizingMaskIntoConstraints = false

    if let top = top {
        topAnchor.constraint(equalTo: top, constant: topConstant).isActive = true
    }

    if let bottom = bottom {
        bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant).isActive = true
    }

    if let left = left {
        leftAnchor.constraint(equalTo: left, constant: leftConstant).isActive = true
    }

    if let right = right {
        rightAnchor.constraint(equalTo: right, constant: -rightConstant).isActive = true
    }

}

}

1 个答案:

答案 0 :(得分:1)

好的,关于函数和return语句的基本内容是,如果你在函数的任何一点调用return,那么在return语句不执行之后,该函数的任何代码都会保留;

因此,对于pMath1函数,inv = 8的初始值;

如果您注意到pMath1()函数,

{{1}}

所以,如果你按照我的意见,你会明白该做什么,但是,我正在调整pMath1(),

{{1}}

好的,现在我们的inv变量不应该坚持8,因为我们知道如何使用return,你可以在pMath2()尝试同样的事情,因为它有同样的错误。玩得开心:)