hint [4/7]
You will need to use slice and specify where to start and where to stop.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [5/7]
Do not forget that when we truncate the word, we also must count the length added by ...
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [6/7]
Solution ahead!
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [7/7]
function truncate(str, num) {
if (str.length <= num)
return str;
if (num <= 3)
return str.substr(0,num) + "...";
return str.substr(0,num-3) + "...";
}
First we need an if-statement to test if the length of the full string passed in as the first argument already fits within the size limit passed in as the second argument. If so we can just return the string that was passed in.
if (str.length <= num)
return str;
Then we need to check if the num fits into our special case, where the desired length is less than or equal to three. In this case we will return the string trimmed to our target length, followed by "..."
. We don't need to worry about the "..."
being counted against the length of our output string in this case.
if (num <= 3)
return str.substr(0,num) + "...";
Finally, we write the return for what happens when neither of the previous if-statements have been true. This call to substr()
gives us a string that is 3 less than the target length specified by num
, which is done so that we have room to add on the "..."
and fit within that target.
return str.substr(0,num-3) + "...";
If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 @richyvk @ltegman for your help with Bonfire: Truncate a String
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [1/7]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [2/7]
We need to reduce the length of the string or truncate it if it is longer than the given maximum lengths specified and add ...
to the end. If it is not that long then we keep it as is.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [3/7]
Strings are immutable in JavaScript so we will need a new variable to store the truncated string.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [4/7]
You will need to use slice and specify where to start and where to stop.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [5/7]
Do not forget that when we truncate the word, we also must count the length added by ...
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [6/7]
Solution ahead!
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [7/7]
function truncate(str, num) {
if (str.length <= num)
return str;
if (num <= 3)
return str.substr(0,num) + "...";
return str.substr(0,num-3) + "...";
}
First we need an if-statement to test if the length of the full string passed in as the first argument already fits within the size limit passed in as the second argument. If so we can just return the string that was passed in.
if (str.length <= num)
return str;
Then we need to check if the num fits into our special case, where the desired length is less than or equal to three. In this case we will return the string trimmed to our target length, followed by "..."
. We don't need to worry about the "..."
being counted against the length of our output string in this case.
if (num <= 3)
return str.substr(0,num) + "...";
Finally, we write the return for what happens when neither of the previous if-statements have been true. This call to substr()
gives us a string that is 3 less than the target length specified by num
, which is done so that we have room to add on the "..."
and fit within that target.
return str.substr(0,num-3) + "...";
If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 @richyvk @ltegman for your help with Bonfire: Truncate a String
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
function truncate(str, num) {
// Clear out that junk in your trunk
str=str.split(""); //.splice(0,num).join("");
if (num>3){
return str.splice(0,num-3).join("")+"...";
}
else return str.splice(0,num).join("")+"...";
}
truncate("A-tisket a-tasket A green and yellow basket", 2);
Why ain't this case working:truncate("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2) should return "A-tisket a-tasket A green and yellow basket".
a
function truncate(str, num) {
// Clear out that junk in your trunk
var etc=3, dots='...';
if (num === 1) {
console.log(str = str.slice(0, 1) + dots);
} else if(num > 1){
if (num === 2) {
console.log(str = str.slice(0,2) + dots);
}else if (num <= 14){
console.log(str = str.slice(0, num - 3 ) + dots);
}else{
console.log(str = str );
}
}
return str;
}
truncate("A-tisket a-tasket A green and yellow basket", 11);
function chunk(arr, size) {
// Break it up.
var iamnosa = [];
while (arr.length) {
iamnosa.push(arr.splice(0, size));
}
return iamnosa;
}
chunk(["a", "b", "c", "d"], 2);
This task can still be daunting even after watching a tutorial. You will want to send each new use of the function 3 inputs: 1. a new string (or character array) that is being built, 2. a position in your new string that's going to be filled next, and 3. an idea of what characters (more specifically positions) from the original string have yet to be used. The pseudo code will look something like this:
```
var str = ???;
perm(current position in original string, what's been used in original string, current string build thus far){
if(current string is finished) {
print current string;
}else{
for(var i = 0; i < str.length; i++) {
if(str[i] has not been used) {
put str[i] into the current position;
mark str[i] as used
perm(current position in original string, what's been used in original string, current string build thus far)
unmark str[i] as used because another branch in the tree for i + 1 will still likely use it;
}
}
}
}
:pencil: read more about algorithm no repeats please on the FCC Wiki
This task requires us to look at each possible permutation of a string. This can be done using a recursion function, but it is not the only way. A common interview question is building a function which collects all permutations of a string. So there is no shortage of tutorials out there on how to do this, in many different code languages.
:pencil: read more about algorithm no repeats please on the FCC Wiki
hint [1/1]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [1/1]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]