Skip to content

Instantly share code, notes, and snippets.

@escusado
Created March 4, 2017 06:29
Show Gist options
  • Save escusado/a3879c770c4473244e153ad0b2680f07 to your computer and use it in GitHub Desktop.
Save escusado/a3879c770c4473244e153ad0b2680f07 to your computer and use it in GitHub Desktop.
// Variable declaration
var animalsVectorArray = [];
var arraySize = 10; // arbitrary array size
var randomPlaceForTheCat = Math.floor(Math.random() * arraySize)-1;
console.log('Our cat will be on the: ' + randomPlaceForTheCat + 'index.');
// Generate random values for the `animalsVectorArray` based on the `possibleAnimals`
for (var i=0; i<arraySize; i++) {
if (i===randomPlaceForTheCat) {
animalsVectorArray[i] = 'cat';
} else {
animalsVectorArray[i] = 'mouse';
}
}
console.log('Our `animalsVectorArray` looks like this:', JSON.stringify(animalsVectorArray)); // JSON.stringify does something really cool, can you figure it out?, try passing an object like {name:'omega',power:9000}
console.log('Looking for the cat...');
for (var i=0; i<arraySize; i++) {
if (animalsVectorArray[i] === 'cat') {
console.log('Found the cat! in the index:', i);
break; // breaks a for loop :)
} else {
console.log('Damn sorry, this is just a mouse');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment