Results 1 to 5 of 5

Thread: Help wit a guessing game

  1. #1
    Join Date
    Mar 2013

    Help wit a guessing game

    Hello people,
    so I am a beginner to javascript and am in web development class at the moment and our teacher wants us to create a javascript guessing game. The idea is we have 6 names that we have to put into an array and after the user enters their name into a prompt they are to guess the names of the sports. I got a start going but I am having major problems when the coding actually gets tough (probably easy for most but not me). Ive posted the instructions and am having a hell of a time at part a of the instructions. I was wondering if anyone could help me out with at least how to set up to code and what to use exactly, ive searched all around and could not find a good resource that has the information I need. So please if anyone would mind helping me out with this it would be hugely appreciated. Also here is the code I have so far.

    <!DOCTYPE html>
    	<title>Assignment 13</title>
    		var sports=new Array("Football", "Basketball", "Rollerblading", "Hiking", "Biking", "Swimming");
    		var person=prompt("Please enter your name","Enter Name Here");
    		if (person!=null && person!="");
      		//message after name will go into here x="Hello " + person + "! How are you today?";
    		var array=prompt("Guess the name of one of the sports in the array","Enter Sport Here");
    		if (array!=null && array!="");
    Instructions for the assignme

    Populate an array with these sports, typed as they are here: 
    Football, Basketball, Rollerblading, Hiking, Biking, Swimming
    2. Prompt the user for their name. This will be used in the messages later.
    3. Prompt the user to guess a sport to see if they can guess one that is in the array. Do 
    not show them what is in the array. This will be repeated 3 times. Read on for the 
    processing of each guess.
    a. For each guess, compare what the user enters to the values in the array. 
    (Remember: a lower case letter is not the same as an uppercase letter and you 
    cannot require the user to enter their guess in a specific case) If what the user 
    entered is not in the array then give them a message to “Guess Again”. If they 
    guessed one of the sports correctly, give them a message like “You are good, try 
    it again!”
    b. For each guess, if the user guesses the right sport then give them 5 points and 
    go on to the next prompt so they can try to guess the next sport. If they don’t 
    guess it then, take away 5 points and go on to the next prompt.
    c. If the user hits the cancel key on the prompt window, before the 3 times, then 
    end the game and give them an alert that says “Playersname, you are chicken!”, 
    where the Playersname is what they entered in #2 above.
    4. After 3 times playing, if the players score is 15 then tell them “Playername, you are a
    mind reader!”
    5. After 3 times playing, if the players score is 0-15 then tell them “Playername, you are 
    not bad”
    6. After 3 times playing, if the players score is less than zero then tell them “Playername, 
    you do not demonstrate ESP tendencies at this time.”

  2. #2
    Join Date
    Feb 2013
    I think your teacher is testing to see if you remember how to compare things without caring about different case characters. In other words, if the user guesses "football", your current script has the item "Football". Note the upper-case 'F' in yours, the user would be considered wrong in a naive condition:
    if ("football" === "Football") // false
    So how would you get the expected result-- (true)?
    To make that harder, "Football" is inside of an array, so there's a question of how to check an array for a value.

    After that is done, 'If what the user entered is not in the array then give them a message to “Guess Again”.'
    Finally: 'If they guessed one of the sports correctly, give them a message like “You are good, try it again!”'

    If you still give up on trying to get it on your own, then here is little a spoiler. I didn't explain anything about random or assuring the user guesses 3 times, nor did I remember the user's name I tried to keep the problem revolving around 'a'. The rest is all up to you:

    If you are seriously interested in learning web development, contact me on this forum for help.
    Last edited by s-p-n; 03-21-2013 at 01:49 AM.

  3. #3
    Join Date
    Feb 2013
    Edit:: accidental double post..

  4. #4
    Join Date
    Mar 2013
    Ok thanks very much, but im still having major problems getting anything to work as soon as the comparisons start, even with what you did with jsfiddle I tried looking at that code and seeing if any of it would translate to my script it just didnt work, does anyone else have an idea of how I can fix this up? I feel like there I am just committing simple mistakes that are killing the entire script but I am not entirely sure.

  5. #5
    Join Date
    Dec 2005


    This is not your homework assignment, but maybe it will give you some hints about your own assignment.

    <!DOCTYPE html>
    <html lang="en">
    <meta charset="utf-8" />
    <title> Untitled </title>
    <script type="text/javascript">
    function $_(IDS) { return document.getElementById(IDS); }
    var sports = [
      "Football", "Basketball", "Rollerblading", "Hiking", "Biking", "Swimming",
      'Roping', 'Riding', 'Wrestling', 'Baseball', 'Hockey', 'Rodeo',
      'Tennis', 'NASCAR', 'Bowling', 'Cricket', 'Soccer', 'Diving'
    The sport starting with: <span id="sport"></span><p>
    Your guess: <input type="text" id="guess" value="">
     <button onclick="checkGuess()">Check guess</button>
    <p><div id="result"></div>
    <script type="text/javascript">
    // Sample from:
    var guessCount = 0;  // make game easier by increasing initial guess count
    var rpick = Math.floor(Math.random()*sports.length);    // get a random index pointer
    var spick = sports[rpick].toLowerCase();                // get the picked sport from the index
    $_('sport').innerHTML = spick.substring(0,(guessCount+1));  // display 1st letter
    function checkGuess() {
      $_('sport').innerHTML = spick.substring(0,(guessCount+1));  // display more letters
      var userGuess = $_('guess').value.toLowerCase();
      if (userGuess == '') { alert('Need an entry to continue'); }
      var fnd = -1;
      for (var i=0; i<sports.length; i++) {
        fnd = spick.indexOf(userGuess);  if (fnd != -1) { break; }
      var msg = '';
      if (userGuess == spick) { msg = 'Wow!  Brilliant!  Most Excellent! '; }
      else {
        if (fnd != -1) { msg = 'Pretty close'; }
        else { msg = 'Nope.  Try again!'; }
      $_('result').innerHTML = msg;
    Last edited by JMRKER; 03-21-2013 at 10:16 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.09794 seconds
  • Memory Usage 2,890KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates