最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Find the nth Digit of a Number JavaScript - Stack Overflow

programmeradmin3浏览0评论

I really thought I'd be able to find the solution to my issue but I think the more I look at it the less I can see. Here is my homework:

Complete the function that takes two numbers as input, num and nth and return the nth digit of num (counting from right to left).
Note:
- If num is negative, ignore its sign and treat it as a positive value
- If nth is not positive, return -1
- Keep in mind that 42 = 00042. This means that findDigit(42, 5) would return 0

Code:

var findDigit = function(num, nth) {
  let change = num.toString();
  let find = (change.charAt(change.length - nth) * 1);
  if (nth <= 0){
    return nth;
  } 
  return find; 
}

The inputs are:

describe("Test", function() {
  it("Examples", function(){
    Test.assertEquals(findDigit(5673, 4), 5);
    Test.assertEquals(findDigit(129, 2), 2);
    Test.assertEquals(findDigit(-2825, 3), 8);
    Test.assertEquals(findDigit(-456, 4), 0);
    Test.assertEquals(findDigit(0, 20), 0);
    Test.assertEquals(findDigit(65, 0), -1);
    Test.assertEquals(findDigit(24, -8), -1);
  });
});

My results are:

  • Test Passed: Value == 5
  • Test Passed: Value == 2
  • Test Passed: Value == 8
  • Expected: 0, instead got: NaN
  • Test Passed: Value == 0
  • Expected: -1, instead got: 0
  • Expected: -1, instead got: -8

I really thought I'd be able to find the solution to my issue but I think the more I look at it the less I can see. Here is my homework:

Complete the function that takes two numbers as input, num and nth and return the nth digit of num (counting from right to left).
Note:
- If num is negative, ignore its sign and treat it as a positive value
- If nth is not positive, return -1
- Keep in mind that 42 = 00042. This means that findDigit(42, 5) would return 0

Code:

var findDigit = function(num, nth) {
  let change = num.toString();
  let find = (change.charAt(change.length - nth) * 1);
  if (nth <= 0){
    return nth;
  } 
  return find; 
}

The inputs are:

describe("Test", function() {
  it("Examples", function(){
    Test.assertEquals(findDigit(5673, 4), 5);
    Test.assertEquals(findDigit(129, 2), 2);
    Test.assertEquals(findDigit(-2825, 3), 8);
    Test.assertEquals(findDigit(-456, 4), 0);
    Test.assertEquals(findDigit(0, 20), 0);
    Test.assertEquals(findDigit(65, 0), -1);
    Test.assertEquals(findDigit(24, -8), -1);
  });
});

My results are:

  • Test Passed: Value == 5
  • Test Passed: Value == 2
  • Test Passed: Value == 8
  • Expected: 0, instead got: NaN
  • Test Passed: Value == 0
  • Expected: -1, instead got: 0
  • Expected: -1, instead got: -8
Share Improve this question edited Jan 21, 2020 at 23:28 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jan 21, 2020 at 22:46 CocoFladeCocoFlade 3051 silver badge8 bronze badges 2
  • You should also include the inputs in your tests so we have a better idea of which cases your code fails at. – jacob13smith Commented Jan 21, 2020 at 22:49
  • Sorry Jacob my apologies, I've just updated it thank you. – CocoFlade Commented Jan 21, 2020 at 22:51
Add a ment  | 

4 Answers 4

Reset to default 5

In the specifications for the function, you should return -1 when nth is negative, not nth.

For the test that fails with NaN, it is because you are grabbing the - and treating it as a digit. You should convert the input to a positive value using Math.abs(), which will get rid of the sign.

I believe this should make your tests pass, except for the case where nth > num.length, which you haven't tested. You should include a test where num = 42 and nth = 5.

var findDigit = function(num, nth){
  if (nth <= 0){
    return -1;
  }
  let change = Math.abs(num).toString();
  let find = (change.charAt(change.length - nth) * 1);
  // I suspect this will fail when nth is larger than the length
  return find; 
}

Edit: To make sure your code passes the test I described above, use this to easily return 0 in this case:

var findDigit = function(num, nth) {
  if (nth <= 0) {
    return -1;
  }
  let change = Math.abs(num).toString();
  if (change.length < nth)
    return 0;
  let find = (change.charAt(change.length - nth) * 1);
  return find; 
}

So the first step is to make sure you are removing the negative sign when you convert the number to a string, as the instructions say we can treat them as positive and the negative sign will be taking up a character position when we traverse each character.

The next step is when nth <= 0 we should return a -1 as the instructions tell us explicitly that

If nth is not positive, return -1.

The resulting function would look like:

var findDigit = function(num, nth) {
  if (nth <= 0) {
    return -1;
  } 
  let change = num.toString().replace('-', '');
  let find = (change.charAt(change.length - nth) * 1);
  return find; 
}

I have translated your requirements into code and added your test cases:

// Write a function that takes two numbers as input, num and nth
function findDigit(num, nth) {
  // If nth is not positive, return -1
  if (nth <= 0) {
    return -1;
  } else {
    // If num is negative, ignore its sign and treat it as a positive value
    let str = Math.abs(num).toString();
    // Keep in mind that 42 = 00042. This means that findDigit(42, 5) would return 0
    if (nth > str.length) {
      return 0;
    } else {
      // Return the nth digit of num (counting from right to left)
      return str.charAt(str.length - nth);
    }
  }
}

// Test cases
console.log(findDigit(5673, 4) == 5);
console.log(findDigit(129, 2) == 2);
console.log(findDigit(-2825, 3) == 8);
console.log(findDigit(-456, 4) == 0);
console.log(findDigit(0, 20) == 0);
console.log(findDigit(65, 0) == -1);
console.log(findDigit(24, -8) == -1);

I guess your function would be better off with test results with something like this:

var findDigit = function(num, nth) {
  let change = num.toString();
  let val = change.charAt(change.length - nth);
  if (!(val >= 0)) {
    val = 0;
  }
  let find = val * 1;
  if (nth <= 0) {
    return -1;
  } 
  return find; 
}
发布评论

评论列表(0)

  1. 暂无评论