• 0

JavaScript Question


Question

Is it possible to add a bit of code into a specified area?

Possibly there's a find and replace sort of function? Obviously not quite find and replace, but similar.

Here's a simple example:

if (num==2 || num==3 || num==4 /*this*/)

{execute some code}

Now, you do a check to see if the number 5 needs to be added to the list, and it does. So you replace '/*this*/' with '|| num==5 /*this*/' and check if 6 needs to be added.

Would that be possible? Either that or another way to add some code into a specified area, depending on some factors.

Link to comment
https://www.neowin.net/forum/topic/791752-javascript-question/
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Cheers nvme.

I'm looking for a solution that doesn't need hard-coding. If the number the user inputs is really high I want the code to dynamically work out the conditions needed in the if statement.

If it will help, I'll explain my problem a little more.

I'm making a prime number calculator, as a learning experience, learn by doing 'n' that.

Obviously the higher the number the user puts in, the higher the number it must be checked against.

For example, I currently have it check whether the number is divisible by 2, 3, 5 & 7. But if the user put in 150, 121 would appear as it's not divisible by those 4 numbers, but is by 11. If someone puts in a number such as 2600 then the script will need to check whether it's divisible by 13, 17 etc.

I could hard-code it but I was hoping for a more flexible solution that would be quite long-winded but be accurate with high numbers.

I'll probably end up having to hard-code figures in, but that's not perfect and wont extend my knowledge of JavaScript.

  • 0

Yeah Mike that's exactly what I worked out through some scribbled notes yesterday :D

It should be able to work, the only problem is I've got to try and and insert the values from 1-the square root (less the remainder) into the equation and I'm not sure how (/even if its possible).

I've got this so far (it's a little bit all over the place until I tie it together):

