## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
chnet
@coins4me
hint
CamperBot
@camperbot

hint [4/7]

## Hint: 2

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]

chnet
@coins4me
hint
CamperBot
@camperbot

hint [5/7]

## Hint: 3

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]

chnet
@coins4me
hint
CamperBot
@camperbot

hint [6/7]

type hint for next hint :pencil: [Contribute at the FCC Wiki]

chnet
@coins4me
hint
CamperBot
@camperbot

hint [7/7]

## Code Solution:

function truncate(str, num) {
if (str.length <= num)
return str;

if (num <= 3)
return str.substr(0,num) + "...";

return str.substr(0,num-3) + "...";
}

# Code Explanation:

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) + "...";

# Credits:

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

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Christopher Ngo
@ingochris
hint
CamperBot
@camperbot

hint [1/7]

## ## :construction: After this are possible spoiler hints.

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]

Christopher Ngo
@ingochris
hint
CamperBot
@camperbot

hint [2/7]

## # Explanation:

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]

Christopher Ngo
@ingochris
hint
CamperBot
@camperbot

hint [3/7]

## Hint: 1

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]

Christopher Ngo
@ingochris
hint
CamperBot
@camperbot

hint [4/7]

## Hint: 2

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]

Christopher Ngo
@ingochris
hint
CamperBot
@camperbot

hint [5/7]

## Hint: 3

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]

Christopher Ngo
@ingochris
hint
CamperBot
@camperbot

hint [6/7]

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Christopher Ngo
@ingochris
hint
CamperBot
@camperbot

hint [7/7]

## Code Solution:

function truncate(str, num) {
if (str.length <= num)
return str;

if (num <= 3)
return str.substr(0,num) + "...";

return str.substr(0,num-3) + "...";
}

# Code Explanation:

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) + "...";

# Credits:

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

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Christopher Ngo
@ingochris
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
Xavier Artot
@xavierartot
I have another solution:
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);
Xavier Artot
@xavierartot
find tuncate
CamperBot
@camperbot
find tuncate
nothing found
Emmanuel Evbuomwan
@iamnosa
This message was deleted
This message was deleted
This message was deleted
This message was deleted
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);
Coryphaeus
@cvdeby
help exact change
CamperBot
@camperbot

# Problem Explanation:

• You have to create a program that will handle when the register does not have
enough cash or will have no cash after the transaction. Other than that it
needs to return an array of the change in the form of an array, so that will
be a 2D array.
Coryphaeus
@cvdeby
CamperBot
@camperbot
no wiki entry for: no repeat please
Coryphaeus
@cvdeby
help no repeats
CamperBot
@camperbot

# Problem Explanation:

• This task requires us to look at each possible permutation of a string. This is best done using a recursion function. Being able to build a function which collects all permutations of a string is a common interview question, so there is no shortage of tutorials out there on how to do this, in many different code languages.
• 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;
}
Toby Chow
@TobyChow
help no repeats
CamperBot
@camperbot

## :point_right: algorithm no repeats please [wiki]

### Problem Explanation:

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.

Xavier Artot
@xavierartot
help Roman Numeral Converter
CamperBot
@camperbot
no wiki entry for: roman numeral converter
Xavier Artot
@xavierartot
find Roman Numeral Converter
CamperBot
@camperbot
find roman numeral converter
nothing found
Dawn Swan
@dawn-swan
hint
CamperBot
@camperbot

hint [1/1]

## ## :construction: After this are possible spoiler hints.

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]

Dawn Swan
@dawn-swan
hint
CamperBot
@camperbot
These hints depend on people like you! Please add to this :point_right: Bonfire's Wiki Hints Page
Dawn Swan
@dawn-swan
hint
CamperBot
@camperbot

hint [1/1]

## ## :construction: After this are possible spoiler hints.

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]