0

Looking to replace an index in an array of numbers if that index is a number with a certain digit (say "1"). For instance, 414 has a "1" in it, so replace with text like "oops, no "1's"

I have a program to take a number as user input , and create an array that includes all the numbers up to and including the input number. I have 'if else' rules for replacing the offending single digit numbers. Not sure how to target multiple digit numbers that include a given digit.

var one = [1]
var two = [2]
var three =[3]
var userInput = $("#userInput").val();
var arr = [];
for (i=0;i<=userInput;i++) {
  arr.push(i);
  if (three.includes(arr[i])){
    arr.splice(i,1,"I'm sorry, Dave. I'm afraid I can't do that.")
  } else if (two.includes(arr[i])){
    arr.splice(i,1,"Boob!")
  } else if (one.includes(arr[i])){
    arr.splice(i,1,"Beep!")
  }
};

for numbers like 13, it should trigger the rule for numbers including 3, but instead it just displays 13 like there everything is fine.

AyamDobhal
  • 33
  • 7
alexanserg
  • 25
  • 4

3 Answers3

1

Not sure what your asking. But maybe a regex and replace will give you ideas:

"414".replace(/1/,"Sorry no 1s");

This will give change 414 to '4Sorry no 1s4'

drclaw
  • 2,463
  • 9
  • 23
0

Still unclear even when you commented with input and output because in comment you just need to replace the number if contains digit .. but in your code you tried to replace array of digits with strings.. Anyway, the next function will let you get the array After replace the numbers which contains the specific digit with 'problem' string

var userInput = 24;
var digit = 2;
console.log(ReplaceDigits(userInput , digit));

//--------------------------------------------
// The function
function ReplaceDigits(_userInput , _digit){
  var Arr = [];
  for (i = 0 ; i <= _userInput ; i++) {
    if((i+'').indexOf(_digit) < 0){
      Arr.push(i);
    }else{
      Arr.push('Problem');
    }
  }
  return Arr;
}
//--------------------------------------------
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

If you'll ask about (i+'') take a look at Best way to check if a number contains another number

Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
0

I'm learning JS and new to Stackoverflow, sorry if I'm wrong.

As you show on your comments, the first digit of userInput is important.

var userInput = 24;
var firstDigit = userInput.toString().substr(0,1);
var arr = [];
for (i = 0; i <= userInput; i++) {
  arr.push(i);
  if(firstDigit==='3') {
    if (arr[i].toString().includes('3')) {
      arr.splice(i,1,"I'm sorry, Dave. I'm afraid I can't do that.");
    }
  }
  if(firstDigit==='2') {
    if (arr[i].toString().includes('2')){
      arr.splice(i,1,"Boob!");
    }
  }
  if(firstDigit==='1') {
    if (arr[i].toString().includes('1')){
      arr.splice(i,1,"Beep!")
    }
  }
};
console.log(arr);
//[0, 1, "Boob!", 3, 4, 5, 6, 7, 8, 9, 10, 11, "Boob!", 13, 14, 15, 16, 17, 18, 19, "Boob!", "Boob!", "Boob!", "Boob!", "Boob!"]

I really hope that this is what you are looking for. Good luck!

edit: You could do this too!

var userInput = 24;
var firstDigit = userInput.toString().substr(0,1);
var arr = [];
for (i = 0; i <= userInput; i++) {
  arr.push(i);
};
newArr = arr.map(e => e.toString().includes(firstDigit) ? 'problem' : e);
console.log(newArr);
//[0, 1, "problem", 3, 4, 5, 6, 7, 8, 9, 10, 11, "problem", 13, 14, 15, 16, 17, 18, 19, "problem", "problem", "problem", "problem", "problem"]
PauloBorba
  • 156
  • 3
  • 5