Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Given

  • a demo script in React
  • an array of 6 random numbers.
  • a map.function in the render block that assigns to each item of the array an button

Task

  • Create a function that prints the item (number) from the array when the indexed
    button is clicked
  • When the item is an even number then print a string (title)
  • Else print number


What I have tried:

This is the code I tried so far.

JavaScript
const dummyData = [{title: "Hello"},1,2,4,3,2,5,6];

JavaScript
const evenNum = dummyData
 .filter ( item => item  % 2 === 0);
  console.log("title") 

JavaScript
<button onClick={() => console.log("clicked item mit id: ", index, item.hasOwnProperty("evenNum") ? item.title : dummyData.item)} >

As a result, I only get the number to each button when clicked. Instead of "Hello" it also shows the even numbers.
Posted
Updated 21-Jan-22 6:04am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900