Reputation: 1
I'm very new in JavaScript and dont understand very well the definitions needed to a function work.
var ShowWords = ['Olá,','sabia','que', 'ta', 'tudo', 'bem?']
for(var i=0; i<ShowWords.length; i++){
console.log(ShowWords[i]);
}
And i dont get it why the code above works and the below doesn't, it only gives me "undefined"
function ShowWords(pal1,pal2,pal3,pal4,pal5,pal6){
for(var i=0; i<ShowWords.length; i++){
return ShowWords[i]
}
}
console.log(ShowWords['Olá,','sabia','que', 'ta', 'tudo', 'bem?']);
Anyone can help?
Upvotes: 0
Views: 1544
Reputation: 4877
You have the wrong syntax for your function invocation. It needs to be function(args)
, not function[args]
.
This will work:
function ShowWords(words){
for(var i=0; i< words.length; i++){
return ShowWords[i]
}
}
console.log(ShowWords('Olá,','sabia','que', 'ta', 'tudo', 'bem?'));
But what you want to do, probably, is this:
function ShowWords(words){
for(var i=0; i< words.length; i++){
return ShowWords[i]
}
}
console.log(ShowWords(['Olá,','sabia','que', 'ta', 'tudo', 'bem?']));
But you really want to do this:
function ShowWords(words){
words.forEach(console.log)
}
ShowWords(['Olá,','sabia','que', 'ta', 'tudo', 'bem?']);
or this:
function makeListFromArray(words) {
return words.join(', ')
}
const list = makeListFromArray(['Olá,','sabia','que', 'ta', 'tudo', 'bem?']);
console.log(list);
Then you want to deal with the case where there is nothing passed in, or a string got passed in, and not throw an error:
function makeListFromArray(words = []) {
return Array.isArray(words) ? words.join(', ') : words;
}
const list = makeListFromArray(['Olá,','sabia','que', 'ta', 'tudo', 'bem?']);
console.log(list);
const str = 'Olá'
console.log(makeListFromArray(str)) // prints Olá
And then you can go leet and make it like this:
const makeListFromArray = (words = []) =>
Array.isArray(words) ? words.join(', ') : words;
const list = makeListFromArray(['Olá,','sabia','que', 'ta', 'tudo', 'bem?']);
console.log(list);
Those are a few ways to accomplish it. Don't use loops, array methods are more powerful, and composable.
And then, there was TypeScript...
const makeListFromArray = (words: string[] = []) => words.join(', ')
const list = makeListFromArray(['Olá,','sabia','que', 'ta', 'tudo', 'bem?']);
console.log(list);
With that single type annotation on (words: string[] = [])
, TypeScript will now refuse to build code that tries to pass anything other than an array of strings (or an empty array) to makeListFromArray
.
It also knows that list is a string, because it knows that Array.join
returns a string.
Since they are all const
it knows that the value and type of list cannot change anywhere in the code, so it knows that list
is a string everywhere.
Upvotes: 3
Reputation: 5148
You have multiple solution to do this. Others answers just give you the basic old way to do it with an array and a for loop (and this is valid!) but I want to give you others solutions:
Using One string/args and use spread operator to keep then all:
const convertToStr = (...args) => {
// args is an array containing all args.
};
Usage:
convertToStr("My", "First", "String");
Or
const words = ["My", "first", "string"];
convertToStr(...words); // << Deconstruct the array to give one element / args
Note that both solution give you the exact same result.
The content of the function:
If you have an array of string and want to concat them together you should use Array.join()
const convertToStr = (...args) => {
const allStrings = args.join(" "); // < Space with " ", put "" for nothing,
console.log(allStrings);
}
Upvotes: 1
Reputation: 9
First of all, use parens instead of brackets to call your function.
If you want to iterate all your arguments, you should use the spread operator (...) to gather all of them in an array, like this:
function ShowWords(...args) {
for (let i = 0; i < args.length; i++){
console.log(args[i]);
}
}
also, var
is usualy kind of deprecated in JavaScript, I suggest you to use the let
keyword instead.
If you only want to join all your arguments into a string, the join method seems to be the simpliest way:
function ShowWords(...args){
return args.join(" ");
}
console.log(ShowWords('Olá', 'sabia','que', 'ta', 'tudo', 'bem?'));
But if all you need is to display words in the console, you must put the console.log function directly inside your loop and don’t use return which breaks the loop and exits the function. for ... of
loops or forEach
array method are a good way to do that:
function ShowWords(...args){
args.forEach(word => console.log(word));
}
ShowWords('Olá', 'sabia','que', 'ta', 'tudo', 'bem?');
function ShowWords(...args){
for (const word of args) {
console.log(word);
}
}
ShowWords('Olá', 'sabia','que', 'ta', 'tudo', 'bem?');
Upvotes: 1
Reputation: 50291
The second code does not work for multiple reasons. First use unique name for function and variable. Secondly if you are passing an array , the function is expected to accept sing argument only. Thirdly you are returning from the for
statement , the function will return after processing first element
var ShowWords = ['Olá,', 'sabia', 'que', 'ta', 'tudo', 'bem?']
function convertToStr(arr) {
let str = '';
for (var i = 0; i < arr.length; i++) {
str += arr[i] + ' ';
}
return str.trim();
}
console.log(convertToStr(ShowWords))
Upvotes: 0