Help a newbie! create a list from an array function EASY

Hi,

I’ve been doing online tutorials e.g MDN, JS hero, Sololearn & completed the first two sections of freecodecamp… but I’ve not really done any challenges and put what ive learnt into action from scratch.

I’ve started doing some problems today and feel like I’ve not learnt anything!

I’ve got visual code editor & code runner.

My first challenge is to list everything in an array.

so

function animals(arr){
var animalString = "";
for (var i = 0; i < arr. length; i++) {

    return animalString += arr[i];

}
     }
console.log(animals(["cat","dog","human","whale","seal"]));

I’d like to return
cat
dog
human
whale.

Or: cat,dog,human,whale.

What I don’t understand is how to write:

let cars = ["Tesla", "Ferrari", "Lamborghini", "Audi"];

for(let i = 0; i < cars.length; i++) {
  console.log(cars[i]);
}

As a function that will be displayed in the VC studio.

But right now all i get is cat…

this is part of a bigger challange:
Write a function that accepts an array of strings. Return the longest string.

I have looked at the solution and can see it is:

function longestString1(arr) {
	var longest = ''; // Step 0
	for (var i = 0; i < arr.length; i++) { // Step 1
		if (arr[i].length > longest.length) { // Step 2 & 3
			longest = arr[i]; // Step 4
		}
	}
	return longest; // Step 5

I just cant understand how arr[i] is stored/ listed to then work compare it.
Thats why im trying to figure out how to just make an array from the list.

God! Coding its like 1 step forward, 2 steps back!!

Hope you can help!

Also, does anyone ever set up a buddy system for low cost / free help!

Thanks x

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Remember that return is the keyword to exit a function. Once you hit a return, no further code is executed.

Thanks…

I still don’t get it :frowning:

how do I get the whole list to show rather than just one element?

Thanks

function animals(arr){
var animalString = "";
for (var i = 0; i < arr. length; i++) {

let myAnimals = animalString += arr[i] + ",";
return myAnimals;

}
}
console.log(animals(["cat","dog","human","whale","seal"]));

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

You have a return inside a for loop. That means that your code stops running as soon as it looks at the first element in your array.

1 Like

Thank you so much!
So simple but only when you know the answers!!

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