1

I was solving an exercise with array methods; where I was expected to input a number and the returned value should be the same number with dashes between each two consecutive even numbers.

I have wrote this code so far but It's returning an empty array, It would be much better if you could point out my mistakes than giving an alternative solution.

function dashit(num){ 
   //test num = 025468, expected arr = ["0","-","2","5","4","-","6","-  ","8"]
    var arr = [];
    var prog = num.toString().split(""); // I suppose ["0","2","5","4","6","8"]
    for (var i = 0; i<num.length; i = i + 2){
        if (num[i] % 2 == 0 ){ // case of "0" and "2"
            if (num[i+1] % 2 == 0){
                arr.push(prog[i]); // "0" pushed from prog to arr
                arr.push("-");     // "-" pushed from prog to arr
                arr.push(prog[i+1]); // "2" pushed from prog to arr
            }
        }
        else { // case of "5" and "4"
                arr.push(prog[i]);  // "5" pushed from prog to arr
                arr.push(prog[i+1]); // "4" pushed from prog to arr
        }
    }
    return arr;
} 
14
  • 1
    I suppose num is an actual number (as in the comment), and num[i] doesn't make sense, but your debugger will tell you everything you need to know Commented Mar 17, 2016 at 22:28
  • 1
    num.length is undefined. Did you mean prog.length? Same with all the other num uses. Why are you using num in same cases and prog in others? Commented Mar 17, 2016 at 22:28
  • 1
    If you know num contains just numbers, you could use parseInt(num) to get an integer. If it could contain some junk characters, a /(\d+)/ regex may be more suitable. Is there any reason you're rolling your own solution? Commented Mar 17, 2016 at 22:30
  • 1
    You are using javascript strings like C strings- they are not. To extract a specific character use substring() method, however in your code prog is an array of characters, you are using num which is still an integer. Commented Mar 17, 2016 at 22:30
  • 1
    Then you should learn how to debug JavaScript ASAP! Otherwise, how you will be able to verify or fix your code? Commented Mar 17, 2016 at 22:31

3 Answers 3

2

You are comparing your num var, you have to compare the prog var :

    function dashit(num){ 
    var prog = num.toString().split(""); // I suppose ["0","2","5","4","6","8"]
    for (var i = 0; i<prog.length; i = i + 2){
        if (prog[i] % 2 == 0 ){ // case of "0" and "2"
            if (prog[i+1] % 2 == 0){
}
Sign up to request clarification or add additional context in comments.

1 Comment

Always willing to help :D
1

You are passing num as octal number(starts with 0)

Try pass it as a string

dashit("025468");

See working example:

function dashit(num){ 
  
    var arr = [];
    
    for (var i = 0; i<num.length; i = i + 2){
        if (num[i] % 2 == 0 ){ // case of "0" and "2"
            if (num[i+1] % 2 == 0){
                arr.push(num[i]); // "0" pushed from prog to arr
                arr.push("-");     // "-" pushed from prog to arr
                arr.push(num[i+1]); // "2" pushed from prog to arr
            }
        }
        else { // case of "5" and "4"
                arr.push(num[i]);  // "5" pushed from prog to arr
                arr.push(num[i+1]); // "4" pushed from prog to arr
                arr.push("-");
        }
    }
    return arr;
} 


document.write(JSON.stringify(dashit("025468")));

2 Comments

thanks alot for your input, it actually worked but for some reason only 1 dash is missing between 4 and 6
@MohamedHegazy you forget add arr.push("-"); in else statement, I've updated the code, please see
0

This problem is perfect for the use of recursion. I'm certain that's what your teacher wanted ;-)

var n = '02546878642';

var isPair = function(n1, n2) {
  return Number(n1) % 2 == 0 && Number(n2) % 2 == 0;
};

var result = (function f(i, o) {

  if (i.length < 1) return o;
  var first = i.shift();
  var next =  i[0];
  o.push(first);
  if (isPair(first, next)) {
    o.push('-');
  }
  return f(i, o);
})(n.split(''), []);



$('#result').html(result.join(''));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='result'>result</div>

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.