javascript DashInsert Coderbyte挑战-为什么arr[i]%2===1有效?

u4vypkhs  于 2022-12-02  发布在  Java
关注(0)|答案(5)|浏览(141)

The Coderbyte problem is: Using the JavaScript language, have the function DashInsert(str) insert dashes ('-') between each two odd numbers in str. For example: if str is 454793 the output should be 4547-9-3. Don't count zero as an odd number.
So when the input is 99946, the output should be 9-9-946.
I had this solution, which wouldn't quite work:

function DashInsert(num) {
var arr = num.toString().split('');
var i = 0;
while(i < arr.length-1){
if( arr[i]%2 !==0 && arr[i+1]%2 !==0){ 
arr.splice(i+1,0,'-'); 
}
i++
}
return arr.join(''); 
}

Then I found this similar answer:

function DashInsert(num) {
var arr = num.toString().split('');

var i = 0

while(i < arr.length-1){
if( arr[i]%2===1 && arr[i+1]%2===1){ 
arr.splice(i+1,0,'-'); 
}
i++
}

return arr.join(''); }
str = 99946;
alert(DashInsert(str));

Can anyone explain why it should be arr[i]%2===1?

m1m5dgzv

m1m5dgzv1#

两个都是正确。2例如,假设上午9:9%2!= 0和9%2 ==1。想想看,所有奇数都可以拆分为2n+1。对2取模总是返回1,而不是0。

idfiyjo8

idfiyjo82#

For anyone else who stumbles upon this in a frustrated googling haze...
After the first hyphen is added, it changes the length of the array, so it gets evaluated in the loop to see if the hyphen element !== 0.
Since a '-' !== 0, another hyphen is added.
This is also why you keep blowing your stack since the hyphen keeps changing the length of your array (side note, always cache your length in a variable outside of your for loop and use that in your loop instead).
To fix it, you could add a bunch more &&'s to your if statement i.e.
if(theArray[x] % 2 !== 0 && theArray[x+1]% 2 !== 0 && theArray[x] !== '-' && theArray[x+1] !== '-')
or you could just be more specific and only look for modulo results that evaluate to 1.

9w11ddsr

9w11ddsr3#

我试过这个,效果很好。
嘿...
私有静态字符串createDashedString(字符串str){

StringBuilder builder = new StringBuilder();
    char[] chararray = str.toCharArray();

    for (int i=0;i<chararray.length-1;i++) {

        int firstInt = Character.getNumericValue(chararray[i]);
        int nextInt = Character.getNumericValue(chararray[i+1]);
        if ((firstInt%2 !=0) && (nextInt%2 !=0)) {
            builder.append(firstInt);
            builder.append("-");
        }else {
            builder.append(firstInt);
        }
    }
    builder.append(chararray[chararray.length-1]);
    return builder.toString();

}

public static void main(String args[]) {

    String str = "999999";
                  //01234
    System.out.println(createDashedString(str));

}
9lowa7mx

9lowa7mx4#

function DashInsert(str) { 

    let bil = 0;
     while (bil < str.length-1) {
         if (Number(str[bil]) % 2 === 1 && Number(str[bil+1]) % 2 === 1) {
           str = str.slice(0,bil+1) + "-" + str.slice(bil+1);
           bil = bil + 2;
         }
         else {  
             bil++;
         }
   }
   return str;  
          
 }

 console.log(DashInsert("454793"));
ubof19bj

ubof19bj5#

let x = '99946' 
    let z=[]
    for(var i = 0;i<x.length;i++){
      if(x[i]%2 == 0 && x[i+1]%2 == 1 ){
        z+=x[ i ]
      }else if (x[i]%2 == 1 && x[i+1]%2 == 0){
          z+=x[i]
      }else if (x[i]%2 == 1 && x[i+1]%2 == 1){
           z+=x[i]+"-"
      }else{
          z+=x[i]
      }
     }
    console.log(z)

相关问题