Home

Question Marks

Resources

  1. CoderByte challenge

Question

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.

Examples

Input: "aa6?9" Output: false

Input: "acc?7??sss?3rr1??????5" Output: true

Answer

Correct:

function QuestionsMarks(str) { // expect string format const sum = (a, b) => parseInt(a) + parseInt(b); let flag = false; // check for number indexes const numIndexes = []; for (let i = 0; i < str.length; i++) { if (/[0-9]/gi.test(str[i])) numIndexes.push(i); } if (numIndexes.length < 2) return false; for (let i = 1; i < numIndexes.length; i++) { if (sum(str[numIndexes[i - 1]], str[numIndexes[i]]) === 10) { flag = true; const matches = str.slice(numIndexes[i - 1], numIndexes[i]).match(/\?/gi); if (!matches || matches.length !== 3) return false; } } return flag; }