Tasks review #1

Agenda

  • Warm up!
  • Love triangle
  • Smart sorter

Warm up!

function warmup(temperature) {

    temperature = temperature * 9;
    temperature = temperature / 5;
    temperature = temperature + 32;
    return temperature;

};

Solution #1

function warmup(temperature) {
  // your implementation here
  var tFahr = 0;
  tFahr = (9 / 5) * temperature + 32;
  return tFahr;
};

Solution #2

function warmup(temperature) {
    return temperature * (9/5) + 32;
};

Solution #3

Love triangle

/**
 * @param preferences - an array of integers. Indices of people, whom they love
 * @returns number of love triangles
 */

module.exports = function getLoveTrianglesCount(preferences = []) {
  var count = 0;
  var summ = 0;
  var arr = [];

  for(let i = 0; i<preferences.length; i++) {

    if (preferences[i+1] == preferences[i] + 1 && preferences[i+2] == preferences[i] - 1) {
      summ = ("" + preferences[i+1]) + ("" + preferences[i]) + ("" + preferences[i+2]);
      for (let c = 0; c < arr.length; c++) {
        if(arr[c] == summ) {
          summ = 0;
        }
      }
      if(summ != 0) {
        arr.push(summ);
        count += 1;
      }
    }
  }
  return count;
};

Solution #1

function getLoveTrianglesCount(preferences = []) {
    // your implementation
    var array = preferences;
    var count = 0;
    let triangles = [];
    
    array.forEach(function(value, index){
      let check = true;
      if(triangles.includes(value - 1)){
        check = false;
      }
      if (check) {
      let length = array.length;
      let secondvalue;
      let thirdvalue;
      if(checkup(value - 1, length) ){
        secondvalue = array[value - 1]; 
        if(checkup(secondvalue - 1, length) ){
          thirdvalue = array[secondvalue - 1];
          if(secondvalue === value){
    
            }else{
            if(thirdvalue - 1 === index){
              count++;
              triangles.push(value - 1);
              triangles.push(secondvalue - 1);
              triangles.push(thirdvalue - 1);
            }
          }
        }
      }
    }
    });
    return count;
 
};

function checkup(index, length){
    if (index >= 0 && index < length){
        return true;
    }else{
        return false;
    } 
};

Solution #2

function getLoveTrianglesCount(preferences = []) {
  let sum = 0;
  for (let i = 0; i < preferences.length; i++) {
    const conditionOne = preferences[preferences[preferences[i] - 1] - 1] === i + 1;
    const conditionTwo = i + 1 !== preferences[i];
    if( conditionOne && conditionTwo ) {
      sum++;
    }
  }
  return sum / 3;
};

Solution #3

Smart sorter

class Sorter {
  constructor() {
    this.Array = [];
    this.COMPARATOR = (left, right) => left - right;
  return this;
  }

  add(element) {
  this.Array[this.Array.length] = element;
  }

  at(index) {
    return this.Array[index];
  }

  get length() {
    return this.Array.length;
  }

  toArray() {
    return this.Array;
  }

  sort(indices) {
    var i, j, min, temp, g;
    for ( i = 0; i < indices.length - 1; i++){
      min = i;
        for (j = i + 1; j < indices.length; j++){
          if (indices[i] > indices[j]) {
            g = indices[i];
            indices[i] = indices[j];
            indices[j] = g;
          }
          if (this.COMPARATOR(this.Array[indices[min]], this.Array[indices[j]]) > 0) { min = j };
        }
      temp = this.Array[indices[i]];
      this.Array[indices[i]] = this.Array[indices[min]] ;
      this.Array[indices[min]] = temp;
    }
    return this.Array;
  }

    setComparator(compareFunction) {
    this.COMPARATOR = compareFunction;
    }

}

Solution #1

class Sorter {
  constructor() {
    // your implementation
    this.arr = [];
   this.funcCompare = (a,b) => {
   return a - b;
   }
  }

  add(element) {
  
    this.arr.push(element);
  
  }

  at(index) {
    // your implementation
     return this.arr[index];
  }

  get length() {
      // your implementation
    return this.arr.length;
  }

  toArray() {
    // your implementation
    return this.arr
  }

  sort(indices) {
    // your implementation
     indices.sort((a,b) => {
   return a - b});

    let temp = [];
    for (let i in indices) {
      temp.push(this.arr[indices[i]]);
    }

    temp.sort(this.funcCompare);

    for(let i = 0; i<indices.length; i++){
      this.arr.splice(indices[i], 1, temp[i]);
    } 
  }


  

  setComparator(compareFunction) {
    // your implementation
     this.funcCompare = compareFunction;
  }
}

Solution #2

class Sorter {
  constructor() {
    this.box=[];
  }

  add(element) {
      this.box.push(element);
  }

  at(index) {
      return this.box[index]
  }

  get length() {
      return this.box.length;
  }

  toArray() {
    return this.box;
  }

  sort(indices) {
    var andrey = [];
    indices.sort(function(left, right) {return left - right});
    for (let i of indices) {
      andrey.push(this.box[i]);
    }
    andrey.sort(function(left, right) {return left - right});
    for (let i of indices) {
      this.box[i] = andrey.shift();
    }
  }

  setComparator(compareFunction){
      this.comparator = compareFunction;
  }
}

Solution #3

Tasks review #1

By shutya

Tasks review #1

  • 280