Homework for beginners JS

Hello!

I’m at the beginning in this field, I am signed up in an introductory course and the current theme is to introduce a word in a string.I got stuck and I really don’t understand what to do.
The code must be contained in that form.
I’ll show you where I lose myself.

I’m sure it’s boring for you, but for me it’s hard :slight_smile:
Please help!!!

<body>
/** I have to put  'strawberries' between 'Ana has' and 'apples' */

<button onclick =" writeAtThePosition('Ana has apples and pears', 'strawberries', 6)">Show</button>
<script>
function writeAtThePosition(string,word,number){
for (var i = 0; i < string.lenght(); i++)

}

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

Hy!
Thanks for the answer
I have to introduce a word “strawberries” in a string “Ana has apples and pears” at a certain position, more precisely at position 6.

In the last message I started writing the code but I blocked my self after I write the function
I have already started to learn freecodecamp, only that I have reached only 40% and I have not yet gathered the knowledge necessary to solve this banality.

What have you tried so far? What part are you stuck on? What specific questions do you have?

I tried to create a new variable to include the requested string
which when called in console.log to show the new string.

another thing I don’t realize is how to enter the new word if I do not divide the string into two variables and then add them with the new word

var str1 = "Ana has";
var str2 = "apples and pears";
newString = "str1 "+ "strawberries"+ "str2";

Have you tried evaluating that code and seeing what you get back? I recommend using repl.it for quick tests.

I’ll get you started. Notice how you’ve got quotes around “str1” and “str2” when you shouldn’t. Just edit that code (right in the window below) and hit the green “run” button til you get what you want :checkered_flag:

the question is that I have to think of it in such a way that writing code and using a “for” and an “if” to have the result Ana has “strawberry” apple pears

Honestly, I don’t get the requirements – neither a for-loop nor a condition makes any sense for this problem – and the answer isn’t even understandable English. Are you sure you got the requirements correct?

Edit: Okay, I sort of get how you could do it in a loop by going over every character in the string, but that’s ridiculous. You wouldn’t even do that in C, for dog’s sake. That’s something you’d only do in assembly or forth and probably not even forth.

Hello!

I’m at the beginning in this field, I am signed up in an introductory course and the current theme is to introduce a word in a string.I got stuck and I really don’t understand what to do.
The code must be contained in that form.
I’ll show you where I lose myself.

I’m sure it’s boring for you, but for me it’s hard :slight_smile:
Please help!!!

<body>
/** I have to put  'strawberries' between 'Ana has' and 'apples' */

<button onclick =" writeAtThePosition('Ana has apples and pears', 'strawberries', 6)">Show</button>
<script>
function writeAtThePosition(string,word,number){
for (var i = 0; i < string.lenght(); i++)

}

I’ll be blunt: I think your course is terrible and you should find another.

Hello!

I’m at the beginning in this field, I am signed up in an introductory course and the current theme is to introduce a word in a string.I got stuck and I really don’t understand what to do.
The code must be contained in that form.
I’ll show you where I lose myself.

I’m sure it’s boring for you, but for me it’s hard :slight_smile:
Please help!!!

<body>
/** I have to put  'strawberries' between 'Ana has' and 'apples' */

<button onclick =" writeAtThePosition('Ana has apples and pears', 'strawberries', 6)">Show</button>
<script>
function writeAtThePosition(string,word,number){
for (var i = 0; i < string.lenght(); i++)

}

hello!
sorry, I didn’t do it intentionally
only that the first relay was not loaded and I thought it was an error or something like that, sorry

Hello
You may be right, but I have to do it anyway,
as I explained, above.
I’m a beginner, but to get stuck in the 2nd lesson is more ridiculous than the lesson itself
If you can explain it to me, it would be ok if not it is not a problem.
thanks

do you have any obligations to do that course in particular?

if you don’t have I highly suggest you do the freecodecamp course, it doesn’t have weird things like that.


So, the starting code for your challenge is the function with the loop? Please confirm or deny this.

What have you learned so far? just loops and string concatenation?
because if you don’t know anything else it seems a bit… weird to do like this. Not impossible, but weird.

Hello
It might sound weird to have a homework like this since it’s an introductory course for web development
It’s a paid course, so yes I have to finish it :slight_smile:
I’m already learning freecodecamp but I also need someone to explain live and to show.
Yes the function writeAtThePosition is what I need to do.
I need to insert strawberries in that phrase at position 6
continuing that code

I am not going to give you the solution, but here some hints:

while you loop over the string, you check when you have reached the correct index, and do something

strings are immutable, you can’t change directly the given string, you need to create a new string - also you can’t change the string you are looping over because it will throw up the loop chasing the length of the string so don’t use the same variable for the new string

Glad to help, just do like this.No need looping.

var str1 = "Ana has";

var str2 = "apples and pears";

newString = str1 + "  " + "strawberries" + "  " + str2;

Hello !
If you do not remember the Homework, it was to insert the word strawberries in that string "ana has apples " at the position 6.
Can I have others hints here, please?
Why newstring does not appear in the console?

         <button onclick = "writeAtThePosition ('ana has apples', 'strawberries', 6)"> Calculate 
 </button>
     <Script>
         function writeAtThePosition (string, word, number) {
         var writeAtThePosition = string, word, number;
         var newString = " ";
         for (var i = 0; i <= string; i ++) {
             if (i === string.lenght) {
                 newString = string [i] + word + string.lenght;
             }
             }

             console.log (newstring);
]

what do you think this line is doing?

what do you think this is doing?

it’s length