No tienes acceso a esta clase

隆Contin煤a aprendiendo! 脷nete y comienza a potenciar tu carrera

Playground: crea tu propia implementaci贸n de shift

8/29

Aportes 44

Preguntas 1

Ordenar por:

驴Quieres ver m谩s aportes, preguntas y respuestas de la comunidad?

o inicia sesi贸n.

Hasta aqu铆 este curso ha sido s煤per genial para mi鈥
El poder construir un Array u objeto y poder manipularlo a partir de mis propios m茅todos, para modificar sus propiedades ha sido muy enriquecedor para mi. Con esto he logrado ser m谩s detallista de los pasos necesarios para lograr algo鈥 En fin, el poner en pr谩ctica la l贸gica para crear peque帽os algoritmos, haciendo uso de los ciclos y condicionales y entender la l贸gica detr谩s de los m茅todos, es lo que hace que este curso tenga un alto valor en aprendizaje significativo鈥 Excelente como siempre, la explicaci贸n de nuestro Prof. Diego, el Grande.


.
.
.
.

  shift(){
    // Tu c贸digo aqu铆 馃憟
    if (this.length !== 0) {
      const firstItem = this.data[0];
      this.shiftIndex(0);

      return firstItem;
    } else {
      return undefined;
    }
  }

dejo mi solucion
*
*
*
*
*
*
*
*
*
*
*
*

 shift() {
        // Tu c贸digo aqu铆 馃憟
        if (this.length == 0) {
            return undefined;
        }

        //obtengo el primer elemento
        const firstItem = this.data[0];

        // corrijo el numero de indice de cada elemento
        for (let i = 0; i < this.length; i++) {
            this.data[i] = this.data[i + 1];
        }
        // remuevo el ultimo slot que queda con undefined
        delete this.data[this.length - 1];

        // corrijo el largo del array
        this.length--;
        
        return firstItem;
    }

Mi soluci贸n al reto. Utilizo el
Object.keys(this.data)
en lugar del
this.length
porque es de las cosas que m谩s me cost贸 aprender al recorrer objetos, es m谩s una elecci贸n personal para tener presente eso y no olvidar c贸mo se hace

import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  shift() {
    // Tu c贸digo aqu铆 馃憟
    let keys = Object.keys(this.data);
    let objectSize = keys.length;

    if (objectSize == 0) return undefined;

    let valueToShift = this.data[0];
    
    if (objectSize > 0) {
      for (let actualPosition = 0; actualPosition < objectSize; actualPosition++) {
        this.data[actualPosition] = this.data[actualPosition + 1];
      }
      delete this.data[objectSize - 1];
      this.length--;
    }
    return valueToShift;
  }
}


Mi querido 鈥渁portazo!鈥 a la comunidad鈥 馃槂

shift() {
// Tu c贸digo aqu铆 馃憟
let first = this.data[0];
if (this.length === 0) {
return undefined;
} else {

for (let i = 0; i <= this.length - 1; i++) {
this.data[i] = this.data[i + 1]
}
delete this.data[this.length - 1];
this.length鈥
return first
}
}

reto

import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  shift(){
    // Tu c贸digo aqu铆 馃憟
    if (this.length === 0) {
      return undefined
    } else {
      const element = this.data[0]
      for (let i = 0; i < this.length; i++) {
          this.data[i] = this.data[i + 1]
      }
      delete this.data[this.length - 1]
      this.length -= 1
        return element
      }
    
  }
}

Soluci贸n 馃槃鈥

.
.
.
.

import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  shift() {
    const firstItem = this.data[0];

    if (firstItem) { 
      for (let i = 1; i < this.length; i++) {
        this.data[i - 1] = this.data[i];
      }
      delete this.data[this.length - 1];
      this.length--;
    }

    return firstItem;
  }
}

Aqui mi solucion, usando el metodo shiftIndex() del profe 馃槂

Esta es mi soluci贸n a este reto 馃挌


Ac谩 pueden probar el c贸digo, (hice un return 鈥榰ndefined鈥 implicito ya que al estar vac铆o el Array sus valores son undefined)

import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  shift(){
    const item = this.data[0]                               
    for (let i = 0; i < this.length - 1; i++) {             
      this.data[i] = this.data[i + 1];                          
    }

    delete this.data[this.length - 1];                          
    this.length--;  
    return item;  
  }
}

Mi solucion

  shift() {
    if (this.length > 0) {
      let deletedItem = this.data[0]
      for (let i = 0; i < this.length; i++) {
        this.data[i] = this.data[i + 1]
      }
      delete this.data[this.length - 1]
      this.length--
      return deletedItem
    }
    return undefined
  }

Mi aporte:
*
*
*
*
*
*
*
*
*
*
shift(){

const nombrePrimeraPosicion = this.data[0];
const cantIndex = this.length;

for (let i = 0; i < cantIndex - 1; i++) {
  this.data[i] = this.data[i + 1];
}

delete this.data[cantIndex - 1];
this.length = this.length === 0 ? 0 : this.length - 1;

return nombrePrimeraPosicion;

}

Mi solucion al reto

if (this.length === 0) {
      return undefined; // No se puede eliminar elementos de un array vac铆o
    }
    const firsrItem = this.data[0];

    for (let i = 0; i < this.length - 1; i++) {
      this.data[i] = this.data[i + 1];
    }
    delete this.data[this.length - 1];
    this.length--;
    return firsrItem;
    
  }

** Soluci贸n **
*
*
*
*
*
*
*
*
*
*
*
*


import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  shift() {
    
    if (!this.data[0]) return undefined;
    
    const firstItem = this.data[0];

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

      this.data[i] = this.data[i + 1];

    }

    delete this.data[this.length - 1];

    this.length--;

    return firstItem;

  }
  
}

Solucion

import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  shift() {
    const backup = this.data[0];
    if (this.length === 0) {
      return undefined;
    }
  
    for (let i = 0; i <this.length; i++){
      this.data[i] = this.data[i + 1];
    }
    delete this.data[this.length - 1];
    this.length--;
    return backup;
  }
}

Meu humilde resoluci贸n:

shift(){
        const deletedItem = this.data[0]
        delete this.data[0]
        this.length--
        return deletedItem
    }

A este si lo pude hacer:

  shift(){
    if (this.length <= 0) return undefined

    let firstElement = this.data[0]
    delete this.data[0]

    for (let i = 0; i < this.length; i++) {
      this.data[i] = this.data[i + 1]
    }
    delete this.data[this.length - 1]
    this.length--
    return firstElement
  }

Al anterior no 馃槮

import { MyArray } from 鈥./MyArray鈥;

export class BetterArray extends MyArray {
constructor(){
super()
this.length = 0
this.data = {}
}

shift(){
const firstItem = this.data[0];
this.shiftIndexToLeft();
delete this.data[this.length - 1];
this.length鈥;
return firstItem;

}
}

Aprovechando mi implementacion de delete quedo asi

import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  delete(index) {
    const item = this.data[index]
    if (!item) return
    delete this.data[index]
    const keys = Object.keys(this.data).map(Number).sort()
    const rightKeys = keys.slice(index)
    rightKeys.forEach(index => {
      const itemData = this.data[index]
      delete this.data[index]
      this.data[index - 1] = itemData
    })
    this.length = Object.keys(this.data).length
    return item
  }
  shift() {
    return this.delete(0)
  }
}

Mi aporte:

import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  shift(){
    // Tu c贸digo aqu铆 馃憟
    const firstItem = this.data[0];

    this.shiftIndex(0);
    return firstItem;
    
  }

  shiftIndex(index) {
    for (let i = index; i < this.length - 1; i++) {
      this.data[i] = this.data[i + 1];
    }
    delete this.data[this.length - 1];
    this.length--;
  }


}
 

.
.
.
.
.
.
.
.
.

import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  shift() {
    const itemEliminado = this.data[0];
    if (this.length > 0) {
      if (itemEliminado != undefined) {
        let newData = {}
        for (let i = 0; i < this.length; i++) {
          newData[i] = this.data[i + 1];
        }
        this.data = newData;
        this.length -= 1;
      }
    }
    return itemEliminado;
    
  }
}

Mi soluci贸n la saqu茅 de la siguiente conclusi贸n:
Utilizamos el mismo c贸digo que ya nos sirvi贸 para eliminar un 鈥淚ndex鈥 espec铆fico, solo que este no lleva un par谩metro ya que se supone que SOLO debe eliminar el primer elemento del array.

shift() {
        const firstItem = this.data[0]; // Le indicamos la posici贸n del array en la cual implementar谩 los cambios.
        if(this.length !== 0){ // Lo hacemos para todos los valores diferentes de cero seg煤n lo pide el ejercicio
            for(let i = 0; i < this.length -1; i++) { // El "let i = 0" porque es donde queremos que suceda
                this.data[i] = this.data [i + 1];
            }
            delete this.data[this.length - 1];
            this.length--;
        } else { // Para el valor de cero retornamos "Undefined"
            return undefined;
        }
        return firstItem; // Devolvemos la const.
    }

y listo. XD

import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  shift() {
    const firstItem = this.data[0];
    for (let i = 0; i < this.length - 1; i++) {
      this.data[i] = this.data[i + 1];
    }
    delete this.data[this.length - 1];
    this.length--;
    return firstItem;
  }
}

Soluci贸n.

import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  shift(){
    // Tu c贸digo aqu铆 馃憟
    if (this.length === 0)
      return undefined;

    let first = this.data[0];
    for (let i = 0; i < this.length - 1; i++) {
      this.data[i] = this.data[i + 1];
    }
    delete this.data[this.length - 1];
    this.length--;
    
    return first;
  }
}

Hasta ac谩, excelente curso!!
Comparto mi soluci贸n.

shift() {
    // Tu c贸digo aqu铆 馃憟
    if (this.length === 0) return undefined;
    const item = this.data[0];
    for (let i = 0; i < this.length - 1; i++){
      this.data[i] = this.data[i + 1];
    }
    delete this.data[this.length - 1];
    this.length--;
    return item;
  }

.
.
.
.
.
.
.
.
.
.
.

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  pop() {
    const lastItem = this.data[this.length - 1];
    delete this.data[this.length - 1];
    this.length--;
    return lastItem;
  }
  
  shiftIndex(index) {
    for (let i = index; i < this.length - 1; i++) {
      this.data[i] = this.data[i + 1];
    }
    this.pop();
  }

  shift() {
    const firstItem = this.data[0];
    delete this.data[0];
    this.shiftIndex(0);
    return firstItem;
  }
}

Hola chicos, aqu铆 les dejo mi c贸digo pensado con programaci贸n funcional. Espero les sea de utilidad.

import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  delete(index) {
    const item = this.data[index];
    this.shiftIndex(index);

    return item;
  }
  shiftIndex(index) {
    for (let i = index; i < this.length - 1; i++) {
      this.data[i] = this.data[i + 1];
    }
    delete this.data[this.length - 1];
    this.length--;
  }

  shift(){
    // Tu c贸digo aqu铆 馃憟
    if (this.length == 0) {
      return undefined
    } else {
      return this.delete(0);
    }
  }
}

hola que tal!
alguien me podria decir que esta mal? me retorna todo lo que me pide menos el object

shift(){
if (this.length === 0) {
return undefined
} else {
const firstitem = this.data[0]
delete this.data[0]
this.length鈥

   for (let i = 1; i < this.length; i++) {
    this.data[i] = this.data[i -1]
    delete this.data[this.length-1]
  }
return firstitem
}
//Mi soluci贸n :)
        shift(){
            const fisrtItem = this.data[0]
            for (let i = 0; i <= this.length -1; i++){
            this.data[i] = this.data[i + 1]
            }
            delete this.data[this.length -1];
            this.length--;
            return fisrtItem;   
        } 

En s铆 no es tan dificil si se hace uso de lo ya hecho antes. Porque shift se puede implementar con las funciones pop y shiftIndex:

export class MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  pop() {
    const item = this.data[this.length - 1];

    delete this.data[this.length - 1];
    this.length--;

    return item;
  }

  shiftIndex(index) {
    for (let i = index; i < this.length; i++) {
      this.data[i] = this.data[i + 1];
    }
  }

  shift() {
    if (!this.length) {
      return undefined;
    }
    const item = this.data[0];

    this.shiftIndex(0);
    this.pop();

    return item;
  }
}

Mi soluci贸n:
*
*
*
*
Alerta Spoiler!
*
*
*
*
*
*

shift() {
    if (this.length > 0) {
      const aEliminar = this.data[0];
      delete this.data[0];
      for (let i = 1; i < this.length; i++) {
        this.data[i - 1] = this.data[i]
      }
      delete this.data[this.length - 1];
      this.length--;
      return aEliminar;
    } else {
      return undefined;
    }
    
    
  }

mi solucion:

import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  shift(){
    if (!this.data) return undefined;
    let item = this.data[0];
    const newList = {};
    for (let i = 0; i < this.length - 1; i++) {
      newList[i] = this.data[i + 1];
    }
    this.length--;
    this.data = newList;
    return item;
    
  }
}

la sol es estaa y solo esta:

shift(){
    let newdata = {};
    if (this.length == 0) {
      return undefined;
    }
    for (let i = 0; i < this.length; i++) {
      if (this.data[i + 1]) {
        newdata[i] = this.data[i + 1];
      }
    }
    const datatoreturn = this.data[0];

    this.data = newdata;
    this.length--;
    return datatoreturn;
  }

Comparto mi soluci贸n al reto鈥










