Javascript 计算函数不起作用

标签 javascript function jsfiddle

这是一个JS Fiddle . 剧本不言自明。我只想指出这是行不通的。请看看并告诉我有什么不同。提前致谢。我遵循了我认为是 javascript 编程中的每条规则,但不知何故我一定忽略了一些东西。我还制作了一个实际工作版本 og 脚本 i PHP。有效的 PHP 是本文中的第二个脚本:PHP split string at last number, insert an extra string and merge the new string .

function calTime(x) {

if (x === '') {
    x = 54098;
} // Time in seconds
var f = 31536000, // seconds in a year
    d = 86400, // seconds in a day
    h = 3600, // seconds in an hour
    m = 60, // seconds in a minute
    xa,
    xb,
    xc,
    xe,
    xq,
    string,
    lb_y = 'year',
    lb_ys = 'years',
    lb_d = 'day',
    lb_ds = 'days',
    lb_h = 'hour',
    lb_hs = 'hours',
    lb_m = 'minute',
    lb_ms = 'minutes',
    lb_s = 'second',
    lb_ss = 'seconds',
    lb_and = 'and';

// a = years
var a = x / f;

// To prevent complications using scientific numbers less than 0 ex 7.2341232E-23
var a1 = a.indexOf("E-");
if (a1) {
    a = 0;
}

// Split a so we only get the numbers before '.'
var a2 = a.indexOf(".");
if (a2) {
    Math.floor(a);
}

// if $a is less or equal to 0 - it is 0
if (a <= 0) {
    a = 0;
}

// b = days
var b = (x - (f * a)) / d;

// To prevent complications using scientific numbers less than 0 ex 7.2341232E-23
var b1 = b.indexOf("E-");
if (b1) {
    b = 0;
}

// Split b so we only get the numbers before '.'
var b2 = b.indexOf(".");
if (b2) {
    Math.floor(b);
}

// if $b is less or equal to 0 - it is 0
if (b <= 0) {
    b = 0;
}

// c = hours
var c = (x - (f * a) - (d * b)) / h;

// To prevent complications using scientific numbers less than 0 ex 7.2341232E-23
var c1 = c.indexOf("E-");
if (c1) {
    c = 0;
}

// Split c so we only get the numbers before '.'
var c2 = c.indexOf(".");
if (c2) {
    Math.floor(c);
}

// if $c is less or equal to 0 - it is 0
if (c <= 0) {
    c = 0;
}

// e = minutes
var e = (x - (f * a) - (d * b) - (h * c)) / m;

// Split $e so we only get the numbers before '.'
var e2 = e.indexOf(".");
if (e2) {
    Math.floor(e);
}

// if $e is less or equal to 0 - it is 0
if (e <= 0) {
    e = 0;
}

// $q = seconds
var q = (x - (f * a) - (d * b) - (h * c) - (m * e));

// Rewrite numbers if below 9
if (a <= 9) {
    xa = '0' + a;
} else {
    xa = a;
}
if (b <= 9) {
    xb = '0' + b;
} else {
    xb = b;
}
if (c <= 9) {
    xc = '0' + c;
} else {
    xc = c;
}
if (e <= 9) {
    xe = '0' + e;
} else {
    xe = e;
}
if (q <= 9) {
    xq = '0' + q;
} else {
    xq = q;
}

// Rewrite labels
if (a <= 1) {
    lb_ys = lb_y;
}
if (b <= 1) {
    lb_ds = lb_d;
}
if (c <= 1) {
    lb_hs = lb_h;
}
if (e <= 1) {
    lb_ms = lb_m;
}
if (q <= 1) {
    lb_ss = lb_s;
}

// if == 0 - do not show
if (a === 0) {
    a = '';
} else {
    a = a + ' ' + lb_ys;
}
if (b === 0) {
    b = '';
} else {
    b = b + ' ' + lb_ds;
}
if (c === 0) {
    c = '';
} else {
    c = c + ' ' + lb_hs;
}
if (e === 0) {
    e = '';
} else {
    e = e + ' ' + lb_ms;
}
if (q === 0) {
    q = '';
} else {
    q = q + ' ' + lb_ss;
}


var time = [a, b, c, e, q];

time = time.filter(Number);

var count = time.count();
var last = time[time.length - 1];

if (count == 1) {
    string = last;
} else if (count === 0) {
    string = '<i>No Time described</i>';
} else {
    string = time.join(', ') + ' ' + lb_and + ' ' + last;
}

return string;
}

