javascript - What does a function with multiple return statements return? -


this exercise out of head first javascript programming book.

    function findcarinlot(car) {             (var = 0; < lot.length; i++) {                 if (car === lot[i]) {                     return i;                 }             }             return -1;         }      var lot = [chevy, taxi, fiat1, fiat2]; 

i'm not going write code chevy, taxi, etc, objects , function assigned 1 of 4 objects , value give variable in exercise. question going on with:

    return -1; 

so doesn't returned when function completes? or negated when

    return i; 

happens? or both values returned? sort out poor brain , tell me rules here.

depending on return hits, thing returns

function findcarinlot(car) {             (var = 0; < lot.length; i++) {                 if (car === lot[i]) {                     return i; // if if statement true return here , function end , never make next return                 }             }             return -1; // called if above if statement false         }      var lot = [chevy, taxi, fiat1, fiat2]; 

to me function looks like, if called it, loop through car array, , if finds said car in array, return index car.

if no car found in array, return -1 meaning car doesn't have index not in array.

so, if have

var lot = [chevy, taxi, fiat1, fiat2]; 

and run

console.log(findcarinlot("fiat1")); // return 2 (third element of array) 

this returned return i inside if statement , never see return -1

but if do

console.log(findcarinlot("lamadamadingdong")); // return -1 never found in array of cars 

hope helps.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -