Skip to content

Instantly share code, notes, and snippets.

@san-sekhon
Forked from austinreuter/self-assessment3.js
Created July 15, 2017 07:54
Show Gist options
  • Save san-sekhon/2a2345c581882eaf28a9d508829c571d to your computer and use it in GitHub Desktop.
Save san-sekhon/2a2345c581882eaf28a9d508829c571d to your computer and use it in GitHub Desktop.
//so, Scrimba had uneccessary "unexpected token" errors that do not exist on replit. Lots of time spent debugging things that don't need debugging
console.log('dog');
var each = function(collection, callback) {
if (Array.isArray(collection)) {
for (var i = 0; i < collection.length; i++) {
callback(collection[i], i, collection);
}
} else {
for (var key in collection) {
callback(collection[key], key, collection);
}
}
};
/*
Issues of 'each';
1)'typeof' array, not properly defined by JS, array is "typeof" object => Array.isArray(collection) to access array definition
2) iterator: not defined => callback
*/
function assertObjectEqual(actual, expected, testName) {
actual = JSON.stringify(actual);
expected = JSON.stringify(expected);
if (actual === expected) {
console.log('passed');
} else {
console.log('FAILED [' + testName + '] Expected "' + expected + '", but got "' + actual + '"');
}
}
// create function;
// input (object "name" prop; string name;) (lower) (upper);
// if obj; if obj name; add property "age": random number in range of two age groups, inclusive, exclusive outerrange;
// if string: create obj with "name" = string input, age prop, within range;
// if not obj "name", return undefined;
function addRandomAgeInclusive(person, lower, upper) {
//if object
//if upper is < lower; return person;
if (upper < lower) {
return person;
}
if (typeof person === 'object') {
//if name;
if (person.hasOwnProperty('name')) {
person.age = randomAge(lower, upper);
//add age: as random inclusive number
return person;
}
//if not name, return undefined;
return undefined;
}
var ouput = {};
output.name = person;
output.age = randomAge(lower, upper);
return output;
//if not object;
//create object
//add age = person;
//add age: as random inclusive number
}
function randomAge(lower, upper) {
//Math.random : random(0,1)
//Math.floor : integer
//range lower and upper
return Math.floor(Math.random() * (upper - lower) + 1) + lower - 1;
}
var upper = 12;
var lower = 10;
console.log(randomAge(10, 12));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment