Created
March 4, 2017 06:29
-
-
Save escusado/a3879c770c4473244e153ad0b2680f07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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