import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  shift(){
    // Tu c贸digo aqu铆 馃憟
    if (this.length > 0) {
      const element = this.data[0];
      for (let i = 0; i < this.length - 1; i++) {
        this.data[i] = this.data[i + 1];
      }
      delete this.data[this.length - 1];
      this.length--;
      return element;
    } else {
      return undefined;
    }
  }
}
shift() {
    const fisrtItem = this.data[0];
    delete this.data[0];

    for (let i = 0; i < this.length; i++) {
      this.data[i] = this.data[i + 1]
    }

    delete this.data[this.length];
    this.length--;

    return fisrtItem;
  }

Mi soluci贸n
*
*
*
*
*
*
*
*
*

  shift(){
    if (this.length === 0) {
      return undefined
    } else {
      
    let deleted = this.data[0];
    for (let i = 0; i < this.length; i++) {
      this.data[i] = this.data[i + 1]
    }
      delete this.data[this.length - 1]
    this.length--
    return deleted
    }
  }

mi aporte, sinceramente hasta ahora me vengo preguntando el por qu茅 del segundo delete pero asi funcion贸

shift(){
    const deleted = this.data[0]; 
    delete this.data[0];
    this.length--;
    for(let i = 0; i<this.length;i++){
        this.data[i]=this.data[i+1];
    }   
    delete this.data[this.length]
    return `El elemento borrado fue ${deleted}`;
    } 
    shift() {
        const firstItem = this.data[0];
        delete this.data[0];
        for(let i = 0; i < this.length - 1; i++) {
            this.data[i] = this.data[i+1];
        }
        delete this.data[this.length - 1];
        this.length--;
        return firstItem;
    }

Sin duda recorrer arrays al rev茅s como en el ejercicio anterior es mentalmente m谩s complicado para mi que hacia adelante. Quiz谩 sea bueno aprender otro idioma donde se lea rtl.

shift() {
    if (!this.length) return undefined;

    const item = this.data[0];

    for (let i = 0; i < this.length; i++) {
      this.data[i] = this.data[i + 1];
    }

    this.length--;
    return item;
  }

ez

  shift() {
    if (this.length) {
      const deleted = this.data[0];
      delete this.data[0];
      this.length--;

      if (this.length > 1) {
        for (let key in this.data) {
          this.data[key - 1] = this.data[key];
        }
        delete this.data[this.length];
      }

      return deleted;
    } else { return undefined }
  }

Les comparto mi solucion, con mucho cari帽o

<code> 
import { MyArray } from "./MyArray";

export class BetterArray extends MyArray {
  constructor(){
    super()
    this.length = 0
    this.data = {}
  }

  shift(){
    this.length === 0 && undefined
    const deletedElement = this.data[0]
    for (let i = 0; i <= this.length - 1; i++) {
      this.data[i] = this.data[i + 1]
    }
    delete this.data[this.length - 1]
    this.length--
    return deletedElement
    
  }
}

Mi soluci贸n:

shift(){
const item = this.data[0];
for (let i = 0; i < this.length - 1; i++) {
  this.data[i] = this.data[i + 1];
} delete this.data[this.length - 1];
this.length--;
return item;   

}

  shift() {
    if (this.length === 0)
      return undefined;

    const item = this.data[0];
    for (let i = 0; this.length >= i; i++) {
      this.data[i] = this.data[i + 1];
    }

    this.length--;
    return item;
  }

Hola. Les comparto mi soluci贸n a este reto. Si tienen alguna retroalimentaci贸n, es bienvenida. Gracias.

shift() {

        if (this.length !== 0) {
            
            const firstItem = this.data[0];

            for (let i = 0; i < this.length; i++) {
                this.data[i] = this.data[i + 1];
            }

            this.length--;
        
            return "First Item deleted: " + firstItem;
        }
        else {
            return "Array is empty";
        }

    }

Soluci贸n:

import { MyArray } from "./MyArray";
export class BetterArray extends MyArray {
  constructor()
  {
    super()
    this.length = 0
    this.data = {}
  }
  get($_index) {
    return this.data[$_index];
  }
  remove($_index) {
    // Save the element to be removed 
    const ele = this.get($_index);
    if (ele === undefined) return undefined
    // shiftIndex the array 1 position on the left
    this.shiftIndexLeft($_index)
    // flush space the last one (Duplicate) 
    delete this.data[--this.length];
    // return the removed item 
    return ele;
  }
  shiftIndexLeft($_n) {
    // Displace the array 1 position on the left 
    for (let i = $_n; i < this.length - 1; i++) {
      this.data[i] = this.data[i + 1];
    }
  } shift() {
    // Remove and return the first element 
    return this.remove(0);
  }
}
undefined