Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can you translate this to javascript please. import java.util.Scanner; import ja

ID: 3834596 • Letter: C

Question

 can you translate this to javascript please.   import java.util.Scanner; import java.util.ArrayList; import java.io.*;  class NameRecord {     private String _name;   // The name being considered     private int[] _rankings;// Its rankings in the 11 censuses              public NameRecord() {}      public NameRecord(String name, int[] ranks) {         _name = name;         _rankings = ranks;     }      public String getName() { return _name; }      public int[] getRankings() { return _rankings; }      public int getRank(int rank) {// use 0 for 1990, 1 for 1910, ..         return _rankings[rank];     }      /** Return year of best ranking with 0 for 1900, .. */     public int bestYear() { // returns the year with best rank         int best = 0;         int value = Integer.MAX_VALUE;         for (int k = 0; k < _rankings.length; k++) {             int r = _rankings[k];             if (r > 0 && r < value ) {                 best = k;                 value = r;             }         }         return best;     }      /** Returns an array with the NameRecord of each name in the file          filename     */     public static NameRecord[] getAllNameRecords(String filename) {         ArrayList<NameRecord> nrs = new ArrayList<NameRecord>();         NameRecord[] r = null;         Scanner fin = null;         try {             fin = new Scanner(new File(filename));             while (fin.hasNextLine()) {                 String line = fin.nextLine();                 Scanner scan = new Scanner(line);                 String name = scan.next();                 int[] ranks = new int[11];                 for (int k = 0; k < 11; k++)                     ranks[k] = scan.nextInt();                 nrs.add(new NameRecord(name, ranks));             }             r = new NameRecord[nrs.size()];             nrs.toArray(r);         } catch (FileNotFoundException e) {             e.printStackTrace();         } finally {             if (fin != null) fin.close();         }         return r;     } }    public class Lab12s09 {     private static NameRecord[] records;      public static int binary(String name) {         int left = 0;         int right = records.length-1;         while (left <= right) {             int middle = (left+right)/2;             int c =          name.compareToIgnoreCase(records[middle].getName());             if (c < 0)                  right = middle - 1;             else if (c > 0)                 left = middle + 1;             else                 return middle;         }         return -1;     }      /**  Display yearly performance for name*/     public static void displayName(String name) {         int where = binary(name);         if (where < 0) {             System.out.println("Sorry, " + name + " is not ranked");             return;         }         System.out.println(name.toUpperCase());         int[] ranks = records[where].getRankings();         int date = 1900;         for (int k = 0; k < 11; k++) {             int r = ranks[k];             System.out.print(date + ": ");             if (r > 0) {                 double v = (int)((1000-r)*6.0/100);                 for (int j = 1; j <= v; j++)                     System.out.print('x');             }             System.out.println("  " + r);             date += 10;         }     }      /** Given the string which, it prints all the names where it occurs          and for each indicates the census when it did best     */     public static void displayNames(String which) {         which = which.toUpperCase();         for (NameRecord x: records) {             String name = x.getName().toUpperCase();             if (name.indexOf(which) >= 0) {                 int year = x.bestYear();                 System.out.println(name.toUpperCase() + "  " +                         (1900+10*year));              }         }     }       public static void main(String[] args) {         String filename = "names-data.txt";         records = NameRecord.getAllNameRecords(filename);         Scanner scan = new Scanner(System.in);         // Ask for names and show their record         while (true) {             System.out.print("Enter name to search: ");             String name = scan.nextLine().trim();             if (name.length() == 0) break;             displayName(name);         }         // Ask for strings and show all names with that string         while (true) {             System.out.print("Enter string to search for: ");             String s = scan.nextLine().trim();             if (s.length() == 0) break;             displayNames(s);         }     } } 

Explanation / Answer

import java.util.Scanner; import java.util.ArrayList; import java.io.*; class NameRecord { private String _name; // The name being considered private int[] _rankings;// Its rankings in the 11 censuses public NameRecord() {} public NameRecord(String name, int[] ranks) { _name = name; _rankings = ranks; } public String getName() { return _name; } public int[] getRankings() { return _rankings; } public int getRank(int rank) {// use 0 for 1990, 1 for 1910, .. return _rankings[rank]; } /** Return year of best ranking with 0 for 1900, .. */ public int bestYear() { // returns the year with best rank int best = 0; int value = Integer.MAX_VALUE; for (int k = 0; k < _rankings.length; k++) { int r = _rankings[k]; if (r > 0 && r 0) { double v = (int)((1000-r)*6.0/100); for (int j = 1; j = 0) { int year = x.bestYear(); System.out.println(name.toUpperCase() + " " + (1900+10*year)); } } } public static void main(String[] args) { String filename = "names-data.txt"; records = NameRecord.getAllNameRecords(filename); Scanner scan = new Scanner(System.in); // Ask for names and show their record while (true) { System.out.print("Enter name to search: "); String name = scan.nextLine().trim(); if (name.length() == 0) break; displayName(name); } // Ask for strings and show all names with that string while (true) { System.out.print("Enter string to search for: "); String s = scan.nextLine().trim(); if (s.length() == 0) break; displayNames(s); } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote