Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

renedoukhan
4,761 PointsA JS function to remove a given number of characters from the end of a string.
Hello :) What's wrong with my code ? Can you come up with a more efficient solution ?
var lettreMinus = function () {
var texte = prompt ("Write the text");
var remo = prompt ("Number of letters to be removed");
var remoNumb = remo.parseInt;
var size = ((texte.length)-1);
var res = (size-remoNumb); // calculate the parameter of substr
alert (texte.substr(res));
var encore = confirm ("Once more ?");
if (encore===true) {lettreMinus()};
}
Oh and I don't remember how to format my code properly here :(

Chyno Deluxe
16,936 Points//Fixed Code Presentation
How to post Code
3 Answers

Nicolás Barceló
17,048 PointsHi Rene,
this is a sample function that is doing what you want
function deleteRightChars (input, number) {
// Test if the length of the input string is less than the number
// of chars to delete
if (input.length <= number) { return ""; }
return input.substr(input, input.length - number);
}
Best regards

Mark Casavantes
Courses Plus Student 13,401 PointsRene,
I think you are missing a } at the end of your function and you have a ; at the end of your if statement at the bottom of your page. Look at your spelling of text, you have texte. I think if you fix these items you are good to go.

Chyno Deluxe
16,936 Points2 Things!
var lettreMinus = function () {
var texte = prompt ("Write the text");
var remo = prompt ("Number of letters to be removed");
var remoNumb = parseInt(remo); // 1. should be parseInt(remo)
var size = ((texte.length)-1);
var res = (size-remoNumb); // calculate the parameter of substr
alert (texte.substr(res));
var encore = confirm ("Once more ?");
if (encore===true) {lettreMinus()};
}
// 2. Not sure if you excluded the call to run the function
// but it is missing and should be added
lettreMinus();
Another thing, I'm not sure if this is your intention but your outcome is not as I would thing it should be. Example.
if texte = number and remo = 2 the outcome is "ber" when i would think it would be "mber".
If this is your intention then disregard below.
But if the outcome should be "mber" then you can do the following and remove the size and res variables and merge the parseInt and prompt together. view below.
var lettreMinus = function () {
var texte = prompt ("Write the text");
var remoNumb = parseInt(prompt ("Number of letters to be removed"));
alert(texte.substr(remoNumb));
var encore = confirm ("Once more ?");
if (encore===true) {lettreMinus()};
}
lettreMinus();
I hope this helps
lucybeck
6,055 Pointslucybeck
6,055 PointsHi , Try slice http://www.w3schools.com/jsref/jsref_slice_string.asp :-)