<script>
/*	
	// This is square rooting the high stop figure.
	var square=Math.sqrt(highstop);
	document.write(square + "<br />");
	// Find the remainder of the square root.
	var rem=square%1;
	document.write(rem + "<br />");
	// Take the remainder off the square root
	square=square-rem;
	document.write(square + "<br />");
	// If the square root greater than or equal to 11, execute the code. 
	if (square>=11)
	{
		//

	}	



	var a, b, c, d
	var highstop=400;

	for (num=1; num<=highstop; num++)
	{
		// Creating variables that hold the remainder when num is divided by these numbers
		a=num%2; b=num%3; c=num%5; d=num%7;
		// If the number is 2, 3, 5 or 7 write it now
		if (num==2 || num==3 || num==5 || num==7)
		{
			document.write(num + ", ");
		}
		// If the remainders of all aren't 0 and if the number isn't 1 then write it out as a prime
		else if (a!=0 && b!=0 && c!=0 && d!=0 && num!=1)
		{
			document.write(num + ", ");
		}
	}
</script>

  • 0
use an array that contains all of the possible values. then loop through until you find your match.

As I say I'm new to JavaScript but after talking this through with a mate and thinking it over I think using an Array would solve the problem, as it seems I can populate the array as and when I want to.

So I'll have an if loop that will check whether the square root of the number input is higher than or equal to 11. If it is, it'll execute some code that will add an element to the array, then loop round again until all the required numbers are added.

Then the condition that checks each number to see whether they're divisible by the newly populated array of numbers will grab the figures from the array and add it to an an if condition and then add some text to a specified div each time until the high limit set by the user is hit.

If any of you understand what I've just written, you've done better than I.

  • 0

A quick question:

Why doesn't this work?

	var primeCheck = [ 2, 3, 5, 7 ];

	// Get the square and and divide it by the variables in array primeCheck, then assign the remainder to a var
	var a1=square%primeCheck[0];
	var a2=square%primeCheck[1];
	var a3=square%primeCheck[2];
	var a4=square%primecheck[3];
	alert (a1);

If I do an alert like this before that code it works:

alert (primeCheck[0]);

I'm new to arrays (just started today) so it may be something obvious, can't spot it though.

Worth noting that anything after that var a1... bit of code doesn't work as JavaScript flips out, doesn't like it for sure.

  • 0
A quick question:

Why doesn't this work?

	var primeCheck = [ 2, 3, 5, 7 ];

	// Get the square and and divide it by the variables in array primeCheck, then assign the remainder to a var
	var a1=square%primeCheck[0];
	var a2=square%primeCheck[1];
	var a3=square%primeCheck[2];
	var a4=square%primecheck[3];
	alert (a1);

If I do an alert like this before that code it works:

alert (primeCheck[0]);

I'm new to arrays (just started today) so it may be something obvious, can't spot it though.

Worth noting that anything after that var a1... bit of code doesn't work as JavaScript flips out, doesn't like it for sure.

its probably an issue with what square is.

i.e.

var square = 10;

a1=square%primeCheck[0];

alert(a1);

will output 0 (10%2=0)

if you're attempting to do a function call that returns a number you're forgetting parenthesis ()

  • 0

Cheers ekw.

The variable square just stores a number string.

Doing this before the array call works too:

alert(square + ", " + primeCheck[0]);

I'll keep at it, but I'm not quite sure what's wrong at the mo :blush:

EDIT: Absolute noob of an error. Didn't capitalise Check in primeCheck in the 4th attempt. Took a while to spot that!

Edited by Fred_Xenonsoft
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Age 16, old enough to get a full-time job, your own bank account, a passport, get married, even join the military and go to war. But talking to your friends on the internet? Oh hell no!
    • I remember when all games had demos; it was a normal thing, not a limited time promotion.
    • Forza Horizon 6 gets big bug-fixing and balancing update by Taras Buria Today, Playground Games released a big Forza Horizon 6 update with a long list of fixes, patches, and balancing tweaks that the studio promised earlier. Version 375.327 is now available on Steam, Microsoft Store, and Xbox, offering users improvements for AI, audio, design, performance, road discovery, upgrades, visuals, online play, and more. Some of the most notable changes in the Series 2 update include rebalanced drivatars, particularly their difficulty and race start behavior. As such, the game should be more balanced on higher difficulty levels, and AI cars should not shoot out when the race starts as if they have rocket boosters. Speaking of difficulty, developers nerfed Drag Tires physics for a more expected and realistic behavior. They are no longer the go-to option for record-breaking times in road racing, and all leaderboard entries with drag tires will be removed. Completionists will also be glad to get a new feature that lets you see road discovery percentage in each region, which should make discovering all roads easier while keeping it quite challenging and interesting (I spent quite a long time finding the last road). Festival Playlist is also getting some much-needed fixes, including patches for bugs that allowed completing Seasonal Jobs ahead of time or where weekly challenges would not unlock for some players. Developers will retroactively give reward points to all who could not complete all challenges due to these bugs. Other changes include changes to Horizon Play progression so that it is easier to reach Level 100, audio improvements on lower-spec devices, fixes for visual glitches, including pixelated smoke, and more. Developers also addressed the currently non-working Eliminator, an online mode gamers used to farm credits with a Hummer EV exploit. Playground Games plans to re-enable it soon. As a gesture of goodwill, players will get a free McLaren Sabre. Those who used the exploit will not be banned, but developers plan to roll back credits to a maximum of 10M for all who farmed credits using the exploit. You can find the complete changelog for the latest Forza Horizon 6 update here.
    • "Samsung is shutting down yet another app used by millions" I will fix the clickbait title for you, free-of-charge: "Samsung shutting down it's Max VPN app"
  • Recent Achievements

    • First Post
      Cosminus earned a badge
      First Post
    • One Year In
      ThatGuyOnline earned a badge
      One Year In
    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      483
    2. 2
      +Edouard
      187
    3. 3
      PsYcHoKiLLa
      122
    4. 4
      Steven P.
      85
    5. 5
      neufuse
      73
  • Tell a friend

    Love Neowin? Tell a friend!