-2

I wrote this code but it seems to keep printing out seal onto the console? Is there another ways to print the correct answer using arrays? //i.stack.imgur.com/i8D8q.png

  • Hello. If you could please post the code here instead of a photo of the code that would be very helpful as it would let us debug/edit/format the code as needed, and also means if the link above ever goes invalid people will always be able to understand this question. Thanks – Matthew Herbst Jul 19 '22 at 03:51
  • There's no code showing how the values are updated/changed so the code you've written will always return the last element in the array given that they are all equal. You would have to update the item in the array, not the original variable. You could try setting it up using an `Object` as it seems like it would make more sense here. – Rylee Jul 19 '22 at 03:54
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jul 19 '22 at 04:12

1 Answers1

0

var items = "a=65/b=923/c=76/d=896/e=1/f=64/g=945/h=203/i=66";

function proto (thisone, order) {
proto[order] = thisone;

}
function proto2(thisone) {
proto2[thisone.split("=")[0]] = thisone.split("=")[1];

}
 function animalMax (value, order2) {
 
 if (!proto2[value.split("=")[0]]) {
 proto(value, order2);
} else {
    proto("null=-1", order2);
}
for (var y = 0; y < sp.length; y++) {

if (sp[y].split("=")[1] > Number(proto[order2].split("=")[1]) && !proto2[sp[y].split("=")[0]]) {
proto(sp[y], order2);
}
}
}
var sp = items.split("/");

for (var i = 0; i < sp.length; i++) {

animalMax(sp[i], i);
proto2(proto[i]);
}
var final = "";
for (var p = 0; p < sp.length; p++) {
final += proto[p];
}

console.log(final);
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – Walter Laurent Jul 19 '22 at 06:12
  • It would make your answer more valuable if you could add an explanation of what the problem with the given code was and how your code solves it. Thanks. – A Haworth Jul 19 '22 at 06:32
  • it's an other way to make protoype.sort ? or i don't anderstood correcly sorry, split is an other way but you have more possibility sure – Walter Laurent Jul 19 '22 at 06:50
  • outch sorry i'have edited in the firt place i haven forget the "number" because the split transporm in string... it's ok now – Walter Laurent Jul 19 '22 at 07:39
  • https://stackoverflow.com/questions/73046756/sort-a-dictionary-alphabetically-by-value-in-javascript/73047320#73047320 – Walter Laurent Jul 20 '22 at 07:14