0

What I am trying to do is make a function that takes user input, splits that input into an array of numbers, then replaces each number with a string depending on what the number is. It seems all this does now is return undefined, because it doesn't want to reassign the index to what It tell it to. I want to do this using a for loop or the forEach method if possible.

Here is my code so far:

  function stringify(num){
  var array = num;

  for (i in array) {
    if (array[i] ==  2) {
      array[i] = "x";
    } else if (array[i] == 5) {
      array[i] = "y";
    } else {
      array[i] = "meow"
    }
    return array;
  } 
}

Here is an example of what I want to eventually happen:

stringify(52527);
y x y x meow

3 Answers 3

2

You could map the new array by using an object for the wanted replacings.

function stringify(num) {
    return Array.from(num, v => ({ 2: 'x', 5: 'y' }[v] || v));
}

console.log(stringify('5252'));

With default value

function stringify(num) {
    var values = { 2: 'x', 5: 'y', default: 'meow' };
    return Array.from(num, v => values[v] || values.default);
}

console.log(stringify('52527'));

Sign up to request clarification or add additional context in comments.

3 Comments

I am trying to specifically solve this problem using a for loop. I want to be able to add extra conditions to this function like if a number is divisible by 6 then the index becomes "b" for example.
@lateralus500, please ask in the question, what you really want.
btw, a single digit is only by six divisible, if it is six.
1

Convert the input data to a string, and split the string to characters:

function stringify(num) {
  var array = String(num).split('');

  for (i in array) {
    if (array[i] === '2') {
      array[i] = "x";
    } else if (array[i] === '5') {
      array[i] = "y";
    } else {
      array[i] = "meow"
    }
  }
  
  return array; // the return should be after the loop ends
}

console.log(stringify(52527));

Another solution would be to use a Array.map() to iterate after the split, and an object with the characters that should be replaced:

function stringify(num) {
  var rep = { 2: 'x', 5: 'y' };
  
  return String(num)
    .split('')
    .map(function(c) {
      return rep[c] || 'meow';
    });
}

console.log(stringify(52527));

3 Comments

This only changes the first index. Also if I were to make this a user input stuff in HTML it just makes it "N" "a" "N" even if it is parsed to an integer
Look at the code again, and see were the return is (the comment // the return should be after the loop ends).
nvm I realized I had my return in the wrong place. this works great
0

I think you might have a couple of problems:

  1. Using a for ... in loop for an array or an iterator isn't ideal; rather use the construction for (var i = 0; i < array.length; i += 1).

  2. As other commenters have said, make sure you're converting the passed argument, which is a Number, into a String. My preference for doing this something like var numString = '' + num;. If you're using es6, you could also use the template literal notation: var numString = `${num}`;

  3. Be careful of using the double equal sign == versus the triple equals === to check equality. The double equal will do some arcane type-casting of its operands. Safer to stick with the triple equal unless you're sure.

  4. Strings are immutable, which means that while you can access the values of their indices, you can't change those values. I would suggest creating a new array that you then join upon return:

    function stringify(num) {
      var numString = '' + num;
      var ret = [];
    
      for (var i = 0; i < numString.length; i += 1) {
        if (numString[i] === '2') { // The index values will be strings, not numbers now
          ret.push('x');
        } else if (numString[i] === '5') {
          ret.push('y');
        } else {
          ret.push('meow');
        }
      }
    
      return ret.join(' ');
    }
    

this results in:

stringify(52527); y x y x meow

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.