Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/objects #452

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
36 changes: 36 additions & 0 deletions staff/mary-quiroz/objects/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Adds an elment in a iterable object.
*
* @param object - The iterable object to mutate.
* @param value - The value to add.
*
* @throws {TypeError} when object is not an object.
*/

function add (object, value) {
object[object.lenth]= value
return object.length
}

console.log('CASE 1: add violet in colors')
var colors = {
0: 'red',
1:'blue',
2: 'green',
length: 3
}
var length = add(colors, 'violet')

console.log(length)
//4

console.log(colors)
/*
{
0:'red'
1:'blue'
2:'green'
3:'violet'
length: 4
}
*/
51 changes: 51 additions & 0 deletions staff/mary-quiroz/objects/extract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Extracts an element that matches the condition from an iterable object
*
* @param object - The iterable object to mutate.
* @param index -The index from which to extract a value.
* users[0]=users[0]
* users[1]=users[1]
* users[2]= callback
* users[3]=users[2]
* users[4]=users[3]
*
* @throws {TyoeError} When object is not an object, or when index is not a number.
*/
function extract(object, callback) {


}

console.log('CASE 1: extract user pepito form users' )

var users = {
0:{name: 'Wendy', age: 19},
1:{name: 'Peter', age:20},
2:{name: 'Pepito', age:50},
3:{name: 'Campa', age: 30},
4:{name: 'James', age:40},
length:5
}



var user = extract(users, function (user){
if(user.name === 'Pepito'){
return user
}
})



console.log(user)
//{name:'Pepito, age:50}

console.log(users)
/*
{
0:{name:'wendy', age: 19},
1:{name:'Peter', age 20},
2:{name: 'Campa',age:30},
3:{name: 'James', age:40},
length: 4
*/
82 changes: 82 additions & 0 deletions staff/mary-quiroz/objects/extractMany.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Extract manys elements that matches the condition from an iterable object.
*
* @param object - The iterable object to mutate.
* @param index - The index from which to extractMany a value.
*
* @thows {TypeError} when objects is not an object, or when index is not a number.
*/
//El extract es la funcion principal
function extractMany(object, callback) {

}
/*
var extracted = {}
var contador = 0

for (let key in object) {
// Si entra aqui, la funcion termina
// verifica si es un objeto
if (typeof object !== 'object') {
throw new TypeError('Is not a object')
}

// { name: 'Pepito', age: 50 }
var element = object[key] //elemt es un objeto de usuario
var result = callback(element)//es true o false

if (result) {
extracted[contador] = element
//{0:{ name: 'Pepito', age: 50 },
//1: { name: 'Campa', age: 30 },
//}
delete object[key]
contador++
}

}
return extracted


}

function fnUsers(user) {
//console.log(user)
return user.age > 25
}

extracted = extractMany(users, fnUsers)
*/

console.log('CASE 1: extracts many user forms users')
var users = {
0:{name:'wendy', age:19},
1:{name:'peter', age:20},
2:{name: 'Pepito', age:50},
3:{name: 'Campa', age:30},
4:{name:'Jame', age: 40},
length: 5
}

var extracted = extractMany(users,function(user){
return user.age > 25
})

console.log(extracted)

/*
{
0:{name: 'Pepito', age:50},
1:{name: 'Campa', age:30},
2:{name: 'James', age:40,}
length:3
}
*/
console.log(users)
/*
{
0:{name:'Wendy', age:19},
1:{name:'peter', age:20},
length:2
}
*/
33 changes: 33 additions & 0 deletions staff/mary-quiroz/objects/indexOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function indexOf (object, value){
for(var i = 0; i< object.length; i++){
var elem = object[i]

if(elem === value)
return i
}
return -1
}

console.log('CASE 1: index of blue in colors')
var colors={
0: 'red',
1:'blue',
2:'green',
length: 3

}

var index = indexOf(colors, 'blue')
console.log(index)
//1

console.log(colors)
/*
{
0: 'red',
1: 'blue',
2: 'green',
length: 3

}
*/
99 changes: 99 additions & 0 deletions staff/mary-quiroz/objects/insert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Inserts an element in iterable object at specfified index.
*
* @param object - The iterable object to mutate.
* @param index - The index from which to insert the given values.
* @param value - The value to insert.
*
* @throws {TypeError} When object is not an object, or when index is not a number.
*/
function insertMany(object, index, value) {
// TODO
if (!(object instanceof Object)) throw new TypeError(object + ' is not an Object')
}

console.log('CASE 1: insert skyblue in index 1')

var colors = {
0: 'red',
1: 'blue',
2: 'green',
length: 3
}

var length = insertMany(colors, 1, 'skyblue')

console.log(length)
// 4

console.log(colors)
/*
{
0: 'red',
1: 'skyblue',
2: 'blue',
3: 'green',
length: 4
}
*/

console.log('CASE 2: insert skyblue, gold and plum in index 2')

var colors = {
0: 'red',
1: 'blue',
2: 'green',
length: 3
}

var length = insertMany(colors, 2, 'skyblue', 'gold', 'plum')

console.log(length)
// 6

console.log(colors)
/*
{
0: 'red',
1: 'blue',
2: 'skyblue',
3: 'gold',
4: 'plum',
5: 'green',
length: 6
}
*/

console.log('CASE 3: fails on undefind object parameter')

try {
insertMany()
} catch (error) {
console.log(error)
// TypeError: undefined is not an Object
}

console.log('CASE 4: fails on 1 as an object parameter')

try {
insertMany(1)
} catch (error) {
console.log(error)
// TypeError: 1 is not an Object
}

console.log('CASE 5: fails on undefined as index parameter')

var colors = {
0: 'red',
1: 'blue',
2: 'green',
length: 3
}

try {
insertMany(colors)
} catch (error) {
console.log(error)
// TypeError: undefined is not a Number
}
Loading