JavaScript Return a string that is all of the words concatenated together return string in c,return string in java,return string

new javascript student here looking for a little clarification. I keep running into problems involving For Loops/Arrays. I'm not familiar enough with the concepts to diagnose my own problems. The online mini course I'm starting out with has limited examples and includes more complex problems on the exercises (which I think is great! I don't mind digging for answers). I try to google for help but can't seem to find anything that explains what I'm doing wrong. I'd really appreciate it if someone could take a look below and shed some light on where and why I'm making mistakes.

Here is the exercise with the instructions commented just below:

function wordsToSentence(words) {

// words is an array of strings // return a string that is all of the words concatenated together // spaces need to be between each word // example: ['Hello', 'world!'] -> 'Hello world!' }

And here is my code that I put in to satisfy the test (which keeps failing):

function wordsToSentence(words) {
for (var i = 0; i < words.length; i++) {
    words[i] + ' ';

}
    return words;
}

More than someone just giving me the correct code, I'd really really appreciate it if someone could break down what I did wrong and how to do it right. Thanks in advance!

Answer:1

you are not actually adding words. First declare an empty string in a variable. This will be your base string, now as you loop over the array keep appending a space and a word to this string. Then return the string and not array. you can use trim() at the end to remove any extra spaces in the end or beginning before returning the string.

function wordsToSentence(words) {
   var sentence = ""; //empty string
   for (var i = 0; i < words.length; i++) {
      sentence += ' ' + words[i]; //add space and word to string
   }
   return sentence.trim(); //return the string formed
}

console.log(wordsToSentence(['this', 'is', 'a', 'sentence']));
Answer:2
function wordsToSentence(words) {
  for (var i = 0; i < words.length; i++) {
    words[i] + ' ';

  }
  return words;
}

This is your code. What you are doing here is going through the array and adding a white space to every word, not even assigning it. Then returning the array. The best solution is (as in the comments):

  function wordsToSentence(words) {
    return words.join(' ');
  }

https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/join

Answer:3

words = ["test", "hello", "world"]


function wordsToSentence(words) {
var stringResult = ""; //create an empty string
for (var i = 0; i < words.length; i++) {
    stringResult += words[i] + " "; //concatenate using +=, retrieving the value using words[i] and append with a space in the end.

}
    return stringResult.trim(); //return the string, with the trim function that strips the last space of it.
}

console.log(wordsToSentence(words));
Answer:4

I am trying to post some data to a php file by using following code. var exam = document.getElementById("information").getAttribute("examid"); var user = document.getElementById("...

I am trying to post some data to a php file by using following code. var exam = document.getElementById("information").getAttribute("examid"); var user = document.getElementById("...

  1. post value in php
  2. post value in pega
  3. post value of checkbox
  4. post value to another page php
  5. post value in ajax
  6. post value in codeigniter
  7. post value in javascript
  8. post value in url
  9. post value of select option
  10. post value of disabled input
  11. post value of unchecked checkbox
  12. post value in jquery ajax
  13. post value to controller ajax
  14. post value in php without form
  15. post value in jquery
  16. post value of button
  17. post value in html
  18. post value in laravel
  19. post value of submit button
  20. post value of checkbox php

I am developing a web page in which a user can use a barcode scanner to scan a QR code. I am using the following code to capture the contents of the QR code: $(document).ready(function () { var ...

I am developing a web page in which a user can use a barcode scanner to scan a QR code. I am using the following code to capture the contents of the QR code: $(document).ready(function () { var ...

I would like to ask a question about event handlers when used in HTML and React. In the book Javascript and Jquery written by Jon Duckett, the author mentioned that using HTML event handler attribute ...

I would like to ask a question about event handlers when used in HTML and React. In the book Javascript and Jquery written by Jon Duckett, the author mentioned that using HTML event handler attribute ...

  1. html event handler attributes
  2. html event handler javascript
  3. html event handler
  4. html event handler return value
  5. html event handler parameters
  6. html event handler list
  7. html event handler arguments
  8. html event handler xss
  9. html event handler dropdown
  10. html event handler input
  11. html event handler remove
  12. html click event handler
  13. html inline event handler
  14. html button event handler
  15. html checkbox event handler
  16. html select event handler
  17. html add event handler
  18. html onclick event handler
  19. html form event handler
  20. html submit event handler

Hello im trying to use Math.min but i am getting NaN as a result, this is the code. method using Math.min method() { var DeltaPrueba = this._markerService.getDeltasRx1(); console.log(DeltaPrueba); ...

Hello im trying to use Math.min but i am getting NaN as a result, this is the code. method using Math.min method() { var DeltaPrueba = this._markerService.getDeltasRx1(); console.log(DeltaPrueba); ...