Skip to content

Instantly share code, notes, and snippets.

@boisbb18
Created June 11, 2017 21:48
Show Gist options
  • Save boisbb18/e8c9da5fe4511fb72aea3c53535e2f0d to your computer and use it in GitHub Desktop.
Save boisbb18/e8c9da5fe4511fb72aea3c53535e2f0d to your computer and use it in GitHub Desktop.
Boiskhon Bakhodirov
// javascript
/*1Debug each: You are given this function each, but it doesn't work exactly as expected.
It should call callback on value, key, and collection respectively for each element of collection,
and accept both arrays and objects. Identify everything incorrect with each as it is provided,
and modify the function so that it works as expected. Be sure to list all that was incorrect about the original function.
*/
var each = function(collection, callback) {
if (Array.isArray(collection)){
for (var i = 0; i < collection.length; i++) {
collection[i] = callback(collection[i], i, collection);
}
} else{
for(var key in collection){
callback(collection[key],key,collection)
}
}
return collection;
};
function printArray(element,index,array){
console.log(element);
}
//each([1,2,3,4,5],printArray);
//each({name:"Bois",lastName:'Bakhodirov'},printArray);
/*
Issues with 'each'
1)If statement was incorrect.
2)callBack function wasn't created
3)so is iteartor function was not passed;
etc..
*/
//console.log(each([1,2,3],callBack));
/*2Part 2
Write a function addRandomAgeInclusive that takes in a name string or an object with a name property,
and two numbers. This function should generate a random number within the range of the two input numbers,
and return an object with an added age property along with the randomly generated number.
Edge cases required for consideration:
First input value is not a string or an object with a 'name' property: return undefined
Second input value is larger than third input value: return original string or object, unmodified.
addRandomAgeInclusive('Mayor', 20, 25); // { name: 'Mayor', age: 25 }
addRandomAgeInclusive({ name: 'Sean', id: 10330293 }, 30, 40); // { name: 'Sean', id: 10330293, age: 39 }
addRandomAgeInclusive({ firstName: 'John' }, 10, 12); //undefined
addRandomAgeInclusive('Kai', 19, 18); //'Kai'
*/
function addRandomAgeInclusive(str,num1,num2){
var obj={};
if(typeof str === 'string'){
obj['name'] = str;
}else if (typeof str === 'object'){
if(str.name != undefined){
obj['name']=str.name;
} else{
return 'undefined';
}
}else{
return 'undefined';
}
if(num2>num1){
obj['age'] = Math.floor(Math.random() * (num2 - num1) + num1);
return obj;
}else{
return obj.name;
}
}
//console.log(addRandomAgeInclusive('Mayor', 20, 25)) // { name: 'Mayor', age: 25 }
//console.log(addRandomAgeInclusive({ name: 'Sean', id: 10330293 }, 30, 40)); // { name: 'Sean', id: 10330293, age: 39 }
//console.log(addRandomAgeInclusive({ firstName: 'John' }, 10, 12)); //undefined
//console.log(addRandomAgeInclusive('Kai', 19, 18)); //'Kai'
function assignGroup(str1){
var arr ='ABCD'
var obj2={};
if(typeof str1 == 'string'){
obj2['name']= str1;
}else if(typeof str1 == 'object'){
obj2['name'] = str1.name;
} else{
return 'undefined';
}
var temp = Math.floor(Math.random() * (4 - 0) + 0);
obj2.group = arr.charAt(temp);
return obj2;
}
// { name: 'Harold', group: 'B' }
//console.log(assignGroup({ name: 'Kyla'})); // { name: 'Kyla', group: 'C' }
//console.log(assignGroup('Cynthia')); // { name: 'Cynthia', age: 'B' }
/*4 Write a function addAges that takes in an array of students and two numbers.
This function should return a new array with a random number within the two input numbers (inclusive)
added as an age property to each element.*/
var students = ["Lula", "Marla", "Isa", "Geoff", "Cassie", "Ana", "Kayla", "Mark", "Katja", "Kelly", "John", "Megan", "Omar", "Oren", "Pauli", "Rick", "Rita", "Ryan", "Tony", "Tyler", "Viktor"];
//addAges(students, 12, 15)[0];
function helperAges(element,index,array){
var obj = {};
var num1 = 12;
var num2 = 15;
obj['name']= element;
obj.age = Math.floor(Math.random() * ((num2 +1)- num1) + num1);
return obj;
}
function addAges(arr,num1,num2){
var array = each(arr,helperAges);
return array;
}
console.log(addAges(students, 12, 15));
/*5 Write a function groupAllStudents that takes in an array of students, and returns a new array with object elements.
Each object will contain the respective student's name and a group.
*/
function groupAllStudents(students){
var array2 = each(students,helperAssign);
return array2;
}
function helperAssign(element,index,array){
var arr ='ABCD'
var obj2={};
obj2['name']= element;
var temp = Math.floor(Math.random() * (4 - 0) + 0);
obj2.group = arr.charAt(temp);
return obj2;
}
console.log(groupAllStudents(students));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment