1

when I console.log it display all the arrays to be the string, because in my arrays, they are multi-dimensions; look below the code using console.log in function, without return but undefined

function changeStrings(arr, replacement) {
    var newArr = []

  for ( var i = 0 ; i < arr.length ; i++) {
    newArr.push(arr[i].split(" "))
  }

  for ( var j = 0 ; j < replacement.length ; j++ ) {
    newArr[0][3] = replacement[j-2]
    newArr[1][3] = replacement[j-1]
    newArr[2][4] = replacement[j]
  }
   
  for ( var k = 0 ; k < newArr.length ; k++ ) {
    console.log(newArr[k].join(" "))
  }
 
};


let initial = ["my city in London", "my name is Mike", "my phone number is 00909090"];
let replacements = ['Paris', 'John', '1234'];

console.log(changeStrings(initial, replacements))

and if i using return , it just print one line;

check this

function changeStrings(arr, replacement) {
    var newArr = []

  for ( var i = 0 ; i < arr.length ; i++) {
    newArr.push(arr[i].split(" "))
  }

  for ( var j = 0 ; j < replacement.length ; j++ ) {
    newArr[0][3] = replacement[j-2]
    newArr[1][3] = replacement[j-1]
    newArr[2][4] = replacement[j]
  }
   
  for ( var k = 0 ; k < newArr.length ; k++ ) {
    var dispplay = newArr[k].join(" ")
    return dispplay
  }
 
};

// now let's test out our functions!
let initial = ["my city in London", "my name is Mike", "my phone number is 00909090"];
let replacements = ['Paris', 'John', '1234'];

console.log(changeStrings(initial, replacements))

the newArr variable is array multidimension it looks like this ;

[ [ 'my', 'city', 'in', 'Paris' ],
  [ 'my', 'name', 'is', 'John' ],
  [ 'my', 'phone', 'number', 'is', '1234' ] ]

i am trying to using for loop for this case, because i want to train my logic;), can anyone help me to find out what's wrong ? or fix this ? thank you

1
  • The problem is that returnstatement end the whole loop (same as 'break'). You can not return directly like that. Commented Nov 4, 2018 at 10:01

2 Answers 2

1

The function is returning just after the first iteration of the loop. That prevents the rest of the iterations from execution. You have to return an array from the function. You can push() all the item to the array inside the loop then return the array:

function changeStrings(arr, replacement) {
  var newArr = []

  for ( var i = 0 ; i < arr.length ; i++) {
    newArr.push(arr[i].split(" "))
  }

  for ( var j = 0 ; j < replacement.length ; j++ ) {
    newArr[0][3] = replacement[j-2]
    newArr[1][3] = replacement[j-1]
    newArr[2][4] = replacement[j]
  }
  var display = [];
  for ( var k = 0 ; k < newArr.length ; k++ ) {
    display.push(newArr[k].join(" "));
  }
 
  return display
};

// now let's test out our functions!
let initial = ["my city in London", "my name is Mike", "my phone number is 00909090"];
let replacements = ['Paris', 'John', '1234'];

console.log(changeStrings(initial, replacements))

Sign up to request clarification or add additional context in comments.

3 Comments

so we should loop again to print those arrays? :D @Mamun
@ZrClassic, yes, since the returned result is an array, you have to loop to print those.....thanks
@ZrClassic, you can also join the array to return string like return display.join('.....') if you want.
0

The problem is that return statement end the whole loop immediately (same as break) . You can not return directly like that, if you want to finish the loop. On the other way, you can use it purposefuly to end the loop, for instance if you found a desired item and don't neet to loop anymore.

How to actually use return in loop:

let fruit = ['apple', 'orange', 'pear', 'strawbery'];
let myFavoriteFruit === 'orange';

const IncludesMyFavouriteFruit = (list) => {
    for (let item of list) {
        if (item === myFavoriteFruit) return true;  // Ends the loop; we'll never get to 'pear' and 'strawbery'.
    }
    return false; // There is not my favourite fruit.
}

Sorry for using ES6, too lazy to write old JS.

1 Comment

can u give example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.