Change index in array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Change index in array

I have an array I.e. var breakfasts = [“eggs”, “ham” , “toast”]; I want the user to input me a number and I will output the new array, changing the position of the number the user inputs with orange juice Var input = parseInt (input()); I.e. Input: 2 Output: [“eggs”, “ham”, “orange juice”] How can I code that to happen?

11th Jun 2021, 2:18 AM
Daniel Lopez
3 Answers
+ 2
breakfasts[input] = "orange juice";
11th Jun 2021, 2:24 AM
Divya Mohan
Divya Mohan - avatar
+ 2
you must specify if the user enter the nth position (1 based) or the index position (0 based)... in first case you must do: breakfasts[input-1] = "orange juice"; but if the function to take 'input' variable from user is named too 'input', you will get an error ^^
11th Jun 2021, 2:40 AM
visph
visph - avatar
0
Thanks!
13th Jun 2021, 3:20 AM
Daniel Lopez