0

Struggling with this question, "Loop through the arrays and push a string with the format "My name is [firstName] [lastName] and I am from [place]" into the array holding the respective bios."

Here is what i have tried so far but its not running properly. Please help!

const firstNames = ["Jon", "Arya", "Jamie"];
const lastNames = ["Snow", "Stark", "Lannister"];
const places = ["The Wall", "Winterfell", "Kings Landing"];
const bios = [];

for (let i =0; i<firstNames.length; i++);{
  for (let i =0; i<lastNames.length; i++);{
    for (let i =0; i<places.length; i++);{
      bios.push('My name is ' + firstNames[i] + lastNames[i] + ' and i am from ' + places[i])
      console.log(bios[i])
    }
  }
}
Omkar Kulkarni
  • 1,091
  • 10
  • 22
  • Try using only one loop, and accessing the nth item from each array every time the loop runs. – Sean Jul 30 '20 at 18:06
  • [forEach loop through two arrays at the same time in javascript](https://stackoverflow.com/questions/57903061) – adiga Jul 30 '20 at 18:07
  • 1
    Each loop runs independently after the previous one. So your i variable is set to 0, 1, 2, 0, 1, 2, 0, 1, 2. When the last loop executes, it looks up the things at index 2 of your arrays and outputs that. You need to execute your bios.push code inside a loop. – Charlie Bamford Jul 30 '20 at 18:10

5 Answers5

1

Almost

You have too many loops and too many semicolons and one too many [i]

this is assuming the same number of items in each array

const firstNames = ["Jon", "Arya", "Jamie"];
const lastNames = ["Snow", "Stark", "Lannister"];
const places = ["The Wall", "Winterfell", "Kings Landing"];
const bios = [];

for (let i = 0; i < firstNames.length; i++) {
  bios.push(i+'. My name is ' + firstNames[i] + ' ' + lastNames[i] + ' and I am from ' + places[i])
}
console.log("length:",bios.length);
console.log(bios)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • The problem needs me to have "bios" contain Jon Snow's bio at index 0, Arya Stark's bio at index 1, and Jamie Lannister's bio at index 2. Also bios should be of length 3. Any suggestions? – Solomiya Romanyshyn Jul 30 '20 at 19:23
1

Try running the code snippet.

If fistName,lastname and places are gonna be the same length or unless there is a need to handle the different length arrays, you don't need to loop three times. Looping on any one of the arrays would do. Second, bios.push needs to be inside the for loop.

const firstNames = ["Jon", "Arya", "Jamie"];
const lastNames = ["Snow", "Stark", "Lannister"];
const places = ["The Wall", "Winterfell", "Kings Landing"];
const bios = [];

if (firstNames.length === lastNames.length && lastNames.length=== places.length){
   for (var i =0; i<firstNames.length; i++){
      bios.push(`My name is ${firstNames[i]} ${lastNames[i]} and I'm from ${places[i]}`)
      console.log(bios[i])
   }
} else {
   console.log('length mismatch across the 3 arrays');
}
    
 
    
  
Zack S
  • 1,412
  • 1
  • 23
  • 37
Riddhijain
  • 487
  • 2
  • 8
0

Here's how we might approach the problem using functional programming.

Our first module will be the Person module. First we create the ability to construct -

  • a person, from a first, last, and place
  • a greeting string, using a person as input
// Person.js

const person = (first, last, place) =>
  ({ first, last, place }) // construct a person
  
const greeting = (p = {}) =>
  `hello my name is ${p.first} ${p.last}, i am from ${p.place}.` // construct greeting

export { person, greeting }

Now let's begin our Main module; the program to run. Along the way we imagine an new module, Arr, as a better version of JavaScript's Array -

// Main.js

import { person, greeting } from "./Person"
import { map } from "./Arr"

const firstNames = // [ ... ]
const lastNames = // [ ... ]
const places = // [ ... ]

const people =
  map(person, firstNames, lastNames, places)  // TODO: implement map
  
for (const p of people)          // for each person, p,
  console.log(greeting(p))       // display greeting for p
  
// hello my name is Jon Snow, i am from The Wall.
// hello my name is Arya Stark, i am from Winterfell.
// hello my name is Jamie Lannister, i am from Kings Landing.

Finally implement the Arr module -

// Arr.js

const isEmpty = (t = []) =>
  t.length === 0
  
const first = (t = []) =>
  t[0]
  
const rest = (t = []) =>
  t.slice(1)

const map = (f, ...ts) =>
  ts.some(isEmpty)
    ? []
    : [ f(...ts.map(first))
      , ...map(f, ...ts.map(rest))
      ]

export { isEmpty, first, rest, map }

Everything is done! Expand the snippet below to verify the result in your browser -

const isEmpty = (t = []) =>
  t.length === 0
  
const first = (t = []) =>
  t[0]
  
const rest = (t = []) =>
  t.slice(1)

const map = (f, ...ts) =>
  ts.some(isEmpty)
    ? []
    : [ f(...ts.map(first))
      , ...map(f, ...ts.map(rest))
      ]
      
const person = (first, last, place) =>
  ({ first, last, place })
  
const greeting = (p = {}) =>
  `hello my name is ${p.first} ${p.last}, i am from ${p.place}.`
  
const firstNames =
  ["Jon", "Arya", "Jamie"]

const lastNames =
  ["Snow", "Stark", "Lannister"]

const places =
  ["The Wall", "Winterfell", "Kings Landing"]

const people =
  map(person, firstNames, lastNames, places)
  
for (const p of people)
  console.log(greeting(p))
hello my name is Jon Snow, i am from The Wall.
hello my name is Arya Stark, i am from Winterfell.
hello my name is Jamie Lannister, i am from Kings Landing.
Mulan
  • 129,518
  • 31
  • 228
  • 259
  • 2
    I'd argue that abstracting out a lot of those simple functions makes the entire solution harder to follow rather than easier. – Sean Jul 30 '20 at 20:07
  • 1
    Alos OP is a beginner. This will scare them off JS forever – mplungjan Jul 30 '20 at 20:40
  • It's funny how imperative-style programmers don't see side effects, intermediate state, and juggling countless local variables as _scary_. Functional style is only "scary" to you if you're not familiar with the concepts and techniques. JavaScript is a multi-paradigm language and limiting yourself to programming in C-style or Java-style means you will miss out on a lot [this little language has to offer](https://stackoverflow.com/a/46918344/633183). Don't allow yourself to become a prisoner of what you know. Keep your mind open to new things. Cheers. – Mulan Jul 31 '20 at 18:58
0

Why not reduce and deconstruct? :)

const firstNames = ["Jon", "Arya", "Jamie"];
const lastNames = ["Snow", "Stark", "Lannister"];
const places = ["The Wall", "Winterfell", "Kings Landing"];

const reduced = [firstNames, lastNames, places].reduce((r, a) => a.map((v, i) => (r[i] || []).concat(v)), []);

const bios = reduced.map(
  ([f, l, p]) => `My name is ${f} ${l} and I am from ${p}`
);

console.log(bios)
Sharan Arumugam
  • 353
  • 6
  • 12
0

// ADD CODE HERE

for (let i = 0; i < firstNames.length; i++) {
  bios.push('My name is ' + firstNames[i] + ' ' + lastNames[i] + ' and I am from ' + places[i])
}
console.log(bios)

// => ['My name is Jon Snow and I am from The Wall', 'My name is Arya Stark and I am from Winterfell', 'My name is Jamie Lannister and I am from Kings Landing']
Ran Marciano
  • 1,431
  • 5
  • 13
  • 30