Any tip to display everything on the same line


#1


Hi everyone, i'm actually not really stuck to an exercise, i just wanted to learn new things. In the 5th exercise of the "loops" chapter in JavaScript, i wanted to go a bit further than asked, as you will be able to see with my code. I just wanted to know if there's any mean to display everything on the same line ? And if there is anything you can give me any tip to improve my code, no problem ! :slight_smile:


The output is like this

'You should visit
Cairo
, Roma
, Chicago
or
Sydney
together !'
Is there any mean to print it like this ?

'You should visit Cairo, Roma, Chicago or Sydney together !'

var myPlaces = ['Dubai', 'Cairo', 'Athens', 'London', 'Roma', 'Chicago', 'Sydney'];
var friendPlaces = ['New York', 'Cairo', 'Roma', 'Sydney', 'Toronto', 'Berlin', 'Paris', 'Chicago'];
var commonPlaces = [];

for (var i = 0 ; i < myPlaces.length ; i++){
  for (var j = 0 ; j < friendPlaces.length ; j++) {
    if (myPlaces[i] === friendPlaces[j]) {
      commonPlaces.push(myPlaces[i]);
    }
	}
}

if (commonPlaces.length === 0){
  console.log('There\'s no place you could visit together :\'(');
}
else {
  console.log ('You should visit ');
  for (var i = 0 ; i < commonPlaces.length ; i++){
    switch (i){
      case 0 :
        console.log(commonPlaces[i]);
        break;
      case commonPlaces.length - 1 :
        console.log(' or ' + commonPlaces[i]);
        break;
      default :
        console.log(', ' + commonPlaces[i]);
        break;
    }
  }
  console.log(' together !');
}


#2

console.log('You should visit Cairo, Roma, Chicago or Sydney together !');

#3

Very funny :smiley:
Any useful answer ? xD


#4

Problem: calling console.log prints unwanted newlines
Solution: call it once instead, with the text you want printed on a line


#5

var myPlaces = ['Dubai', 'Cairo', 'Athens', 'London', 'Roma', 'Chicago', 'Sydney'];
var friendPlaces = ['New York', 'Cairo', 'Roma', 'Sydney', 'Toronto', 'Berlin', 'Paris', 'Chicago'];
var commonPlaces = [];

for (var i = 0; i < myPlaces.length; i++) {
  for (var j = 0; j < friendPlaces.length; j++) {
    if (myPlaces[i] === friendPlaces[j]) {
      commonPlaces.push(myPlaces[i]);
    }
  }
}

function abc(commonPlaces) {
  if (commonPlaces.length === 0) {
    console.log('There\'s no place you could visit together :\'(');
  } else {
    var output = 'You should visit '; //Create a string variable that will store the sentence.
    for (var i = 0; i < commonPlaces.length; i++) {
      switch (i) {
        case 0:
          output += commonPlaces[i]; //Use the original "output" variable and add to it the first country.
          break;
        case commonPlaces.length - 1:
          output += ' or ' + commonPlaces[i];
          break;
        default:
          output += ', ' + commonPlaces[i];
          break;
      }
    }
    output = output + ' together!'; //Finally redefine "output" by adding at the end of it 'together!'.
    console.log(output); //Print your sentence
  }
}

abc(commonPlaces); //Function call

I hope it helps!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.