Reputation:
UPDATE: Thank you very much for advises! Great community! I specified the task.
I need to write a function, which has two arguments: message, text. If message could be created from text letters, function should return true. Otherwise, return false.
compose("ohle", "hello"); //should return true
compose("elooooo", "hello"); // should return false, because there is only one "o" in text "hello".
I've tried this:
function compose(message, text){
for(var i = 0, len = message.length; i < len; i++){
if(text.includes(message[i])){
console.log(true);
} else {
console.log(false);
}
}
}
Actually, I cannot think what shoud I put in the loop besides console.log or return. But then, it consoles log for each letter.
Do You have some insights? Task should be written in javascript ES5. Thanks
Upvotes: 0
Views: 62
Reputation: 1
You can use Array.prototype.every()
and .indexOf()
const compose = (letter, text) => [...text].every(char => letter.indexOf(char) > -1);
console.log(compose("ohle", "hello"));
console.log(compose("elooooo", "hello"));
Upvotes: 1
Reputation: 1370
It doesn't returns 7 true, actually your function logs 7 trues. If you want one result you need:
function compose(letter, text){
for(var i = 0, len = letter.length; i < len; i++){
if(!text.includes(letter[i])){
return false;
}
}
return true;
}
And then you can console.log result of your function
Upvotes: 0