dimanche 5 août 2018

Coderbyte Challenge: Questions Marks - RegExp pattern '/d(\?\?\?)d/gi' incorect

I was solving the Coderbyte Challenge - Questions Marks When I run my code in the browser it all works fine, however, once I run it on the coderbyte website it throws an error.

The Challenge is:

Have the function QuestionsMarks(str) take the str string parameter, which will contain single digit numbers, letters, and question marks, and check if there are exactly 3 question marks between every pair of two numbers that add up to 10. If so, then your program should return the string true, otherwise it should return the string false. If there aren't any two numbers that add up to 10 in the string, then your program should return false as well.

For example: if str is "arrb6???4xxbl5???eee5" then your program should return true because there are exactly 3 question marks between 6 and 4, and 3 question marks between 5 and 5 at the end of the string.

Use the Parameter Testing feature in the box below to test your code with different arguments.

Test Cases Are:

"arrb6???4xxbl5???eee5" true

"aa6?9" false

"acc?7??sss?3rr1??????5" true

My solution to this was to use RegExp to solve the challenge. the code below works well when I run it in the Browser, however, Coderbyte console throws an error every time:

/tmp/009904362/main.js:11 clean = clean.match(/d(???)d/gi); ^SyntaxError: Invalid regular expression: /d(???)d/

Here is my code -

function QuestionsMarks(str) { 

//create a "clean" array containing only the numbers and question marks from str
  var result;
  let clean = str.match(/[0-9?]/g);
// join() the array back in to the string
  clean = clean.join("");     

// use match() to return an array of pairs that match the pattern d???d  
 clean = clean.match(/d(\?\?\?)d/gi);

//create a function sumCheck() that converts first and last char of every array string to Number and checks if the sum of digits is 10
//using forEach() run the sumcheck() on all strings in the array
 clean.forEach(sumCheck);

 function sumCheck(string){
        if((Number(string.charAt(0)) + Number(string.charAt(string.length - 1)))  == 10){
                result = true;
        }else{
            result = false;
        }
 }
    return result;
  }
QuestionsMarks("acc?7??sss?3rr1??????5");

Aucun commentaire:

Enregistrer un commentaire