var map2 = new Map();
map2.set('cat', 'Kitten');
map2.set('cattle', 'Calf');
map2.set('cheetah', 'Cub');
map2.set('dog', 'Pup');
map2.set('1cat', 'Kitten');
map2.set('1cattle', 'Calf');
map2.set('1cheetah', 'Cub');
map2.set('1dog', 'Pup');
map2.set('2cat', 'Kitten');
map2.set('2cattle', 'Calf');
map2.set('2cheetah', 'Cub');
map2.set('2dog', 'Pup');
var babyAnimal2 = {
cat: 'Kitten',
cattle: 'Calf',
cheetah: 'Cub',
dog: 'Pup',
"1cat": 'Kitten',
"1cattle": 'Calf',
"1cheetah": 'Cub',
"1dog": 'Pup',
"2cat": 'Kitten',
"2cattle": 'Calf',
"2cheetah": 'Cub',
"2dog": 'Pup',
}
var animals = ["dog", "cat", "cattle", "cheetah","1dog", "1cat", "1cattle", "1cheetah","2dog", "2cat", "2cattle", "2cheetah",];
for (var i=0;i<100;i++){
var animal = animals[Math.floor(Math.random() * 12)];
function test(animal) {
switch(animal){
case 'cat': return 'Kitten'
case 'cattle': return 'Calf'
case 'cheetah': return 'Cub'
case 'dog': return 'Pup'
case '1cat': return 'Kitten'
case '1cattle': return 'Calf'
case '1cheetah': return 'Cub'
case '1dog': return 'Pup'
case '2cat': return 'Kitten'
case '2cattle': return 'Calf'
case '2cheetah': return 'Cub'
case '2dog': return 'Pup'
default: return "I don't know that"
}
}
console.log(test(animal))
}
for (var i=0;i<100;i++){
var animal = animals[Math.floor(Math.random() * 12)];
function test(animal) {
var babyAnimal = {
cat: 'Kitten',
cattle: 'Calf',
cheetah: 'Cub',
dog: 'Pup',
"1cat": 'Kitten',
"1cattle": 'Calf',
"1cheetah": 'Cub',
"1dog": 'Pup',
"2cat": 'Kitten',
"2cattle": 'Calf',
"2cheetah": 'Cub',
"2dog": 'Pup',
}
return babyAnimal[animal] ?? "I don't know that"
}
console.log(test(animal))
}
for (var i=0;i<100;i++){
var animal = animals[Math.floor(Math.random() * 12)];
function test(animal) {
if(animal==='cat'){
return 'Kitten'
} else if(animal=='cattle'){
return 'Calf'
} else if(animal==='cheetah'){
return 'Cub';
} else if(animal==='dog'){
return 'Pup';
} else if(animal==='1cat'){
return 'Kitten'
} else if(animal=='1cattle'){
return 'Calf'
} else if(animal==='1cheetah'){
return 'Cub';
} else if(animal==='1dog'){
return 'Pup';
} else if(animal==='2cat'){
return 'Kitten'
} else if(animal=='2cattle'){
return 'Calf'
} else if(animal==='2cheetah'){
return 'Cub';
} else if(animal==='2dog'){
return 'Pup';
}
return "I don't know that"
}
console.log(test(animal))
}
for (var i=0;i<100;i++){
var animal = animals[Math.floor(Math.random() * 12)];
function test(animal) {
return babyAnimal2[animal] ?? "I don't know that"
}
console.log(test(animal))
}
for (var i=0;i<100;i++){
var animal = animals[Math.floor(Math.random() * 12)];
function test(animal) {
const map1 = new Map();
map2.set('cat', 'Kitten');
map2.set('cattle', 'Calf');
map2.set('cheetah', 'Cub');
map2.set('dog', 'Pup');
map2.set('1cat', 'Kitten');
map2.set('1cattle', 'Calf');
map2.set('1cheetah', 'Cub');
map2.set('1dog', 'Pup');
map2.set('2cat', 'Kitten');
map2.set('2cattle', 'Calf');
map2.set('2cheetah', 'Cub');
map2.set('2dog', 'Pup');
return map1.get(animal) || "I don't know that"
}
console.log(test(animal))
}
for (var i=0;i<100;i++){
var animal = animals[Math.floor(Math.random() * 12)];
function test(animal) {
return map2.get(animal) || "I don't know that"
}
console.log(test(animal))
}
for (var i=0;i<100;i++){
var animal = animals[Math.floor(Math.random() * 12)];
function test(animal) {
if(animal==='cat') return 'Kitten';
if(animal=='cattle') return 'Calf';
if(animal==='cheetah') return 'Cub';
if(animal==='dog') return 'Pup';
if(animal==='1cat') return 'Kitten';
if(animal=='1cattle') return 'Calf';
if(animal==='1cheetah') return 'Cub';
if(animal==='1dog') return 'Pup';
if(animal==='2cat') return 'Kitten';
if(animal=='2cattle') return 'Calf';
if(animal==='2cheetah') return 'Cub';
if(animal==='2dog') return 'Pup';
return "I don't know that"
}
console.log(test(animal))
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Switch | |
Object Literal | |
If Else | |
Object Literal defined outside function | |
Map | |
Map - defined outsid function | |
If - just for Jason ;P |
Test name | Executions per second |
---|---|
Switch | 3128.5 Ops/sec |
Object Literal | 2997.0 Ops/sec |
If Else | 2793.2 Ops/sec |
Object Literal defined outside function | 2247.5 Ops/sec |
Map | 1948.6 Ops/sec |
Map - defined outsid function | 2318.6 Ops/sec |
If - just for Jason ;P | 2540.5 Ops/sec |
A classic JavaScript interview question!
The problem is that the test
function is being called with an object, but it's expecting a string value for the animal
property.
To fix this, you can modify the test
function to take an object as well. Here's an updated implementation:
function test(animal) {
if (animal.animal === '1cattle') return 'Calf';
if (animal.animal === '1cheetah') return 'Cub';
if (animal.animal === '1dog') return 'Pup';
if (animal.animal === '2cat') return 'Kitten';
if (animal.animal === '2cattle') return 'Calf';
if (animal.animal === '2cheetah') return 'Cub';
if (animal.animal === '2dog') return 'Pup';
return "I don't know that"
}
By adding the .animal
property accessor, we can now access the animal
property of the object being passed to the function.
Alternatively, you could also consider making the test
function more flexible by using a switch statement or an object with string keys, like this:
const animalMap = {
'1cattle': 'Calf',
'1cheetah': 'Cub',
'1dog': 'Pup',
'2cat': 'Kitten',
'2cattle': 'Calf',
'2cheetah': 'Cub',
'2dog': 'Pup'
};
function test(animal) {
return animalMap[animal];
}
This approach would eliminate the need for multiple if
statements and make the code more concise.