• 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

    • I totally disagree. Very little good comes out of governments all around the world manipulating everything they can and usually the people are not the benefactors. What you say about being restricted and expensive sounds almost like the arguments against firearms and why banning them will protect people as if making something illegal somehow will prevent the criminals from having and using them. AI being far less mainstream could simply mean the average person will not benefit, but "big brother" and the corporations will benefit, which is almost for sure NOT a good thing.
    • I do apologize to the author Mr. Sen for my rude comment, questioning his knowledge of the subject. It is I whom lacked knowledge of the subject. Sorry!
    • Hello All Have a MSI Pro B650 VC Wifi Rev 1.0 motherboard Ryzen 7 7700X Radeon 7800XT OC 16GB 32GB Teamgroup DDR 5 5600mhz Samsung 990 Pro 1TB Boot NVMe Samsung 990 Pro 2TB Game NVMe Lian Li Lancool Black ARGB 216 Case Seasonic Focus GX 750 Watt Power supply   Wondering today what is best spot to plug in the following items on system for performance and not bottle neck anything if i can help it Creative Pebble Pro USB C or A Speakers, ((Powered by External USB C to C PD Adapter)  Logitech G513 USB Gaming Keyboard Logitech G502X Wired Gaming Mouse Cyberpower UPS USB Cable for UPS Power Management/System shutdown External drives connected occasionally are as follows---WD My Book 8TB (primary backup drive)   Seagate 8TB in External USB 3.0 Enclosure,  Seagate Portable 1TB USB 3.0 drive,   WD My Passport (Blue) 2TB, and WD My Passport (Red) 2TB,    WD Elements 500GB USB 2.0 External (Oldest one, Christmas 2003)       **Do have a 7 Port Powered  USB Hub as well, but when i use that--that leaves only the USB Flash spot for something to directly connect to system if needed.    Rear USB C 2x2 unused right now as moved the Creative speakers off it to USB A port next to it, with a USB C to A Cable, as figured speakers didn't near audio from USB C port and tie up the high speed port**   Front Ports trying to limit use of, so i don't have Front I/O port go bad again, already had it replaced once by Lian Li support all the way from Taiwan over night ((Do get extra nervous at times on things,  so i might just be extra nervous for nothing lol))
    • "connect with audiences" is the most obvious corporate speak you can think of. I only bought Need for Speed from EA because it was the only racing game with cops in existence and I dig that. Now that they killed off NFS franchise, I have nothing to spend money on. EA is officially dead for me, just like Ubisoft which I've been boycotting for some 20 years now...
  • Recent Achievements

    • 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
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      496
    2. 2
      +Edouard
      203
    3. 3
      PsYcHoKiLLa
      127
    4. 4
      Steven P.
      83
    5. 5
      ATLien_0
      80
  • Tell a friend

    Love Neowin? Tell a friend!