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])
    }
  }
}
3
  • Try using only one loop, and accessing the nth item from each array every time the loop runs. Commented Jul 30, 2020 at 18:06
  • forEach loop through two arrays at the same time in javascript Commented Jul 30, 2020 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. Commented Jul 30, 2020 at 18:10

5 Answers 5

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)

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

1 Comment

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?
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');
}
    
 
    
  

2 Comments

This post could be improved by describing what you changed and why, instead of just posting code.
Thank you, It got posted accidentally while I was still editing. I have added my points :)
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.

3 Comments

I'd argue that abstracting out a lot of those simple functions makes the entire solution harder to follow rather than easier.
Alos OP is a beginner. This will scare them off JS forever
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. Don't allow yourself to become a prisoner of what you know. Keep your mind open to new things. Cheers.
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)

2 Comments

Because OP is a rank beginner. No need to confuse them
totally agreed.
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']

Comments

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.