简体   繁体   English

创建一个 function,具有数组参数的 validateCred()。 Scope 的变量

[英]Create a function, validateCred() that has a parameter of an array. Scope of variables

I'm learning Javascript on Codecademy and have gotten stumped by a problem.我正在 Codecademy 上学习 Javascript,但遇到了一个问题。 I believe my issue is with the scope of my iterator tracker but not too sure.我相信我的问题出在我的迭代器跟踪器的 scope 上,但不太确定。

Here are the directions given to me: "Create a function, validateCred() that has a parameter of an array. The purpose of validateCred() is to return true when an array contains digits of a valid credit card number and false when it is invalid. This function should NOT mutate the values of the original array.以下是给我的指示:“创建一个 function,具有数组参数的 validateCred()。validateCred() 的目的是当数组包含有效信用卡号的数字时返回 true,当它包含有效信用卡号时返回 false无效。此 function 不应改变原始数组的值。

To find out if a credit card number is valid or not, use the Luhn algorithm.要确定信用卡号是否有效,请使用 Luhn 算法。 Generally speaking, an algorithm is a series of steps that solve a problem — the Luhn algorithm is a series of mathematical calculations used to validate certain identification numbers, eg credit card numbers.一般来说,算法是解决问题的一系列步骤——Luhn 算法是一系列数学计算,用于验证某些识别号码,例如信用卡号码。 The calculations in the Luhn algorithm can be broken down as the following steps: Luhn算法中的计算可以分解为以下步骤:

Starting from the farthest digit to the right, AKA the check digit, iterate to the left.从最右边的数字开始,也就是校验位,向左迭代。 As you iterate to the left, every other digit is doubled (the check digit is not doubled).当您向左迭代时,每隔一个数字都会加倍(校验位不会加倍)。 If the number is greater than 9 after doubling, subtract 9 from its value.如果翻倍后数字大于 9,则从其值中减去 9。 Sum up all the digits in the credit card number.将信用卡号中的所有数字相加。

If the sum modulo 10 is 0 (if the sum divided by 10 has a remainder of 0) then the number is valid, otherwise, it's invalid.如果和模 10 为 0(如果和除以 10 的余数为 0)则该数有效,否则无效。 Here's a visual that outlines the steps .是概述步骤的视觉效果 Check your function using both the provided valid and invalid numbers."使用提供的有效和无效号码检查您的 function。”

Here's an array that should be passed into this function and return true:这是一个应该传递给这个 function 并返回 true 的数组:

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];

Here's an array that should return false:这是一个应该返回 false 的数组:

const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];

Lastly, here's my code for trying to solve this:最后,这是我试图解决这个问题的代码:

function validateCred(arr) {
  //Keeps track for the sum modulo later on
  let totalSum = 0;
  //Iterates to the left
  for (let i = arr.length; i > 0; i--) {
    //Keeps track of place to later double every other digit
    let iterater = 1;
    //Checks for every other digit and doubles it
    if (iterater % 2 === 0) {
      //Doubles it
      let doubledAmnt = arr[i] * 2;
      //Checks if doubled amount is greater than 9
      if (doubledAmnt > 9) {
        totalSum += (doubledAmnt - 9);
      //Adds the doubled amount if not over 9
      } else {
        totalSum += doubledAmnt;
      }
    //Adds the digit if it does not need to be doubled  
    } else {
      totalSum += arr[i];
    };
    //Adds to place tracker (I think my problem is here?)
    iterater++
  };

//Checks if the total sum is divisible by 10 to return true or false
  if (totalSum % 10 === 0) {
    return true;
  } else {
    return false;
  }
}
function validateCred(arr) {
  //Keeps track for the sum modulo later on
  let totalSum = 0;

  let iterater = 1; //iterater should be outside loop so that it wont reset in for loop

  //Iterates to the left
  for (let i = arr.length-1; i >= 0; i--) { //index starts in 0, so you should minus 1 to read the 16 digits

    //Checks for every other digit and doubles it
    if (iterater % 2 === 0) {
     
      //Doubles it
      let doubledAmnt = arr[i] * 2;
      //Checks if doubled amount is greater than 9
      if (doubledAmnt > 9) {
        totalSum += (doubledAmnt - 9);
      //Adds the doubled amount if not over 9
      } else {
        totalSum += doubledAmnt;
      }
    //Adds the digit if it does not need to be doubled  
    } else {
      totalSum += arr[i];
   
    };
    //Adds to place tracker (I think my problem is here?)
    iterater++
  };
  
  

//Checks if the total sum is divisible by 10 to return true or false
  if (totalSum % 10 === 0) {
    return true;
  } else {
    return false;
  }
}

Please note that indexes always start in 0 so if you start with 1, you have to minus 1 so that it will read the whole array.请注意,索引始终从 0 开始,因此如果您从 1 开始,则必须减去 1 才能读取整个数组。 Next is, you put the iterater inside the loop, so it resets whenever the loop iterates, so you have to put it outside the loop.接下来是,您将iterater放在循环内,因此每当循环迭代时它都会重置,因此您必须将其放在循环外。 Hope this helps.希望这可以帮助。

Please see the code I modified above with comments.请参阅我上面修改的代码并附上评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM