Small Coding Challenge - Help

Hi guys. I’ve been studying for a couple of week on this website Basic JavaScript and Object Oriented Programming. I’m attempting a coding challenge but it’s getting quite stressful and i can’t solve few functions.

So far i created this by the challenge request:


function createRiver(name, continent, lengthInKilometers) { 

  var object = {  
    "name" : name,
    "continent" : continent,
    "lengthInKilometers" : lengthInKilometers,
    logRiver: function() {
    console.log("The " + this.name + " river is " + this.lengthInKilometers + " kilometers long.");
 }
}
  return object
}

Then i had to push inside an array using the function some values of rivers. Having this:

 var rivers = [];
    
    var amazon = createRiver("Amazon", "South America", 6575);
    var danube = createRiver("Danube", "Europe", 2850);
    var ganges = createRiver("Ganges", "Asia", 2704);
    var mekong = createRiver("Mekong", "Asia", 4350);
    var mississippi = createRiver("Mississippi", "North America", 3730);
    var nile = createRiver("Nile", "Africa", 6650);
    var volga = createRiver("Volga", "Europe", 3530);

 rivers.push(amazon, danube, ganges, mekong, mississippi, nile, volga);

It’s now asking to create 2 functions which i have no idea how to create.

getRiverByName - this function expects a string as an argument and returns the object in the rivers array whose name property is equal to the string that is passed to it (if there is one).

getRiversByContinent - this function expects a string as an argument and returns an array containing the objects in the rivers array whose continent properties are equal to the string that is passed to it.

Everything should be done without using ES6. I hope someone can really help me out!
Thanks in advance.

have you already done this challenge from the curriculum? Profile Lookup

what you need to do is quite similar for the first case

not much different in the second either, but there is the added complexity of the array

I did. I tried to write something like this for the 1st function:

function getRiverByName(name) {
  for (var x = 0; x < rivers.length; x++) {
    if (rivers[x].name === name) {
      if (rivers[x].hasOwnProperty) {
        return rivers[x].name;
      }
    }

But nothing works.

For the 2nd function i really don’t know where to start. Not sure if i need a loop to iterate through the array.

Hi,

Is it necessary to use ES5?

is what you are returning an object?

Hi, yes it’s part of the request. And i still didn’t get through ES6.

also, what you are doing with this? do you need to test if the object contains a property? or you don’t?

because that as it is it is not doing anything at all, rivers[x].hasOwnProperty is truthy because it is a function

I understand what you mean. I’m trying to edit the code but still looks like doesn’t work :frowning:

…and returns the object

So you need return the object what is rivers[x] in your code.

what’s your code now?

1 Like

I wrote something like this and appears to be working:

     for (var x = 0; x < rivers.length; x++) {
         if (rivers[x].name === name) {
             if (rivers[x].hasOwnProperty) {
                 return rivers[x];
             }
         }
     }
 };

Not sure how. Because has you just said .hasOwnProperty is truthy because it is a function.

so it’s like writing

if (true) {
...
}

so that check is useless - why have an useless check?

I just removed that line and yes, it works without :smiley:

For the 2nd function, any help? I’m trying to write a similar code but it returns only the first object and than it stops. I don’t know how to return it into an array.

you need to return an array with all the objects that pass the test, right?
remember that a return statement returns a value and stops the function

this time you want to return an array, right?

Precious help! This appears to be working. :slight_smile:

 function getRiverByContinent(continent) {
    var arr = []
     for (var x = 0; x < rivers.length; x++) {
         if (rivers[x].continent === continent) {
              arr.push(rivers[x]);
             }
     }
     return arr
 };

awesome! congratulations and happy coding

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.