javascript 是Lucky任务- CodeSignal;即使if else语句默认为false,也保持运行true

shstlldc  于 2022-12-17  发布在  Java
关注(0)|答案(6)|浏览(129)

我在CodeSignal上执行isLucky任务时,代码总是返回true。我该怎么做才能解决这个问题?
任务:如果前半位数字之和等于后半位数字之和,则认为该票号是幸运的。
给定一张彩票号码n,判断它是否幸运。
我试着用

if(first == second) {
return true;
} else { 
return false;
}

function isLucky(n) {
    var half = n.length/2;
    var first = 0;
    var second = 0;
    for(var i = 0; i < half; i++) {
        first += n[i];
    }
    for(var j = half+1; j < n.length; j++) {
        second += n[j];
    }
    if(first != second) {
        return false;   
    } else {
        return true;
    }
}

其中一项实践测试为n = 239017;我希望它返回false,但它返回true。

uelo1irk

uelo1irk1#

JavaScript解决方案

function isLucky(n) {    
    let count = 0;
    n = String(n).split('').map(t => {return parseInt(t)});
    n.forEach( (el, i) => { (i < n.length / 2) ? count += el : count -= el });
    return count == 0;
}
s5a0g9ez

s5a0g9ez2#

n是一个数字,数字没有length属性或数字索引。记住这一点,我们可以看到代码的重要部分永远不会运行:

function isLucky(n) {
    var half = n.length/2; // n.length is undefined; undefined / 2 is NaN

    var first = 0;
    var second = 0;

    // (0 < NaN) is false; this loop never runs
    for(var i = 0; i < half; i++) {
        first += n[i];
    }

    // NaN + 1 is NaN, (NaN < undefined) is false; this loop never runs
    for(var j = half+1; j < n.length; j++) {
        second += n[j];
    }

    // Nothing has happened since first and second were both set to 0; return true
    if(first != second) {
        return false;   
    } else {
        return true;
    }
}

Converting n to a string解决了这些问题,但是现在isLucky总是返回false。阅读函数,我们期望first包含n前半部分的数字之和,second包含后半部分的数字之和。在函数返回之前记录firstsecond的值,以检查该假设:

function isLucky(n) {
  n = n.toString();
  var half = n.length / 2;
  var first = 0;
  var second = 0;
  for (var i = 0; i < half; i++) {
    first += n[i];
  }
  for (var j = half + 1; j < n.length; j++) {
    second += n[j];
  }
  
  console.log(first, second);
  if (first != second) {
    return false;
  } else {
    return true;
  }
}

console.log(isLucky(239017));

这些不是和,也不是偶数(stack snippet控制台没有显示,但Chrome和Firefox的控制台记录的数字是彩色的,字符串是黑色的,这些都是字符串)。
这是因为n[i]是一个字符串,而+是字符串的连接,用Number(n[i])+n[i]将它们转换成数字,isLucky(239017)就可以工作,但其他数字仍然有问题:

function isLucky(n) {
  n = n.toString();
  var half = n.length / 2;
  var first = 0;
  var second = 0;
  for (var i = 0; i < half; i++) {
    first += +n[i];
  }
  for (var j = half + 1; j < n.length; j++) {
    second += +n[j];
  }
  
  console.log(first, second);
  if (first != second) {
    return false;
  } else {
    return true;
  }
}

console.log('239017 (should be false):', isLucky(239017));
console.log('123321 (should be true):', isLucky(123321));
console.log('123933 (should be false):', isLucky(123933));

看起来后半部分的第一位数没有包含在总和中。您可以在早期版本中看到这种情况:如果将second初始化为0,我们将期望与'239017'的后半部分连接的字符串为'0017',但我们得到的是'017'
half而不是half + 1开始第二个循环(并删除诊断日志),isLucky工作正常:

function isLucky(n) {
  n = n.toString();
  var half = n.length / 2;
  var first = 0;
  var second = 0;
  for (var i = 0; i < half; i++) {
    first += +n[i];
  }
  for (var j = half; j < n.length; j++) {
    second += +n[j];
  }
  
  if (first != second) {
    return false;
  } else {
    return true;
  }
}

console.log('239017 (should be false):', isLucky(239017));
console.log('123321 (should be true):', isLucky(123321));
console.log('123933 (should be false):', isLucky(123933));
irtuqstp

irtuqstp3#

JS中的溶液

function isLucky(n) {
    const b = n.toString().split('')
    const y = b.length / 2;
    let c1 = 0
    let c2 = 0
    b.map((e, i) => {
        if (i+1 <= y) c1 += parseInt(e);
        if (i+1 > y) c2 += parseInt(e);
    })
    return c1 === c2
}
7ajki6be

7ajki6be4#

function solution(n) {
// array from string n
    const nArr = n.toString().split('');
// split the nArr into two halfs
    const firstHalf = nArr.splice(0, nArr.length / 2);
    const secondHalf = nArr.splice(nArr.length / 2 - 1, nArr.length);
// Using reduce to sum both halfs from nArray, then compare them
    return firstHalf.sort( (a, b) => b - a).reduce( (prev, curr) => Number(prev) +  Number(curr) ) == 
            secondHalf.sort( (a, b) => b - a).reduce( (prev, curr) => Number(prev) +  Number(curr) );
}
enyaitl3

enyaitl35#

(1).仅当n为String或Array而非Integer时,length属性才有效
var half = n.length/2;
需要先将整型转换为字符串。
n = n.toString();
(2).当你需要字符串中的任何一个特定的数字字符时,你需要将字符类型转换为整数。
sum½ += parseInt(n[index])
(3).第一个和在一半处停止,不检查精确索引= half处的字符。
因此第二求和应当以下标= half而不是half + 1开始。
for(var j = half; j < n.length; j++) {

  • (4).可以优化最后一个条件。

当布尔结果取决于条件时,您可以执行 *
return (first == second);

function solution(n) {
    n = n.toString();
    var half = n.length/2, first = 0, second = 0;
    for(var i = 0; i < half; i++) {
        first += parseInt(n[i]);
    }
    for(var j = half; j < n.length; j++) {
        second += parseInt(n[j]);
    }
    return first == second;
}
console.log(solution(11));
console.log(solution(1230));
console.log(solution(134008));
console.log(solution(999999));
console.log(solution(123321));
console.log(solution(239017));
console.log(solution(261534));
console.log(solution(100000));
qacovj5a

qacovj5a6#

function solution(n) {
    var count = 0;
    n = String(n).split('').map(t => {return parseInt(t)});
    n.forEach( (el, i) => { (i < n.length / 2) ? count += el : count -= el });
    return count == 0;
}

相关问题