document.getElementById("demo").innerHTML = calTime(83200);

最佳答案

我会尝试在一个地方找出脚本的所有技术错误。

  1. 时间计算不正确

    一天有 86400 秒,一年 365 天有 31536000 秒。如果人们不想担心值(value)观,您通常会看到他们这样做:

    var minutes = 60;
    var hours = 60 * 60;
    var days = 24 * 60 * 60;
    var years = 365 * 24 * 60 * 60;
    
  2. 对不支持该方法的对象(在本例中为数字)使用 indexOf()

    其他人已经在评论和答案中指出了这一点,但基本上,如果您要对它们调用字符串方法,请将您的数字转换为字符串:

    num = num + "";
    numIndex = (num + "").indexOf("foo");
    
  3. 没有正确检查 indexOf() 的返回值

    indexOf() 返回字符串开始位置的索引(从 0 开始)。如果未找到该字符串,则返回 -1。在多个位置,您正在执行以下操作:

    var a2 = a.indexOf("E-");
    if (a2) {
      a = 0;
    }
    

    a2,在这种情况下,如果不符合科学记数法,则为 -1。唯一被视为 false 的整数值是 0。因此,-1 值始终被视为 true,您始终将年、日和小时设置为 0,无论它们是否采用科学记数法格式还是不是。

  4. 科学记数法不考虑大小写

    在我的浏览器中,非常小、接近于零的值如下所示:

    7.888609052210118e-31
    

    您的搜索将不匹配该值。考虑到您的逻辑,这甚至可能无关紧要。有什么理由不总是使用 Math.floor() 吗?你的 JS 浮点问题将成为一个问题,无论你如何分割它。

  5. 不使用 Math.floor() 的返回值

    在多个位置,您执行如下操作:

    Math.floor(a);
    

    然后您继续假设 a 已采用其下限值。您需要执行以下操作才能实现这一点:

    a = Math.floor(a);
    
  6. 将所有时间组件设置为字符串,然后按 Number 过滤它们

    您在时间数组中显式存储字符串格式(例如 23 小时 6 分钟),但随后您按 Number 过滤时间数组。我认为您正在尝试过滤掉空白字符串,这是您将时间值设置为 0 时的值。将函数传递给 filter() 以过滤掉那些空白条目,就像这样:

    time.filter(function(x) { return x === "" ? false : true; });
    
  7. count() 不是 Array

    的方法

    您可能正在寻找 length,您实际上在其下方正确使用了它。

  8. 将整个时间数组连接在一起,然后再次添加数组中的最后一项

    我会让你解决这个问题。你不想重复最后一项,你可能还想处理时间平均分配到你的一个类别中的情况

这些是您脚本的技术问题。另一个完整的答案可以写成更优雅的方式来完成你想要做的事情。

关于Javascript 计算函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14665273/

相关文章:

javascript - 如何减少重复行?

javascript - 将具有子数组的对象数组映射并减少到具有父 id 的子数组

python - 在python中获取执行时间函数

javascript - jsFiddle onload 和 no-wrap in body

jquery - 如何在 jsfiddle 中使用最新的 jQuery 进行 ajax 工作?

javascript - 如何使用 mobile safari 停止在 touchend 上滚动?

javascript - 当调用 destroy() 时,AngularJS 在控制台上显示错误

javascript - 获取对象内部的函数名称

javascript - jQuery 1.9 .live() 不是函数

javascript - 为什么 jsfiddle 会抛出函数未定义的错误?