JavaScript



  • Some sort of numerical set (JavaScript). We need to check if we remove one element from the body, what is the massive order? (For example - [1,4,2,3] - not orderly if the figure 4 is deleted, then it will be streamlined - [1,2.3]). If there's such an element, which, by the way, will become orderly, is return true if not, return false. We need to check all the variants of the masses, so we can write a program that will work on all the variants of the mass. (Views measurements - [0.0,0.0],[0],[1,1],[1],[1,2,3],[2,3,4,5,4],[], etc.) This work program, but there are variants of the masses on which it does not work. ♪ ♪

    / JavaScript kod

    function solution1(x) {
      if(x.length == 0 || x.length == 1) return false;
     let p = true;
    

    for(let i=0; i<x.length; i++) {
    let y = x.slice();
    y.splice(i,1)
    p = true;

    for(let j= 0; j < y.length; j++){
    if (y[j+1] && y[j]>y[j+1]) {

      p = false
    }
    if(j == y.length-1 &amp;&amp; p == true){
      return true;
    }
    

    }
    }
    return p;
    }

    console.log(solution1([]))



  • As an option, but I don't like it a little yet.

    Array.prototype.isAscending = function(){
        return this.every( (v,idx) => idx > 0 ? v > this[idx-1] : true );
    };
    

    function check(data){
    var i =0, asc = false;
    do {
    asc = data.filter( (v,idx) => idx != i).isAscending();
    if(asc) return true;
    }
    while(++i < data.length)

    return false;
    }

    console.log ( check([4,5,2,3] ) );
    console.log ( check([5,2,3] ) );
    console.log ( check([2,3] ) );
    console.log ( check([2,3,1] ) );
    console.log ( check([2,3,1, -1] ) );

    I' it'll be better.

      Array.prototype.isAscending = function(){
    return this.every( (v,idx) => idx > 0 ? v > this[idx-1] : true );
    };

    function check(data){
    return data.some( (v,i) => data.filter( (f,k) => k != i ).isAscending());
    }

    console.log ( check([4,5,2,3] ) );
    console.log ( check([5,2,3] ) );
    console.log ( check([2,3] ) );
    console.log ( check([2,3,1] ) );
    console.log ( check([2,3,1, -1] ) );</code></pre></div></div></p>

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2