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?
3 odpowiedzi
+ 2
breakfasts[input] = "orange juice";
+ 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 ^^
0
Thanks!